redoc/lib/shared/components/Tabs/tabs.spec.ts

150 lines
4.1 KiB
TypeScript
Raw Normal View History

2015-12-19 16:31:34 +03:00
'use strict';
2016-06-12 20:44:34 +03:00
import { getChildDebugElement, getChildDebugElementAll } from '../../../../tests/helpers';
import { Component } from '@angular/core';
2015-12-19 16:31:34 +03:00
import {
inject,
2016-07-01 15:53:16 +03:00
TestComponentBuilder
2016-05-09 22:55:16 +03:00
} from '@angular/core/testing';
2016-06-12 20:44:34 +03:00
import {Tabs, Tab} from '../index';
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
describe('Common components', () => {
describe('Tabs Component', () => {
let builder;
let component;
let childDebugEls;
2016-02-03 17:47:20 +03:00
let debugEl;
2015-12-19 20:36:28 +03:00
let fixture;
2016-02-03 17:47:20 +03:00
let hostComponent;
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
beforeEach(inject([TestComponentBuilder], (tcb) => {
builder = tcb;
2016-07-01 15:53:16 +03:00
fixture = builder.createSync(TestApp);
hostComponent = fixture.debugElement.componentInstance;
debugEl = getChildDebugElement(fixture.debugElement, 'tabs');
childDebugEls = getChildDebugElementAll(debugEl, 'tab');
component = debugEl.componentInstance;
2015-12-19 20:36:28 +03:00
}));
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
it('should init component', () => {
expect(component).not.toBeNull();
});
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
it('should handle inner tabs', () => {
component.tabs.should.have.lengthOf(2);
childDebugEls.should.have.lengthOf(2);
});
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
it('should activate first tab by default', () => {
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let [tab1, tab2] = tabs;
2015-12-19 16:31:34 +03:00
tab1.active.should.be.true();
tab2.active.should.be.false();
2015-12-19 20:36:28 +03:00
});
2015-12-19 16:31:34 +03:00
2015-12-19 20:36:28 +03:00
it('should change active tab on click', () => {
fixture.detectChanges();
2016-02-03 17:47:20 +03:00
//let headerEls = nativeElement.querySelectorAll('li');
2015-12-19 20:36:28 +03:00
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let [tab1, tab2] = tabs;
2015-12-19 16:31:34 +03:00
2016-02-03 17:47:20 +03:00
let tabsInst = debugEl.componentInstance;
tabsInst.selectTab(tab2);
tab1.active.should.be.false();
tab2.active.should.be.true();
});
it('should change tab by title and not emit Event', (done) => {
fixture.detectChanges();
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let [tab1, tab2] = tabs;
let tab2Title = 'Tab2';
let tabsInst = debugEl.componentInstance;
tabsInst.selectyByTitle(tab2Title);
tab1.active.should.be.false();
tab2.active.should.be.true();
setTimeout(() => {
hostComponent.eventLog.should.be.deepEqual([]);
done();
});
});
it('should emit event on tab Change', (done) => {
fixture.detectChanges();
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let tab2 = tabs[1];
let tabsInst = debugEl.componentInstance;
tabsInst.selectTab(tab2, true);
setTimeout(() => {
hostComponent.eventLog.should.be.deepEqual(['Tab2']);
done();
});
});
it('should emit event on tab change by Title with notify true', (done) => {
fixture.detectChanges();
let tab2Title = 'Tab2';
let tabsInst = debugEl.componentInstance;
tabsInst.selectyByTitle(tab2Title, true);
setTimeout(() => {
hostComponent.eventLog.should.be.deepEqual(['Tab2']);
done();
});
});
it('should not emit event on tab change with notify false', (done) => {
fixture.detectChanges();
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let tab2 = tabs[1];
let tabsInst = debugEl.componentInstance;
tabsInst.selectTab(tab2, false);
setTimeout(() => {
hostComponent.eventLog.should.be.deepEqual([]);
done();
});
});
it('should leave current tab active if selectyByTitle non existing title', () => {
fixture.detectChanges();
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let [tab1, tab2] = tabs;
let tabsInst = debugEl.componentInstance;
tabsInst.selectyByTitle('badTitle');
tab1.active.should.be.true();
tab2.active.should.be.false();
2015-12-19 20:36:28 +03:00
});
2015-12-19 16:31:34 +03:00
});
});
/** Test component that contains an ApiInfo. */
@Component({
selector: 'test-app',
2015-12-19 16:31:34 +03:00
directives: [Tabs, Tab],
template:
2016-02-03 17:47:20 +03:00
`<tabs (change)="onEvent($event)">
2015-12-19 16:31:34 +03:00
<tab tabTitle="Tab1" tabStatus="test">Test</tab>
<tab tabTitle="Tab2" tabStatus="test">Test</tab>
</tabs>`
})
class TestApp {
2016-06-12 20:44:34 +03:00
eventLog: any;
2016-02-03 17:47:20 +03:00
constructor() {
this.eventLog = [];
}
onEvent(event) {
this.eventLog.push(event);
}
2015-12-19 16:31:34 +03:00
}