forked from angular/components
/
tab-header.spec.ts
782 lines (615 loc) · 27.2 KB
/
tab-header.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import {Dir, Direction} from '@angular/cdk/bidi';
import {END, ENTER, HOME, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
import {MutationObserverFactory, ObserversModule} from '@angular/cdk/observers';
import {SharedResizeObserver} from '@angular/cdk/observers/private';
import {PortalModule} from '@angular/cdk/portal';
import {ScrollingModule, ViewportRuler} from '@angular/cdk/scrolling';
import {
createKeyboardEvent,
createMouseEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
} from '@angular/cdk/testing/private';
import {CommonModule} from '@angular/common';
import {Component, ViewChild} from '@angular/core';
import {
ComponentFixture,
TestBed,
discardPeriodicTasks,
fakeAsync,
flushMicrotasks,
tick,
waitForAsync,
} from '@angular/core/testing';
import {MatRippleModule} from '@angular/material/core';
import {By} from '@angular/platform-browser';
import {Subject} from 'rxjs';
import {MatTabHeader} from './tab-header';
import {MatTabLabelWrapper} from './tab-label-wrapper';
describe('MDC-based MatTabHeader', () => {
let fixture: ComponentFixture<SimpleTabHeaderApp>;
let appComponent: SimpleTabHeaderApp;
let resizeEvents: Subject<ResizeObserverEntry[]>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
CommonModule,
PortalModule,
MatRippleModule,
ScrollingModule,
ObserversModule,
MatTabHeader,
MatTabLabelWrapper,
SimpleTabHeaderApp,
],
providers: [ViewportRuler],
});
TestBed.compileComponents();
resizeEvents = new Subject();
spyOn(TestBed.inject(SharedResizeObserver), 'observe').and.returnValue(resizeEvents);
}));
describe('focusing', () => {
let tabListContainer: HTMLElement;
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.detectChanges();
appComponent = fixture.componentInstance;
tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement;
});
it('should initialize to the selected index', () => {
// Recreate the fixture so we can test that it works with a non-default selected index
fixture.destroy();
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.componentInstance.selectedIndex = 1;
fixture.detectChanges();
appComponent = fixture.componentInstance;
tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement;
expect(appComponent.tabHeader.focusIndex).toBe(1);
});
it('should send focus change event', () => {
appComponent.tabHeader.focusIndex = 2;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(2);
});
it('should be able to focus a disabled tab', () => {
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
appComponent.tabHeader.focusIndex = appComponent.disabledTabIndex;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(appComponent.disabledTabIndex);
});
it('should move focus right including over disabled tabs', () => {
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
expect(appComponent.disabledTabIndex).toBe(1);
dispatchKeyboardEvent(tabListContainer, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(1);
dispatchKeyboardEvent(tabListContainer, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(2);
});
it('should move focus left including over disabled tabs', () => {
appComponent.tabHeader.focusIndex = 3;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(3);
// Move focus left to index 3
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(2);
expect(appComponent.disabledTabIndex).toBe(1);
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(1);
});
it('should support key down events to move and select focus', () => {
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
// Move focus right to 1
dispatchKeyboardEvent(tabListContainer, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(1);
// Try to select 1. Should not work since it's disabled.
expect(appComponent.selectedIndex).toBe(0);
const firstEnterEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
fixture.detectChanges();
expect(appComponent.selectedIndex).toBe(0);
expect(firstEnterEvent.defaultPrevented).toBe(false);
// Move focus right to 2
dispatchKeyboardEvent(tabListContainer, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(2);
// Select 2 which is enabled.
expect(appComponent.selectedIndex).toBe(0);
const secondEnterEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
fixture.detectChanges();
expect(appComponent.selectedIndex).toBe(2);
expect(secondEnterEvent.defaultPrevented).toBe(true);
// Move focus left to 1
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(1);
// Move again to 0
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
// Select the focused 0 using space.
expect(appComponent.selectedIndex).toBe(2);
const spaceEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', SPACE);
fixture.detectChanges();
expect(appComponent.selectedIndex).toBe(0);
expect(spaceEvent.defaultPrevented).toBe(true);
});
it('should not prevent the default space/enter action if the current is selected', () => {
appComponent.tabHeader.focusIndex = appComponent.tabHeader.selectedIndex = 0;
fixture.detectChanges();
const spaceEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', SPACE);
fixture.detectChanges();
expect(spaceEvent.defaultPrevented).toBe(false);
const enterEvent = dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
fixture.detectChanges();
expect(enterEvent.defaultPrevented).toBe(false);
});
it('should move focus to the first tab when pressing HOME', () => {
appComponent.tabHeader.focusIndex = 3;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(3);
const event = dispatchKeyboardEvent(tabListContainer, 'keydown', HOME);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
expect(event.defaultPrevented).toBe(true);
});
it('should focus disabled items when moving focus using HOME', () => {
appComponent.tabHeader.focusIndex = 3;
appComponent.tabs[0].disabled = true;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(3);
dispatchKeyboardEvent(tabListContainer, 'keydown', HOME);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
});
it('should move focus to the last tab when pressing END', () => {
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
const event = dispatchKeyboardEvent(tabListContainer, 'keydown', END);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(3);
expect(event.defaultPrevented).toBe(true);
});
it('should focus disabled items when moving focus using END', () => {
appComponent.tabHeader.focusIndex = 0;
appComponent.tabs[3].disabled = true;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
dispatchKeyboardEvent(tabListContainer, 'keydown', END);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(3);
});
it('should not do anything if a modifier key is pressed', () => {
const rightArrowEvent = createKeyboardEvent('keydown', RIGHT_ARROW, undefined, {shift: true});
const enterEvent = createKeyboardEvent('keydown', ENTER, undefined, {shift: true});
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
dispatchEvent(tabListContainer, rightArrowEvent);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
expect(rightArrowEvent.defaultPrevented).toBe(false);
expect(appComponent.selectedIndex).toBe(0);
dispatchEvent(tabListContainer, enterEvent);
fixture.detectChanges();
expect(appComponent.selectedIndex).toBe(0);
expect(enterEvent.defaultPrevented).toBe(false);
});
});
describe('pagination', () => {
describe('ltr', () => {
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
appComponent = fixture.componentInstance;
appComponent.dir = 'ltr';
fixture.detectChanges();
});
it('should show width when tab list width exceeds container', () => {
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(false);
// Add enough tabs that it will obviously exceed the width
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(true);
});
it('should scroll to show the focused tab label', () => {
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
// Focus on the last tab, expect this to be the maximum scroll distance.
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
fixture.detectChanges();
const {offsetLeft, offsetWidth} = appComponent.getSelectedLabel(
appComponent.tabHeader.focusIndex,
);
const viewLength = appComponent.getViewLength();
expect(appComponent.tabHeader.scrollDistance).toBe(offsetLeft + offsetWidth - viewLength);
// Focus on the first tab, expect this to be the maximum scroll distance.
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
});
it('should show ripples for pagination buttons', () => {
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(true);
const buttonAfter = fixture.debugElement.query(
By.css('.mat-mdc-tab-header-pagination-after'),
);
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripple to show up initially.')
.toBe(0);
dispatchFakeEvent(buttonAfter.nativeElement, 'mousedown');
dispatchFakeEvent(buttonAfter.nativeElement, 'mouseup');
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected one ripple to show up after mousedown')
.toBe(1);
});
it('should allow disabling ripples for pagination buttons', () => {
appComponent.addTabsForScrolling();
appComponent.disableRipple = true;
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(true);
const buttonAfter = fixture.debugElement.query(
By.css('.mat-mdc-tab-header-pagination-after'),
);
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripple to show up initially.')
.toBe(0);
dispatchFakeEvent(buttonAfter.nativeElement, 'mousedown');
dispatchFakeEvent(buttonAfter.nativeElement, 'mouseup');
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripple to show up after mousedown')
.toBe(0);
});
it('should update the scroll distance if a tab is removed and no tabs are selected', fakeAsync(() => {
appComponent.selectedIndex = 0;
appComponent.addTabsForScrolling();
fixture.detectChanges();
// Focus the last tab so the header scrolls to the end.
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
fixture.detectChanges();
const {offsetLeft, offsetWidth} = appComponent.getSelectedLabel(
appComponent.tabHeader.focusIndex,
);
const viewLength = appComponent.getViewLength();
expect(appComponent.tabHeader.scrollDistance).toBe(offsetLeft + offsetWidth - viewLength);
// Remove the first two tabs which includes the selected tab.
appComponent.tabs = appComponent.tabs.slice(2);
fixture.detectChanges();
tick();
expect(appComponent.tabHeader.scrollDistance).toBe(
appComponent.tabHeader._getMaxScrollDistance(),
);
}));
});
describe('rtl', () => {
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
appComponent = fixture.componentInstance;
appComponent.dir = 'rtl';
fixture.detectChanges();
});
it('should scroll to show the focused tab label', () => {
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
// Focus on the last tab, expect this to be the maximum scroll distance.
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
fixture.detectChanges();
const {offsetLeft} = appComponent.getSelectedLabel(appComponent.tabHeader.focusIndex);
expect(offsetLeft).toBe(0);
// Focus on the first tab, expect this to be the maximum scroll distance.
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
});
});
describe('scrolling when holding paginator', () => {
let nextButton: HTMLElement;
let prevButton: HTMLElement;
let header: MatTabHeader;
let headerElement: HTMLElement;
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.componentInstance.disableRipple = true;
fixture.detectChanges();
fixture.componentInstance.addTabsForScrolling(50);
fixture.detectChanges();
nextButton = fixture.nativeElement.querySelector('.mat-mdc-tab-header-pagination-after');
prevButton = fixture.nativeElement.querySelector('.mat-mdc-tab-header-pagination-before');
header = fixture.componentInstance.tabHeader;
headerElement = fixture.nativeElement.querySelector('.mat-mdc-tab-header');
});
it('should scroll towards the end while holding down the next button using a mouse', fakeAsync(() => {
assertNextButtonScrolling('mousedown', 'click');
}));
it('should scroll towards the start while holding down the prev button using a mouse', fakeAsync(() => {
assertPrevButtonScrolling('mousedown', 'click');
}));
it('should scroll towards the end while holding down the next button using touch', fakeAsync(() => {
assertNextButtonScrolling('touchstart', 'touchend');
}));
it('should scroll towards the start while holding down the prev button using touch', fakeAsync(() => {
assertPrevButtonScrolling('touchstart', 'touchend');
}));
it('should not scroll if the sequence is interrupted quickly', fakeAsync(() => {
expect(header.scrollDistance).withContext('Expected to start off not scrolled.').toBe(0);
dispatchFakeEvent(nextButton, 'mousedown');
fixture.detectChanges();
tick(100);
dispatchFakeEvent(headerElement, 'mouseleave');
fixture.detectChanges();
tick(3000);
expect(header.scrollDistance)
.withContext('Expected not to have scrolled after a while.')
.toBe(0);
}));
it('should clear the timeouts on destroy', fakeAsync(() => {
dispatchFakeEvent(nextButton, 'mousedown');
fixture.detectChanges();
fixture.destroy();
// No need to assert. If fakeAsync doesn't throw, it means that the timers were cleared.
}));
it('should clear the timeouts on click', fakeAsync(() => {
dispatchFakeEvent(nextButton, 'mousedown');
fixture.detectChanges();
dispatchFakeEvent(nextButton, 'click');
fixture.detectChanges();
// No need to assert. If fakeAsync doesn't throw, it means that the timers were cleared.
}));
it('should clear the timeouts on touchend', fakeAsync(() => {
dispatchFakeEvent(nextButton, 'touchstart');
fixture.detectChanges();
dispatchFakeEvent(nextButton, 'touchend');
fixture.detectChanges();
// No need to assert. If fakeAsync doesn't throw, it means that the timers were cleared.
}));
it('should clear the timeouts when reaching the end', fakeAsync(() => {
dispatchFakeEvent(nextButton, 'mousedown');
fixture.detectChanges();
// Simulate a very long timeout.
tick(60000);
// No need to assert. If fakeAsync doesn't throw, it means that the timers were cleared.
}));
it('should clear the timeouts when reaching the start', fakeAsync(() => {
header.scrollDistance = Infinity;
fixture.detectChanges();
dispatchFakeEvent(prevButton, 'mousedown');
fixture.detectChanges();
// Simulate a very long timeout.
tick(60000);
// No need to assert. If fakeAsync doesn't throw, it means that the timers were cleared.
}));
it('should stop scrolling if the pointer leaves the header', fakeAsync(() => {
expect(header.scrollDistance).withContext('Expected to start off not scrolled.').toBe(0);
dispatchFakeEvent(nextButton, 'mousedown');
fixture.detectChanges();
tick(300);
expect(header.scrollDistance)
.withContext('Expected not to scroll after short amount of time.')
.toBe(0);
tick(1000);
expect(header.scrollDistance)
.withContext('Expected to scroll after some time.')
.toBeGreaterThan(0);
let previousDistance = header.scrollDistance;
dispatchFakeEvent(headerElement, 'mouseleave');
fixture.detectChanges();
tick(100);
expect(header.scrollDistance).toBe(previousDistance);
}));
it('should not scroll when pressing the right mouse button', fakeAsync(() => {
expect(header.scrollDistance).withContext('Expected to start off not scrolled.').toBe(0);
dispatchEvent(
nextButton,
createMouseEvent('mousedown', undefined, undefined, undefined, undefined, 2),
);
fixture.detectChanges();
tick(3000);
expect(header.scrollDistance)
.withContext('Expected not to have scrolled after a while.')
.toBe(0);
}));
/**
* Asserts that auto scrolling using the next button works.
* @param startEventName Name of the event that is supposed to start the scrolling.
* @param endEventName Name of the event that is supposed to end the scrolling.
*/
function assertNextButtonScrolling(startEventName: string, endEventName: string) {
expect(header.scrollDistance).withContext('Expected to start off not scrolled.').toBe(0);
dispatchFakeEvent(nextButton, startEventName);
fixture.detectChanges();
tick(300);
expect(header.scrollDistance)
.withContext('Expected not to scroll after short amount of time.')
.toBe(0);
tick(1000);
expect(header.scrollDistance)
.withContext('Expected to scroll after some time.')
.toBeGreaterThan(0);
let previousDistance = header.scrollDistance;
tick(100);
expect(header.scrollDistance)
.withContext('Expected to scroll again after some more time.')
.toBeGreaterThan(previousDistance);
dispatchFakeEvent(nextButton, endEventName);
}
/**
* Asserts that auto scrolling using the previous button works.
* @param startEventName Name of the event that is supposed to start the scrolling.
* @param endEventName Name of the event that is supposed to end the scrolling.
*/
function assertPrevButtonScrolling(startEventName: string, endEventName: string) {
header.scrollDistance = Infinity;
fixture.detectChanges();
let currentScroll = header.scrollDistance;
expect(currentScroll).withContext('Expected to start off scrolled.').toBeGreaterThan(0);
dispatchFakeEvent(prevButton, startEventName);
fixture.detectChanges();
tick(300);
expect(header.scrollDistance)
.withContext('Expected not to scroll after short amount of time.')
.toBe(currentScroll);
tick(1000);
expect(header.scrollDistance)
.withContext('Expected to scroll after some time.')
.toBeLessThan(currentScroll);
currentScroll = header.scrollDistance;
tick(100);
expect(header.scrollDistance)
.withContext('Expected to scroll again after some more time.')
.toBeLessThan(currentScroll);
dispatchFakeEvent(nextButton, endEventName);
}
});
describe('disabling pagination', () => {
it('should not show the pagination controls if pagination is disabled', () => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
appComponent = fixture.componentInstance;
appComponent.disablePagination = true;
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(false);
// Add enough tabs that it will obviously exceed the width
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader._showPaginationControls).toBe(false);
});
it('should not change the scroll position if pagination is disabled', () => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
appComponent = fixture.componentInstance;
appComponent.disablePagination = true;
fixture.detectChanges();
appComponent.addTabsForScrolling();
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.scrollDistance).toBe(0);
});
});
it('should re-align the ink bar when the direction changes', fakeAsync(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.detectChanges();
const inkBar = fixture.componentInstance.tabHeader._inkBar;
spyOn(inkBar, 'alignToElement');
fixture.detectChanges();
fixture.componentInstance.dir = 'rtl';
fixture.detectChanges();
tick();
expect(inkBar.alignToElement).toHaveBeenCalled();
}));
it('should re-align the ink bar when the header is resized', fakeAsync(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.detectChanges();
const inkBar = fixture.componentInstance.tabHeader._inkBar;
spyOn(inkBar, 'alignToElement');
resizeEvents.next([]);
fixture.detectChanges();
tick(32);
expect(inkBar.alignToElement).toHaveBeenCalled();
discardPeriodicTasks();
}));
it('should update arrows when the header is resized', fakeAsync(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
const header = fixture.componentInstance.tabHeader;
spyOn(header, '_checkPaginationEnabled');
resizeEvents.next([]);
fixture.detectChanges();
flushMicrotasks();
expect(header._checkPaginationEnabled).toHaveBeenCalled();
discardPeriodicTasks();
}));
it('should update the pagination state if the content of the labels changes', () => {
const mutationCallbacks: Function[] = [];
spyOn(TestBed.inject(MutationObserverFactory), 'create').and.callFake(
(callback: Function) => {
mutationCallbacks.push(callback);
return {observe: () => {}, disconnect: () => {}} as any;
},
);
fixture = TestBed.createComponent(SimpleTabHeaderApp);
fixture.detectChanges();
const tabHeaderElement: HTMLElement =
fixture.nativeElement.querySelector('.mat-mdc-tab-header');
const labels = Array.from<HTMLElement>(
fixture.nativeElement.querySelectorAll('.label-content'),
);
const extraText = new Array(100).fill('w').join();
const enabledClass = 'mat-mdc-tab-header-pagination-controls-enabled';
expect(tabHeaderElement.classList).not.toContain(enabledClass);
labels.forEach(label => {
label.style.width = '';
label.textContent += extraText;
});
mutationCallbacks.forEach(callback => callback());
fixture.detectChanges();
expect(tabHeaderElement.classList).toContain(enabledClass);
});
});
});
interface Tab {
label: string;
disabled?: boolean;
}
@Component({
template: `
<div [dir]="dir">
<mat-tab-header [selectedIndex]="selectedIndex" [disableRipple]="disableRipple"
(indexFocused)="focusedIndex = $event"
(selectFocusedIndex)="selectedIndex = $event"
[disablePagination]="disablePagination">
@for (tab of tabs; track tab; let i = $index) {
<div matTabLabelWrapper class="label-content" style="min-width: 30px; width: 30px"
[disabled]="!!tab.disabled"
(click)="selectedIndex = i">{{tab.label}}</div>
}
</mat-tab-header>
</div>
`,
styles: `
:host {
width: 130px;
}
`,
standalone: true,
imports: [Dir, MatTabHeader, MatTabLabelWrapper],
})
class SimpleTabHeaderApp {
disableRipple: boolean = false;
selectedIndex: number = 0;
focusedIndex: number;
disabledTabIndex = 1;
tabs: Tab[] = [{label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}];
dir: Direction = 'ltr';
disablePagination: boolean;
@ViewChild(MatTabHeader, {static: true}) tabHeader: MatTabHeader;
constructor() {
this.tabs[this.disabledTabIndex].disabled = true;
}
addTabsForScrolling(amount = 4) {
for (let i = 0; i < amount; i++) {
this.tabs.push({label: 'new'});
}
}
getViewLength() {
return this.tabHeader._tabListContainer.nativeElement.offsetWidth;
}
getSelectedLabel(index: number) {
return this.tabHeader._items.toArray()[this.tabHeader.focusIndex].elementRef.nativeElement;
}
}