1
- import { AfterContentChecked , AfterViewChecked , Component , ContentChildren , QueryList , ViewChild } from '@angular/core' ;
2
- import { async , TestBed } from '@angular/core/testing' ;
1
+ import { AfterContentChecked , AfterViewChecked , Component , ContentChildren , QueryList , ViewChild , NgZone } from '@angular/core' ;
2
+ import { Location } from '@angular/common' ;
3
+ import { Router } from '@angular/router' ;
4
+ import { async , inject , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
3
5
import { RouterTestingModule } from '@angular/router/testing' ;
4
6
import { By } from '@angular/platform-browser' ;
5
7
import { IgxBottomNavComponent ,
@@ -9,15 +11,28 @@ import { IgxBottomNavComponent,
9
11
IgxTabTemplateDirective } from './tabbar.component' ;
10
12
11
13
import { configureTestSuite } from '../test-utils/configure-suite' ;
14
+ import { RoutingViewComponentsModule ,
15
+ RoutingView1Component ,
16
+ RoutingView2Component ,
17
+ RoutingView3Component } from './routing-view-components' ;
18
+ import { wait } from '../test-utils/ui-interactions.spec' ;
12
19
13
- describe ( 'TabBar' , ( ) => {
20
+
21
+ fdescribe ( 'TabBar' , ( ) => {
14
22
configureTestSuite ( ) ;
15
23
beforeEach ( async ( ( ) => {
24
+
25
+ const testRoutes = [
26
+ { path : 'view1' , component : RoutingView1Component } ,
27
+ { path : 'view2' , component : RoutingView2Component } ,
28
+ { path : 'view3' , component : RoutingView3Component }
29
+ ] ;
30
+
16
31
TestBed . configureTestingModule ( {
17
- declarations : [ TabBarTestComponent , BottomTabBarTestComponent , TemplatedTabBarTestComponent ] ,
18
- imports : [ IgxBottomNavModule , RouterTestingModule ]
32
+ declarations : [ TabBarTestComponent , BottomTabBarTestComponent , TemplatedTabBarTestComponent , TabBarRoutingTestComponent ] ,
33
+ imports : [ IgxBottomNavModule , RoutingViewComponentsModule , RouterTestingModule . withRoutes ( testRoutes ) ] ,
19
34
} )
20
- . compileComponents ( ) ;
35
+ . compileComponents ( ) ;
21
36
} ) ) ;
22
37
23
38
it ( 'should initialize igx-bottom-nav, igx-tab-panel and igx-tab' , ( ) => {
@@ -149,6 +164,77 @@ describe('TabBar', () => {
149
164
150
165
tabbar . tabs . forEach ( ( tab ) => expect ( tab . relatedPanel . customTabTemplate ) . toBeDefined ( ) ) ;
151
166
} ) ;
167
+
168
+ fit ( 'should navigate to the correct URL when clicking on tab buttons' , fakeAsync ( ( ) => {
169
+
170
+ const router = TestBed . get ( Router ) ;
171
+ const location = TestBed . get ( Location ) ;
172
+ const fixture = TestBed . createComponent ( TabBarRoutingTestComponent ) ;
173
+ const bottomNav = fixture . componentInstance . bottomNavComp ;
174
+ fixture . detectChanges ( ) ;
175
+
176
+ fixture . ngZone . run ( ( ) => { router . initialNavigation ( ) ; } ) ;
177
+
178
+ tick ( ) ;
179
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
180
+
181
+ fixture . ngZone . run ( ( ) => { bottomNav . tabs . toArray ( ) [ 2 ] . select ( ) ; } ) ;
182
+ tick ( ) ;
183
+ expect ( location . path ( ) ) . toBe ( '/view3' ) ;
184
+
185
+ fixture . ngZone . run ( ( ) => { bottomNav . tabs . toArray ( ) [ 1 ] . select ( ) ; } ) ;
186
+ tick ( ) ;
187
+ expect ( location . path ( ) ) . toBe ( '/view2' ) ;
188
+
189
+ fixture . ngZone . run ( ( ) => { bottomNav . tabs . toArray ( ) [ 0 ] . select ( ) ; } ) ;
190
+ tick ( ) ;
191
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
192
+ } ) ) ;
193
+
194
+ fit ( 'should select the correct tab button/panel when navigating an URL' , fakeAsync ( ( ) => {
195
+
196
+ const router = TestBed . get ( Router ) ;
197
+ const location = TestBed . get ( Location ) ;
198
+ const fixture = TestBed . createComponent ( TabBarRoutingTestComponent ) ;
199
+ const bottomNav = fixture . componentInstance . bottomNavComp ;
200
+ fixture . detectChanges ( ) ;
201
+
202
+ fixture . ngZone . run ( ( ) => { router . initialNavigation ( ) ; } ) ;
203
+ tick ( ) ;
204
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
205
+ expect ( bottomNav . selectedIndex ) . toBe ( 0 ) ;
206
+ expect ( bottomNav . panels . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
207
+ expect ( bottomNav . tabs . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
208
+
209
+
210
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view3' ] ) ; } ) ;
211
+ tick ( ) ;
212
+ expect ( location . path ( ) ) . toBe ( '/view3' ) ;
213
+ fixture . detectChanges ( ) ;
214
+ expect ( bottomNav . selectedIndex ) . toBe ( 2 ) ;
215
+ expect ( bottomNav . panels . toArray ( ) [ 2 ] . isSelected ) . toBe ( true ) ;
216
+ expect ( bottomNav . tabs . toArray ( ) [ 2 ] . isSelected ) . toBe ( true ) ;
217
+
218
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view2' ] ) ; } ) ;
219
+ tick ( ) ;
220
+ expect ( location . path ( ) ) . toBe ( '/view2' ) ;
221
+ fixture . detectChanges ( ) ;
222
+ expect ( bottomNav . selectedIndex ) . toBe ( 1 ) ;
223
+ expect ( bottomNav . panels . toArray ( ) [ 1 ] . isSelected ) . toBe ( true ) ;
224
+ expect ( bottomNav . tabs . toArray ( ) [ 1 ] . isSelected ) . toBe ( true ) ;
225
+
226
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view1' ] ) ; } ) ;
227
+ tick ( ) ;
228
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
229
+ fixture . detectChanges ( ) ;
230
+ expect ( bottomNav . selectedIndex ) . toBe ( 0 ) ;
231
+ expect ( bottomNav . panels . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
232
+ expect ( bottomNav . tabs . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
233
+
234
+ } ) ) ;
235
+
236
+
237
+
152
238
} ) ;
153
239
154
240
@Component ( {
@@ -256,3 +342,35 @@ class TemplatedTabBarTestComponent {
256
342
@ViewChild ( IgxBottomNavComponent ) public tabbar : IgxBottomNavComponent ;
257
343
@ViewChild ( 'wrapperDiv' ) public wrapperDiv : any ;
258
344
}
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+ @Component ( {
354
+ template : `
355
+ <div #wrapperDiv>
356
+ <div>
357
+ <router-outlet></router-outlet>
358
+ </div>
359
+ <igx-bottom-nav>
360
+ <igx-tab-panel label="Tab 1" routerLink="/view1">
361
+ Content in tab # 1
362
+ </igx-tab-panel>
363
+ <igx-tab-panel label="Tab 2" routerLink="/view2">
364
+ Content in tab # 2
365
+ </igx-tab-panel>
366
+ <igx-tab-panel label="Tab 3" routerLink="/view3">
367
+ Content in tab # 3
368
+ </igx-tab-panel>
369
+ </igx-bottom-nav>
370
+ </div>
371
+ `
372
+ } )
373
+ class TabBarRoutingTestComponent {
374
+ @ViewChild ( IgxBottomNavComponent )
375
+ public bottomNavComp : IgxBottomNavComponent ;
376
+ }
0 commit comments