-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tabset): prevent select call if no active tab found (#1444)
- Loading branch information
Showing
2 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/framework/theme/components/tabset/tabset.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, QueryList, ViewChild, ViewChildren } from '@angular/core'; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { ActivatedRoute, Params } from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { NbTabComponent, NbTabsetComponent, NbTabsetModule } from '@nebular/theme'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import createSpy = jasmine.createSpy; | ||
|
||
@Component({ | ||
template: ` | ||
<nb-tabset routeParam="tab"> | ||
<nb-tab *ngIf="showTabs" tabTitle="1" route="1">1</nb-tab> | ||
<nb-tab *ngIf="showTabs" tabTitle="2" route="2">2</nb-tab> | ||
<nb-tab *ngIf="showTabs" tabTitle="3" route="3" disabled>3</nb-tab> | ||
</nb-tabset> | ||
`, | ||
}) | ||
export class TabsetTestComponent { | ||
showTabs = true; | ||
|
||
@ViewChild(NbTabsetComponent) tabsetComponent: NbTabsetComponent; | ||
@ViewChildren(NbTabComponent) tabComponents: QueryList<NbTabComponent>; | ||
|
||
getDisabledTab(): NbTabComponent { | ||
return this.tabComponents.toArray()[2]; | ||
} | ||
} | ||
|
||
export class ActivatedRouteStub { | ||
private subject = new BehaviorSubject<Params>(null); | ||
readonly params = this.subject.asObservable(); | ||
|
||
constructor(params: Params = {}) { | ||
this.subject.next(params); | ||
} | ||
|
||
setParams(params?: Params) { | ||
this.subject.next(params); | ||
}; | ||
} | ||
|
||
describe('NbTabsetComponent', () => { | ||
let fixture: ComponentFixture<TabsetTestComponent>; | ||
let testComponent: TabsetTestComponent; | ||
let tabsetComponent: NbTabsetComponent; | ||
let activatedRouteStub: ActivatedRouteStub; | ||
|
||
beforeEach(fakeAsync(() => { | ||
activatedRouteStub = new ActivatedRouteStub(); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [ TabsetTestComponent ], | ||
imports: [ | ||
CommonModule, | ||
RouterTestingModule.withRoutes([]), | ||
NbTabsetModule, | ||
], | ||
providers: [{ provide: ActivatedRoute, useValue: activatedRouteStub }], | ||
}); | ||
|
||
fixture = TestBed.createComponent(TabsetTestComponent); | ||
testComponent = fixture.componentInstance; | ||
|
||
fixture.detectChanges(); | ||
tick(); | ||
|
||
tabsetComponent = testComponent.tabsetComponent; | ||
})); | ||
|
||
it('should mark tab as active if selected in route param', fakeAsync(() => { | ||
const selectTabSpy = spyOn(tabsetComponent, 'selectTab'); | ||
const tabToSelect: NbTabComponent = testComponent.tabComponents.first; | ||
|
||
activatedRouteStub.setParams({ tab: tabToSelect.route }); | ||
fixture.detectChanges(); | ||
tick(); | ||
|
||
expect(selectTabSpy).toHaveBeenCalledTimes(1); | ||
expect(selectTabSpy).toHaveBeenCalledWith(tabToSelect); | ||
expect(tabToSelect.active).toEqual(true); | ||
})); | ||
|
||
it('should not mark disabled tab as active if selected in route param', fakeAsync(() => { | ||
const changeTabSpy = createSpy('changeTabSpy'); | ||
const disabledTab: NbTabComponent = testComponent.getDisabledTab(); | ||
|
||
activatedRouteStub.setParams({ tab: disabledTab.route }); | ||
fixture.detectChanges(); | ||
tick(); | ||
|
||
expect(changeTabSpy).not.toHaveBeenCalled(); | ||
expect(disabledTab.active).toEqual(false); | ||
})); | ||
|
||
it(`should not call 'selectTab' if no tabs found`, fakeAsync(() => { | ||
const selectTabSpy = spyOn(tabsetComponent, 'selectTab'); | ||
|
||
testComponent.showTabs = false; | ||
fixture.detectChanges(); | ||
|
||
activatedRouteStub.setParams({ tab: 1 }); | ||
fixture.detectChanges(); | ||
tick(); | ||
|
||
expect(selectTabSpy).not.toHaveBeenCalled(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters