-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
tab-nav-bar.spec.ts
206 lines (158 loc) · 6.63 KB
/
tab-nav-bar.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {MdTabsModule} from '../index';
import {MdTabNav} from './tab-nav-bar';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ViewportRuler} from '../../core/overlay/position/viewport-ruler';
import {FakeViewportRuler} from '../../core/overlay/position/fake-viewport-ruler';
import {dispatchFakeEvent, dispatchMouseEvent} from '../../core/testing/dispatch-events';
import {Direction, Directionality} from '../../core/bidi/index';
import {Subject} from 'rxjs/Subject';
describe('MdTabNavBar', () => {
let dir: Direction = 'ltr';
let dirChange = new Subject();
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdTabsModule],
declarations: [
SimpleTabNavBarTestApp,
TabLinkWithNgIf,
],
providers: [
{provide: Directionality, useFactory: () => ({
value: dir,
change: dirChange.asObservable()
})},
{provide: ViewportRuler, useClass: FakeViewportRuler},
]
});
TestBed.compileComponents();
}));
describe('basic behavior', () => {
let fixture: ComponentFixture<SimpleTabNavBarTestApp>;
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabNavBarTestApp);
fixture.detectChanges();
});
it('should change active index on click', () => {
// select the second link
let tabLink = fixture.debugElement.queryAll(By.css('a'))[1];
tabLink.nativeElement.click();
expect(fixture.componentInstance.activeIndex).toBe(1);
// select the third link
tabLink = fixture.debugElement.queryAll(By.css('a'))[2];
tabLink.nativeElement.click();
expect(fixture.componentInstance.activeIndex).toBe(2);
});
it('should add the disabled class if disabled', () => {
const tabLinkElements = fixture.debugElement.queryAll(By.css('a'))
.map(tabLinkDebugEl => tabLinkDebugEl.nativeElement);
expect(tabLinkElements.every(tabLinkEl => !tabLinkEl.classList.contains('mat-tab-disabled')))
.toBe(true, 'Expected every tab link to not have the disabled class initially');
fixture.componentInstance.disabled = true;
fixture.detectChanges();
expect(tabLinkElements.every(tabLinkEl => tabLinkEl.classList.contains('mat-tab-disabled')))
.toBe(true, 'Expected every tab link to have the disabled class if set through binding');
});
it('should update aria-disabled if disabled', () => {
const tabLinkElements = fixture.debugElement.queryAll(By.css('a'))
.map(tabLinkDebugEl => tabLinkDebugEl.nativeElement);
expect(tabLinkElements.every(tabLink => tabLink.getAttribute('aria-disabled') === 'false'))
.toBe(true, 'Expected aria-disabled to be set to "false" by default.');
fixture.componentInstance.disabled = true;
fixture.detectChanges();
expect(tabLinkElements.every(tabLink => tabLink.getAttribute('aria-disabled') === 'true'))
.toBe(true, 'Expected aria-disabled to be set to "true" if link is disabled.');
});
it('should update the tabindex if links are disabled', () => {
const tabLinkElements = fixture.debugElement.queryAll(By.css('a'))
.map(tabLinkDebugEl => tabLinkDebugEl.nativeElement);
expect(tabLinkElements.every(tabLink => tabLink.tabIndex === 0))
.toBe(true, 'Expected element to be keyboard focusable by default');
fixture.componentInstance.disabled = true;
fixture.detectChanges();
expect(tabLinkElements.every(tabLink => tabLink.tabIndex === -1))
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
});
it('should show ripples for tab links', () => {
const tabLink = fixture.debugElement.nativeElement.querySelector('.mat-tab-link');
dispatchMouseEvent(tabLink, 'mousedown');
dispatchMouseEvent(tabLink, 'mouseup');
expect(tabLink.querySelectorAll('.mat-ripple-element').length)
.toBe(1, 'Expected one ripple to show up if user clicks on tab link.');
});
it('should re-align the ink bar when the direction changes', () => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
spyOn(inkBar, 'alignToElement');
dirChange.next();
fixture.detectChanges();
expect(inkBar.alignToElement).toHaveBeenCalled();
});
it('should re-align the ink bar when the tabs list change', () => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
spyOn(inkBar, 'alignToElement');
fixture.componentInstance.tabs = [1, 2, 3, 4];
fixture.detectChanges();
expect(inkBar.alignToElement).toHaveBeenCalled();
});
it('should re-align the ink bar when the tab labels change the width', done => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
const spy = spyOn(inkBar, 'alignToElement').and.callFake(() => {
expect(spy.calls.any()).toBe(true);
done();
});
fixture.componentInstance.label = 'label change';
fixture.detectChanges();
expect(spy.calls.any()).toBe(false);
});
it('should re-align the ink bar when the window is resized', fakeAsync(() => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
spyOn(inkBar, 'alignToElement');
dispatchFakeEvent(window, 'resize');
tick(10);
fixture.detectChanges();
expect(inkBar.alignToElement).toHaveBeenCalled();
}));
});
it('should clean up the ripple event handlers on destroy', () => {
let fixture: ComponentFixture<TabLinkWithNgIf> = TestBed.createComponent(TabLinkWithNgIf);
fixture.detectChanges();
let link = fixture.debugElement.nativeElement.querySelector('.mat-tab-link');
fixture.componentInstance.isDestroyed = true;
fixture.detectChanges();
dispatchMouseEvent(link, 'mousedown');
expect(link.querySelector('.mat-ripple-element'))
.toBeFalsy('Expected no ripple to be created when ripple target is destroyed.');
});
});
@Component({
selector: 'test-app',
template: `
<nav md-tab-nav-bar>
<a md-tab-link
*ngFor="let tab of tabs; let index = index"
[active]="activeIndex === index"
[disabled]="disabled"
(click)="activeIndex = index">
Tab link {{label}}
</a>
</nav>
`
})
class SimpleTabNavBarTestApp {
@ViewChild(MdTabNav) tabNavBar: MdTabNav;
label = '';
disabled: boolean = false;
tabs = [0, 1, 2];
activeIndex = 0;
}
@Component({
template: `
<nav md-tab-nav-bar>
<a md-tab-link *ngIf="!isDestroyed">Link</a>
</nav>
`
})
class TabLinkWithNgIf {
isDestroyed = false;
}