Skip to content

Commit 918bca8

Browse files
committed
test(tabs/bottomNav): adding test for checking initial selection #4297
1 parent 5a5c0a2 commit 918bca8

File tree

5 files changed

+122
-20
lines changed

5 files changed

+122
-20
lines changed

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

+49-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { BottomNavRoutingViewComponentsModule,
1515
import { Router } from '@angular/router';
1616
import { Location } from '@angular/common';
1717

18-
describe('TabBar', () => {
18+
fdescribe('TabBar', () => {
1919
configureTestSuite();
2020
beforeEach(async(() => {
2121

@@ -26,7 +26,8 @@ describe('TabBar', () => {
2626
];
2727

2828
TestBed.configureTestingModule({
29-
declarations: [TabBarTestComponent, BottomTabBarTestComponent, TemplatedTabBarTestComponent, TabBarRoutingTestComponent],
29+
declarations: [TabBarTestComponent, BottomTabBarTestComponent, TemplatedTabBarTestComponent, TabBarRoutingTestComponent,
30+
TabBarTabsOnlyModeTestComponent],
3031
imports: [IgxBottomNavModule, BottomNavRoutingViewComponentsModule, RouterTestingModule.withRoutes(testRoutes)]
3132
})
3233
.compileComponents();
@@ -227,6 +228,33 @@ describe('TabBar', () => {
227228

228229
});
229230

231+
describe('Tabs-only Mode Tests', () => {
232+
configureTestSuite();
233+
234+
let fixture;
235+
let bottomNav;
236+
let theTabs;
237+
238+
beforeEach(async(() => {
239+
fixture = TestBed.createComponent(TabBarTabsOnlyModeTestComponent);
240+
bottomNav = fixture.componentInstance.bottomNavComp;
241+
fixture.detectChanges();
242+
theTabs = bottomNav.contentTabs.toArray();
243+
}));
244+
245+
it('should retain the correct initial selection status', () => {
246+
expect(theTabs[0].isSelected).toBe(false);
247+
expect(theTabs[0].elementRef().nativeElement.classList.contains('igx-bottom-nav__menu-item')).toBe(true);
248+
249+
expect(theTabs[1].isSelected).toBe(true);
250+
expect(theTabs[1].elementRef().nativeElement.classList.contains('igx-bottom-nav__menu-item--selected')).toBe(true);
251+
252+
expect(theTabs[2].isSelected).toBe(false);
253+
expect(theTabs[2].elementRef().nativeElement.classList.contains('igx-bottom-nav__menu-item')).toBe(true);
254+
});
255+
256+
});
257+
230258
});
231259

232260
@Component({
@@ -356,3 +384,22 @@ class TabBarRoutingTestComponent {
356384
@ViewChild(IgxBottomNavComponent)
357385
public bottomNavComp: IgxBottomNavComponent;
358386
}
387+
388+
@Component({
389+
template: `
390+
<div #wrapperDiv>
391+
<igx-bottom-nav>
392+
<igx-tab label="Tab 1">
393+
</igx-tab>
394+
<igx-tab label="Tab 2" [isSelected]="true">
395+
</igx-tab>
396+
<igx-tab label="Tab 3">
397+
</igx-tab>
398+
</igx-bottom-nav>
399+
</div>
400+
`
401+
})
402+
class TabBarTabsOnlyModeTestComponent {
403+
@ViewChild(IgxBottomNavComponent)
404+
public bottomNavComp: IgxBottomNavComponent;
405+
}

Diff for: projects/igniteui-angular/src/lib/tabbar/tabbar.component.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,14 @@ export class IgxBottomNavComponent implements AfterViewInit {
218218
}
219219
});
220220
} else {
221-
this.selectedIndex = args.panel.index;
222-
this.panels.forEach((p) => {
223-
if (p.index !== this.selectedIndex) {
224-
this._deselectPanel(p);
225-
}
226-
});
221+
if (args.panel) {
222+
this.selectedIndex = args.panel.index;
223+
this.panels.forEach((p) => {
224+
if (p.index !== this.selectedIndex) {
225+
this._deselectPanel(p);
226+
}
227+
});
228+
}
227229
}
228230
}
229231

Diff for: projects/igniteui-angular/src/lib/tabs/tab-item.component.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,11 @@ export class IgxTabItemComponent implements IgxTabItemBase {
293293
}
294294

295295
private transformIndicatorAnimation(element: HTMLElement): void {
296-
this._tabs.selectedIndicator.nativeElement.style.visibility = `visible`;
297-
this._tabs.selectedIndicator.nativeElement.style.width = `${element.offsetWidth}px`;
298-
this._tabs.selectedIndicator.nativeElement.style.transform = `translate(${element.offsetLeft}px)`;
296+
if (this._tabs && this._tabs.selectedIndicator) {
297+
this._tabs.selectedIndicator.nativeElement.style.visibility = `visible`;
298+
this._tabs.selectedIndicator.nativeElement.style.width = `${element.offsetWidth}px`;
299+
this._tabs.selectedIndicator.nativeElement.style.transform = `translate(${element.offsetLeft}px)`;
300+
}
299301
}
300302

301303
private onKeyDown(isLeftArrow: boolean, index = null): void {

Diff for: projects/igniteui-angular/src/lib/tabs/tabs.component.spec.ts

+49-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { TabsRoutingViewComponentsModule,
1919
TabsRoutingView2Component,
2020
TabsRoutingView3Component } from './tabs-routing-view-components.spec';
2121

22-
describe('IgxTabs', () => {
22+
fdescribe('IgxTabs', () => {
2323
configureTestSuite();
2424
beforeEach(async(() => {
2525

@@ -31,7 +31,8 @@ describe('IgxTabs', () => {
3131

3232
TestBed.configureTestingModule({
3333
declarations: [TabsTestComponent, TabsTest2Component, TemplatedTabsTestComponent,
34-
TabsTestSelectedTabComponent, TabsTestCustomStylesComponent, TabsTestBug4420Component, TabsRoutingTestComponent],
34+
TabsTestSelectedTabComponent, TabsTestCustomStylesComponent, TabsTestBug4420Component, TabsRoutingTestComponent,
35+
TabsTabsOnlyModeTestComponent],
3536
imports: [IgxTabsModule, IgxButtonModule, IgxDropDownModule, IgxToggleModule, BrowserAnimationsModule,
3637
TabsRoutingViewComponentsModule, RouterTestingModule.withRoutes(testRoutes)]
3738
})
@@ -478,6 +479,33 @@ describe('IgxTabs', () => {
478479

479480
});
480481

482+
describe('Tabs-only Mode Tests', () => {
483+
configureTestSuite();
484+
485+
let fixture;
486+
let tabsComp;
487+
let theTabs;
488+
489+
beforeEach(async(() => {
490+
fixture = TestBed.createComponent(TabsTabsOnlyModeTestComponent);
491+
tabsComp = fixture.componentInstance.tabs;
492+
fixture.detectChanges();
493+
theTabs = tabsComp.tabs.toArray();
494+
}));
495+
496+
it('should retain the correct initial selection status', () => {
497+
expect(theTabs[0].isSelected).toBe(false);
498+
expect(theTabs[0].nativeTabItem.nativeElement.classList.contains('igx-tabs__header-menu-item')).toBe(true);
499+
500+
expect(theTabs[1].isSelected).toBe(true);
501+
expect(theTabs[1].nativeTabItem.nativeElement.classList.contains('igx-tabs__header-menu-item--selected')).toBe(true);
502+
503+
expect(theTabs[2].isSelected).toBe(false);
504+
expect(theTabs[2].nativeTabItem.nativeElement.classList.contains('igx-tabs__header-menu-item')).toBe(true);
505+
});
506+
507+
});
508+
481509
});
482510

483511
@Component({
@@ -687,3 +715,22 @@ class TabsRoutingTestComponent {
687715
@ViewChild(IgxTabsComponent)
688716
public tabs: IgxTabsComponent;
689717
}
718+
719+
@Component({
720+
template: `
721+
<div #wrapperDiv>
722+
<igx-tabs>
723+
<igx-tab-item label="Tab 1">
724+
</igx-tab-item>
725+
<igx-tab-item label="Tab 2" [isSelected]="true">
726+
</igx-tab-item>
727+
<igx-tab-item label="Tab 3">
728+
</igx-tab-item>
729+
</igx-tabs>
730+
</div>
731+
`
732+
})
733+
class TabsTabsOnlyModeTestComponent {
734+
@ViewChild(IgxTabsComponent)
735+
public tabs: IgxTabsComponent;
736+
}

Diff for: projects/igniteui-angular/src/lib/tabs/tabs.component.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,23 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
252252
this.selectedIndex = args.tab.index;
253253
} else {
254254
const prevSelectedIndex = this.selectedIndex;
255-
if (prevSelectedIndex !== -1 && this.groups.toArray()[prevSelectedIndex] !== undefined) {
255+
if (prevSelectedIndex !== -1 && this.groups && this.groups.toArray()[prevSelectedIndex] !== undefined) {
256256
this.onTabItemDeselected.emit(
257257
{
258258
tab: this.groups.toArray()[prevSelectedIndex].relatedTab,
259259
group: this.groups.toArray()[prevSelectedIndex]
260260
});
261261
}
262-
this.selectedIndex = args.group.index;
263-
this.groups.forEach((p) => {
264-
if (p.index !== this.selectedIndex) {
265-
this.deselectGroup(p);
266-
}
267-
});
262+
if (args.group) {
263+
this.selectedIndex = args.group.index;
264+
}
265+
if (this.groups) {
266+
this.groups.forEach((p) => {
267+
if (p.index !== this.selectedIndex) {
268+
this.deselectGroup(p);
269+
}
270+
});
271+
}
268272
}
269273
}
270274

0 commit comments

Comments
 (0)