1
- import { Component , DebugElement } from '@angular/core' ;
1
+ import { Component , DebugElement , ViewChildren , QueryList } from '@angular/core' ;
2
2
import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
5
5
import { UIRouterModule } from '../../src/uiRouterNgModule' ;
6
6
import { UISref } from '../../src/directives/uiSref' ;
7
- import { UIRouter } from '@uirouter/core' ;
7
+ import { UIRouter , TargetState , TransitionOptions } from '@uirouter/core' ;
8
8
import { Subject } from 'rxjs/Subject' ;
9
+ import { Subscription } from "rxjs/Subscription" ;
9
10
10
11
describe ( 'uiSref' , ( ) => {
11
12
@Component ( {
12
13
template : `
13
- <a [uiSref]="linkA" [target]="targetA"></a>
14
+ <a [uiSref]="linkA" [target]="targetA" [uiParams]="linkAParams" [uiOptions]="linkAOptions" ></a>
14
15
<a [uiSref]="linkB"></a>
15
16
`
16
17
} )
17
18
class TestComponent {
18
19
linkA : string ;
20
+ linkAParams : any ;
21
+ linkAOptions : TransitionOptions ;
19
22
targetA : string ;
20
23
linkB : string ;
21
24
25
+ @ViewChildren ( UISref ) srefs : QueryList < UISref > ;
26
+
27
+ get linkASref ( ) {
28
+ return this . srefs . first ;
29
+ }
30
+
31
+ get linkBSref ( ) {
32
+ return this . srefs . toArray ( ) [ 1 ] ;
33
+ }
34
+
22
35
constructor ( ) {
23
36
this . linkA = null ;
37
+ this . linkAParams = null ;
38
+ this . linkAOptions = null ;
24
39
this . targetA = '' ;
25
40
this . linkB = '' ;
26
41
}
@@ -40,6 +55,7 @@ describe('uiSref', () => {
40
55
des = fixture . debugElement . queryAll ( By . directive ( UISref ) ) ;
41
56
} ) ;
42
57
58
+
43
59
it ( 'should not bind "null" string to `href`' , ( ) => {
44
60
expect ( des [ 0 ] . nativeElement . hasAttribute ( 'href' ) ) . toBeFalsy ( ) ;
45
61
expect ( des [ 1 ] . nativeElement . hasAttribute ( 'href' ) ) . toBeFalsy ( ) ;
@@ -114,6 +130,75 @@ describe('uiSref', () => {
114
130
} ) ;
115
131
} ) ;
116
132
} ) ;
133
+
134
+ describe ( 'when the bound values change' , ( ) => {
135
+ let fixture : ComponentFixture < TestComponent > ;
136
+ let comp : TestComponent ;
137
+ let logger : TargetState [ ] ;
138
+ let subscription : Subscription ;
139
+
140
+ beforeEach ( ( ) => {
141
+ fixture = TestBed . configureTestingModule ( {
142
+ declarations : [ TestComponent ] ,
143
+ imports : [ UIRouterModule . forRoot ( { useHash : true } ) ]
144
+ } ) . createComponent ( TestComponent ) ;
145
+ fixture . detectChanges ( ) ;
146
+ comp = fixture . componentInstance ;
147
+ logger = [ ] ;
148
+ subscription = comp . linkASref . targetState$ . subscribe ( evt => logger . push ( evt ) ) ;
149
+ } ) ;
150
+
151
+ afterEach ( ( ) => {
152
+ subscription . unsubscribe ( ) ;
153
+ } ) ;
154
+
155
+ describe ( 'when the uiSref is empty' , ( ) => {
156
+ it ( 'should emit an empty target state event' , ( ) => {
157
+ expect ( logger . length ) . toBe ( 1 ) ;
158
+ expect ( logger [ 0 ] . name ( ) ) . toBeNull ( ) ;
159
+ } ) ;
160
+ } )
161
+
162
+ describe ( 'when the target state changes' , ( ) => {
163
+ beforeEach ( ( ) => {
164
+ comp . linkA = 'stateA' ;
165
+ fixture . detectChanges ( ) ;
166
+ } ) ;
167
+
168
+ it ( 'should emit an event' , ( ) => {
169
+ expect ( logger . length ) . toBe ( 2 ) ;
170
+ expect ( logger [ 1 ] . name ( ) ) . toBe ( 'stateA' ) ;
171
+ } ) ;
172
+ } ) ;
173
+
174
+ describe ( 'when the target params change' , ( ) => {
175
+ const params = { paramA : 'paramA' } ;
176
+
177
+ beforeEach ( ( ) => {
178
+ comp . linkAParams = params ;
179
+ fixture . detectChanges ( ) ;
180
+ } ) ;
181
+
182
+ it ( 'should emit an event' , ( ) => {
183
+ expect ( logger . length ) . toBe ( 2 ) ;
184
+ expect ( logger [ 1 ] . params ( ) ) . toEqual ( params ) ;
185
+ } ) ;
186
+ } ) ;
187
+
188
+ describe ( 'when the transition options change' , ( ) => {
189
+ const options : TransitionOptions = { custom : 'custom' } ;
190
+
191
+ beforeEach ( ( ) => {
192
+ comp . linkAOptions = options ;
193
+ fixture . detectChanges ( ) ;
194
+ } ) ;
195
+
196
+ it ( 'should emit an event' , ( ) => {
197
+ expect ( logger . length ) . toBe ( 2 ) ;
198
+ expect ( logger [ 1 ] . options ( ) . custom ) . toEqual ( options . custom ) ;
199
+ } ) ;
200
+ } )
201
+ } ) ;
117
202
} ) ;
118
203
} ) ;
119
204
0 commit comments