1
1
import { Component , QueryList , ViewChild } from '@angular/core' ;
2
2
import { async , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
3
+ import { Location } from '@angular/common' ;
4
+ import { Router } from '@angular/router' ;
3
5
import { RouterTestingModule } from '@angular/router/testing' ;
4
6
import { IgxTabItemComponent } from './tab-item.component' ;
5
7
import { IgxTabsGroupComponent } from './tabs-group.component' ;
@@ -12,15 +14,26 @@ import { IgxToggleModule } from '../directives/toggle/toggle.directive';
12
14
import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
13
15
import { By } from '@angular/platform-browser' ;
14
16
import { UIInteractions } from '../test-utils/ui-interactions.spec' ;
17
+ import { RoutingViewComponentsModule ,
18
+ RoutingView1Component ,
19
+ RoutingView2Component ,
20
+ RoutingView3Component } from './routing-view-components' ;
15
21
16
22
describe ( 'IgxTabs' , ( ) => {
17
23
configureTestSuite ( ) ;
18
24
beforeEach ( async ( ( ) => {
25
+
26
+ const testRoutes = [
27
+ { path : 'view1' , component : RoutingView1Component } ,
28
+ { path : 'view2' , component : RoutingView2Component } ,
29
+ { path : 'view3' , component : RoutingView3Component }
30
+ ] ;
31
+
19
32
TestBed . configureTestingModule ( {
20
33
declarations : [ TabsTestComponent , TabsTest2Component , TemplatedTabsTestComponent ,
21
- TabsTestSelectedTabComponent , TabsTestCustomStylesComponent , TabsTestBug4420Component ] ,
34
+ TabsTestSelectedTabComponent , TabsTestCustomStylesComponent , TabsTestBug4420Component , TabsRoutingTestComponent ] ,
22
35
imports : [ IgxTabsModule , IgxButtonModule , IgxDropDownModule , IgxToggleModule ,
23
- BrowserAnimationsModule , RouterTestingModule ]
36
+ RoutingViewComponentsModule , BrowserAnimationsModule , RouterTestingModule . withRoutes ( testRoutes ) ]
24
37
} )
25
38
. compileComponents ( ) ;
26
39
} ) ) ;
@@ -399,6 +412,73 @@ describe('IgxTabs', () => {
399
412
const indicator = dom . query ( By . css ( '.igx-tabs__header-menu-item-indicator' ) ) ;
400
413
expect ( indicator . nativeElement . style . width ) . toBe ( '90px' ) ;
401
414
} ) ) ;
415
+
416
+ fit ( 'should navigate to the correct URL when clicking on tab buttons' , fakeAsync ( ( ) => {
417
+
418
+ const router = TestBed . get ( Router ) ;
419
+ const location = TestBed . get ( Location ) ;
420
+ const fixture = TestBed . createComponent ( TabsRoutingTestComponent ) ;
421
+ const tabsComp = fixture . componentInstance . tabsComp ;
422
+ fixture . detectChanges ( ) ;
423
+
424
+ fixture . ngZone . run ( ( ) => { router . initialNavigation ( ) ; } ) ;
425
+
426
+ tick ( ) ;
427
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
428
+
429
+ fixture . ngZone . run ( ( ) => { tabsComp . tabs . toArray ( ) [ 2 ] . select ( ) ; } ) ;
430
+ tick ( ) ;
431
+ expect ( location . path ( ) ) . toBe ( '/view3' ) ;
432
+
433
+ fixture . ngZone . run ( ( ) => { tabsComp . tabs . toArray ( ) [ 1 ] . select ( ) ; } ) ;
434
+ tick ( ) ;
435
+ expect ( location . path ( ) ) . toBe ( '/view2' ) ;
436
+
437
+ fixture . ngZone . run ( ( ) => { tabsComp . tabs . toArray ( ) [ 0 ] . select ( ) ; } ) ;
438
+ tick ( ) ;
439
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
440
+ } ) ) ;
441
+
442
+ fit ( 'should select the correct tab button/panel when navigating an URL' , fakeAsync ( ( ) => {
443
+
444
+ const router = TestBed . get ( Router ) ;
445
+ const location = TestBed . get ( Location ) ;
446
+ const fixture = TestBed . createComponent ( TabsRoutingTestComponent ) ;
447
+ const tabsComp = fixture . componentInstance . tabsComp ;
448
+ fixture . detectChanges ( ) ;
449
+
450
+ fixture . ngZone . run ( ( ) => { router . initialNavigation ( ) ; } ) ;
451
+ tick ( ) ;
452
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
453
+ expect ( tabsComp . selectedIndex ) . toBe ( 0 ) ;
454
+ expect ( tabsComp . groups . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
455
+ expect ( tabsComp . tabs . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
456
+
457
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view3' ] ) ; } ) ;
458
+ tick ( ) ;
459
+ expect ( location . path ( ) ) . toBe ( '/view3' ) ;
460
+ fixture . detectChanges ( ) ;
461
+ expect ( tabsComp . selectedIndex ) . toBe ( 2 ) ;
462
+ expect ( tabsComp . groups . toArray ( ) [ 2 ] . isSelected ) . toBe ( true ) ;
463
+ expect ( tabsComp . tabs . toArray ( ) [ 2 ] . isSelected ) . toBe ( true ) ;
464
+
465
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view2' ] ) ; } ) ;
466
+ tick ( ) ;
467
+ expect ( location . path ( ) ) . toBe ( '/view2' ) ;
468
+ fixture . detectChanges ( ) ;
469
+ expect ( tabsComp . selectedIndex ) . toBe ( 1 ) ;
470
+ expect ( tabsComp . groups . toArray ( ) [ 1 ] . isSelected ) . toBe ( true ) ;
471
+ expect ( tabsComp . tabs . toArray ( ) [ 1 ] . isSelected ) . toBe ( true ) ;
472
+
473
+ fixture . ngZone . run ( ( ) => { router . navigate ( [ '/view1' ] ) ; } ) ;
474
+ tick ( ) ;
475
+ expect ( location . path ( ) ) . toBe ( '/view1' ) ;
476
+ fixture . detectChanges ( ) ;
477
+ expect ( tabsComp . selectedIndex ) . toBe ( 0 ) ;
478
+ expect ( tabsComp . groups . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
479
+ expect ( tabsComp . tabs . toArray ( ) [ 0 ] . isSelected ) . toBe ( true ) ;
480
+
481
+ } ) ) ;
402
482
} ) ;
403
483
404
484
@Component ( {
@@ -586,3 +666,28 @@ class TabsTestCustomStylesComponent {
586
666
class TabsTestBug4420Component {
587
667
@ViewChild ( IgxTabsComponent ) public tabs : IgxTabsComponent ;
588
668
}
669
+
670
+ @Component ( {
671
+ template : `
672
+ <div #wrapperDiv>
673
+ <igx-tabs>
674
+ <igx-tabs-group label="Tab 1" routerLink="/view1">
675
+ Content in tab # 1
676
+ </igx-tabs-group>
677
+ <igx-tabs-group label="Tab 2" routerLink="/view2">
678
+ Content in tab # 2
679
+ </igx-tabs-group>
680
+ <igx-tabs-group label="Tab 3" routerLink="/view3">
681
+ Content in tab # 3
682
+ </igx-tabs-group>
683
+ </igx-tabs>
684
+ <div>
685
+ <router-outlet></router-outlet>
686
+ </div>
687
+ </div>
688
+ `
689
+ } )
690
+ class TabsRoutingTestComponent {
691
+ @ViewChild ( IgxTabsComponent )
692
+ public tabsComp : IgxTabsComponent ;
693
+ }
0 commit comments