Skip to content

Commit 1bd5961

Browse files
committed
test(bottomNav): adding routing tests for the bottomNav component #4297
1 parent e7b2cb0 commit 1bd5961

File tree

4 files changed

+149
-10
lines changed

4 files changed

+149
-10
lines changed

projects/igniteui-angular/src/lib/tabbar/tabbar.component.spec.ts

+124-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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';
35
import { RouterTestingModule } from '@angular/router/testing';
46
import { By } from '@angular/platform-browser';
57
import { IgxBottomNavComponent,
@@ -9,15 +11,28 @@ import { IgxBottomNavComponent,
911
IgxTabTemplateDirective } from './tabbar.component';
1012

1113
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';
1219

13-
describe('TabBar', () => {
20+
21+
fdescribe('TabBar', () => {
1422
configureTestSuite();
1523
beforeEach(async(() => {
24+
25+
const testRoutes = [
26+
{ path: 'view1', component: RoutingView1Component },
27+
{ path: 'view2', component: RoutingView2Component },
28+
{ path: 'view3', component: RoutingView3Component }
29+
];
30+
1631
TestBed.configureTestingModule({
17-
declarations: [TabBarTestComponent, BottomTabBarTestComponent, TemplatedTabBarTestComponent],
18-
imports: [IgxBottomNavModule, RouterTestingModule]
32+
declarations: [TabBarTestComponent, BottomTabBarTestComponent, TemplatedTabBarTestComponent, TabBarRoutingTestComponent],
33+
imports: [IgxBottomNavModule, RoutingViewComponentsModule, RouterTestingModule.withRoutes(testRoutes)],
1934
})
20-
.compileComponents();
35+
.compileComponents();
2136
}));
2237

2338
it('should initialize igx-bottom-nav, igx-tab-panel and igx-tab', () => {
@@ -149,6 +164,77 @@ describe('TabBar', () => {
149164

150165
tabbar.tabs.forEach((tab) => expect(tab.relatedPanel.customTabTemplate).toBeDefined());
151166
});
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+
152238
});
153239

154240
@Component({
@@ -256,3 +342,35 @@ class TemplatedTabBarTestComponent {
256342
@ViewChild(IgxBottomNavComponent) public tabbar: IgxBottomNavComponent;
257343
@ViewChild('wrapperDiv') public wrapperDiv: any;
258344
}
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+
}

src/app/bottomnav-routing/bottomnav-routing.sample.html

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
<div class="sample-content">
66
<article class="sample-column">
77
<div class="tabbar-wrapper" #tabbarEl>
8+
Programmatically change URL:<br>
9+
<button (click)="navigateUrl1()">/bottom-navigation-routing/view1</button><br>
10+
<button (click)="navigateUrl2()">/bottom-navigation-routing/view2</button><br>
11+
<button (click)="navigateUrl3()">/bottom-navigation-routing/view3</button><br>
812
<div>
913
<router-outlet></router-outlet>
1014
</div>
1115
<igx-bottom-nav>
12-
<igx-tab-panel #tab1 label="Tab 1" routerLink='/bottom-navigation-routing/view1'>
16+
<igx-tab-panel #tab1 label="Tab 1" routerLink="/bottom-navigation-routing/view1">
1317
Content in tab # 1
1418
</igx-tab-panel>
1519
<igx-tab-panel #tab2 label="Tab 2" [routerLink]="['/bottom-navigation-routing', 'view2']">
1620
Content in tab # 2
1721
</igx-tab-panel>
18-
<igx-tab-panel #tab3 label="Tab 3" routerLink='/bottom-navigation-routing/view3'>
22+
<igx-tab-panel #tab3 label="Tab 3" routerLink="/bottom-navigation-routing/view3">
1923
Content in tab # 3
2024
</igx-tab-panel>
2125
</igx-bottom-nav>

src/app/bottomnav-routing/bottomnav-routing.sample.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import { Component } from '@angular/core';
2-
2+
import { Router } from '@angular/router';
33
@Component({
44
selector: 'app-bottomnav-routing-sample',
55
styleUrls: ['bottomnav-routing.sample.css'],
66
templateUrl: 'bottomnav-routing.sample.html'
77
})
88
export class BottomNavRoutingSampleComponent {
9+
10+
constructor(private router: Router) {
11+
}
12+
13+
public navigateUrl1() {
14+
this.router.navigateByUrl('/bottom-navigation-routing/view1');
15+
}
16+
17+
public navigateUrl2() {
18+
this.router.navigateByUrl('/bottom-navigation-routing/view2');
19+
}
20+
21+
public navigateUrl3() {
22+
this.router.navigateByUrl('/bottom-navigation-routing/view3');
23+
}
924
}
1025

1126
@Component({

src/app/tabs-routing/tabs-routing.sample.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
Content in tab # 3
2121
</igx-tabs-group>
2222
</igx-tabs>
23-
<router-outlet></router-outlet>
23+
<div>
24+
<router-outlet></router-outlet>
25+
</div>
2426
</div>
2527
</div>
2628
</div>

0 commit comments

Comments
 (0)