1
1
import { RouterLinkWithHref } from '@angular/router' ;
2
2
import { RouterTestingModule } from '@angular/router/testing' ;
3
- import { NavigationService , TrackDirective } from '@hypertrace/common' ;
3
+ import {
4
+ ExternalNavigationWindowHandling ,
5
+ NavigationParamsType ,
6
+ NavigationService ,
7
+ TrackDirective
8
+ } from '@hypertrace/common' ;
4
9
import { createHostFactory , mockProvider , SpectatorHost } from '@ngneat/spectator/jest' ;
5
10
import { MockDirective } from 'ng-mocks' ;
6
11
import { of } from 'rxjs' ;
@@ -26,10 +31,12 @@ describe('Link component', () => {
26
31
27
32
const anchorElement = spectator . query ( '.ht-link' ) ;
28
33
expect ( anchorElement ) . toExist ( ) ;
29
- expect ( anchorElement ) . toHaveClass ( 'ht-link disabled' ) ;
34
+ expect ( anchorElement ) . toHaveClass ( 'ht-link internal disabled' ) ;
30
35
} ) ;
31
36
32
37
test ( 'Link should navigate correctly to external URLs' , ( ) => {
38
+ window . open = jest . fn ( ) ;
39
+
33
40
spectator = createHost ( `<ht-link [paramsOrUrl]="paramsOrUrl"></ht-link>` , {
34
41
hostProps : {
35
42
paramsOrUrl : 'http://test.hypertrace.ai'
@@ -47,28 +54,30 @@ describe('Link component', () => {
47
54
] ,
48
55
extras : { skipLocationChange : true }
49
56
} )
50
- )
57
+ ) ,
58
+ isExternalUrl : jest . fn ( ) . mockReturnValue ( true )
51
59
} )
52
60
]
53
61
} ) ;
54
62
55
- const anchorElement = spectator . query ( '.ht-link' ) ;
63
+ let anchorElement = spectator . query ( '.ht-link.external' ) as HTMLAnchorElement ;
56
64
expect ( anchorElement ) . toExist ( ) ;
57
65
expect ( anchorElement ) . not . toHaveClass ( 'disabled' ) ;
58
- const routerLinkDirective = spectator . query ( RouterLinkWithHref ) ;
59
-
60
- expect ( routerLinkDirective ) . toBeDefined ( ) ;
61
- expect ( routerLinkDirective ?. routerLink ) . toEqual ( [
62
- '/external' ,
63
- {
64
- url : 'http://test.hypertrace.ai' ,
65
- navType : 'same_window'
66
+ expect ( anchorElement . href ) . toBe ( 'http://test.hypertrace.ai/' ) ;
67
+ spectator . click ( anchorElement ) ;
68
+ expect ( window . open ) . toHaveBeenLastCalledWith ( 'http://test.hypertrace.ai' , '_self' ) ;
69
+
70
+ // With new window
71
+ spectator . setHostInput ( {
72
+ paramsOrUrl : {
73
+ navType : NavigationParamsType . External ,
74
+ url : '/test' ,
75
+ windowHandling : ExternalNavigationWindowHandling . NewWindow
66
76
}
67
- ] ) ;
68
- expect ( routerLinkDirective ! . skipLocationChange ) . toBeTruthy ( ) ;
69
- expect ( routerLinkDirective ! . queryParams ) . toBeUndefined ( ) ;
70
- expect ( routerLinkDirective ! . queryParamsHandling ) . toBeUndefined ( ) ;
71
- expect ( routerLinkDirective ! . replaceUrl ) . toBeUndefined ( ) ;
77
+ } ) ;
78
+ expect ( anchorElement . href ) . toBe ( 'http://localhost/test' ) ;
79
+ spectator . click ( anchorElement ) ;
80
+ expect ( window . open ) . toHaveBeenLastCalledWith ( '/test' , undefined ) ;
72
81
} ) ;
73
82
74
83
test ( 'Link should navigate correctly to internal relative URLs' , ( ) => {
@@ -83,7 +92,7 @@ describe('Link component', () => {
83
92
]
84
93
} ) ;
85
94
86
- const anchorElement = spectator . query ( '.ht-link' ) ;
95
+ const anchorElement = spectator . query ( '.ht-link.internal ' ) ;
87
96
expect ( anchorElement ) . toExist ( ) ;
88
97
expect ( anchorElement ) . not . toHaveClass ( 'disabled' ) ;
89
98
const routerLinkDirective = spectator . query ( RouterLinkWithHref ) ;
@@ -108,7 +117,7 @@ describe('Link component', () => {
108
117
]
109
118
} ) ;
110
119
111
- const anchorElement = spectator . query ( '.ht-link' ) ;
120
+ const anchorElement = spectator . query ( '.ht-link.internal ' ) ;
112
121
expect ( anchorElement ) . toExist ( ) ;
113
122
expect ( anchorElement ) . not . toHaveClass ( 'disabled' ) ;
114
123
const routerLinkDirective = spectator . query ( RouterLinkWithHref ) ;
@@ -133,9 +142,9 @@ describe('Link component', () => {
133
142
]
134
143
} ) ;
135
144
136
- const anchorElement = spectator . query ( '.ht-link' ) ;
145
+ const anchorElement = spectator . query ( '.ht-link.internal ' ) ;
137
146
expect ( anchorElement ) . toExist ( ) ;
138
- expect ( anchorElement ) . toHaveClass ( 'ht-link disabled' ) ;
147
+ expect ( anchorElement ) . toHaveClass ( 'ht-link internal disabled' ) ;
139
148
140
149
const routerLinkDirective = spectator . query ( RouterLinkWithHref ) ;
141
150
0 commit comments