Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabset): prevent select call if no active tab found #1444

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/framework/theme/components/tabset/tabset.component.spec.ts
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();
}));
});
10 changes: 6 additions & 4 deletions src/framework/theme/components/tabset/tabset.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { map, delay } from 'rxjs/operators';
import { map, delay, filter } from 'rxjs/operators';
import {
Component,
Input,
Expand Down Expand Up @@ -301,11 +301,13 @@ export class NbTabsetComponent implements AfterContentInit {
this.tabs.find((tab) => this.routeParam ? tab.route === params[this.routeParam] : tab.active),
),
delay(0),
map((tab: NbTabComponent) => tab || this.tabs.first),
filter((tab: NbTabComponent) => !!tab),
)
.subscribe((activeTab) => {
this.selectTab(activeTab || this.tabs.first);
.subscribe((tabToSelect: NbTabComponent) => {
this.selectTab(tabToSelect);
this.changeDetectorRef.markForCheck();
});
});
}

// TODO: navigate to routeParam
Expand Down