-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathtab-group.spec.ts
921 lines (739 loc) · 29.4 KB
/
tab-group.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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
import {LEFT_ARROW} from '@angular/cdk/keycodes';
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
import {Component, OnInit, QueryList, ViewChild, ViewChildren} from '@angular/core';
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {CommonModule} from '@angular/common';
import {Observable} from 'rxjs';
import {MatTab, MatTabGroup, MatTabHeaderPosition, MatTabsModule} from './index';
describe('MatTabGroup', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [MatTabsModule, CommonModule, NoopAnimationsModule],
declarations: [
SimpleTabsTestApp,
SimpleDynamicTabsTestApp,
BindedTabsTestApp,
AsyncTabsTestApp,
DisabledTabsTestApp,
TabGroupWithSimpleApi,
TemplateTabs,
TabGroupWithAriaInputs,
TabGroupWithIsActiveBinding,
],
});
TestBed.compileComponents();
}));
describe('basic behavior', () => {
let fixture: ComponentFixture<SimpleTabsTestApp>;
let element: HTMLElement;
beforeEach(() => {
fixture = TestBed.createComponent(SimpleTabsTestApp);
element = fixture.nativeElement;
});
it('should default to the first tab', () => {
checkSelectedIndex(1, fixture);
});
it('will properly load content on first change detection pass', () => {
fixture.detectChanges();
expect(element.querySelectorAll('.mat-tab-body')[1].querySelectorAll('span').length).toBe(3);
});
it('should change selected index on click', () => {
let component = fixture.debugElement.componentInstance;
component.selectedIndex = 0;
checkSelectedIndex(0, fixture);
// select the second tab
let tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
tabLabel.nativeElement.click();
checkSelectedIndex(1, fixture);
// select the third tab
tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[2];
tabLabel.nativeElement.click();
checkSelectedIndex(2, fixture);
});
it('should support two-way binding for selectedIndex', fakeAsync(() => {
let component = fixture.componentInstance;
component.selectedIndex = 0;
fixture.detectChanges();
let tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
tabLabel.nativeElement.click();
fixture.detectChanges();
tick();
expect(component.selectedIndex).toBe(1);
}));
// Note: needs to be `async` in order to fail when we expect it to.
it('should set to correct tab on fast change', async(() => {
let component = fixture.componentInstance;
component.selectedIndex = 0;
fixture.detectChanges();
setTimeout(() => {
component.selectedIndex = 1;
fixture.detectChanges();
setTimeout(() => {
component.selectedIndex = 0;
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.selectedIndex).toBe(0);
});
}, 1);
}, 1);
}));
it('should change tabs based on selectedIndex', fakeAsync(() => {
let component = fixture.componentInstance;
let tabComponent = fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
spyOn(component, 'handleSelection').and.callThrough();
checkSelectedIndex(1, fixture);
tabComponent.selectedIndex = 2;
checkSelectedIndex(2, fixture);
tick();
expect(component.handleSelection).toHaveBeenCalledTimes(1);
expect(component.selectEvent.index).toBe(2);
}));
it('should update tab positions when selected index is changed', () => {
fixture.detectChanges();
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
const tabs: MatTab[] = component._tabs.toArray();
expect(tabs[0].position).toBeLessThan(0);
expect(tabs[1].position).toBe(0);
expect(tabs[2].position).toBeGreaterThan(0);
// Move to third tab
component.selectedIndex = 2;
fixture.detectChanges();
expect(tabs[0].position).toBeLessThan(0);
expect(tabs[1].position).toBeLessThan(0);
expect(tabs[2].position).toBe(0);
// Move to the first tab
component.selectedIndex = 0;
fixture.detectChanges();
expect(tabs[0].position).toBe(0);
expect(tabs[1].position).toBeGreaterThan(0);
expect(tabs[2].position).toBeGreaterThan(0);
});
it('should clamp the selected index to the size of the number of tabs', () => {
fixture.detectChanges();
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
// Set the index to be negative, expect first tab selected
fixture.componentInstance.selectedIndex = -1;
fixture.detectChanges();
expect(component.selectedIndex).toBe(0);
// Set the index beyond the size of the tabs, expect last tab selected
fixture.componentInstance.selectedIndex = 3;
fixture.detectChanges();
expect(component.selectedIndex).toBe(2);
});
it('should not crash when setting the selected index to NaN', () => {
let component = fixture.debugElement.componentInstance;
expect(() => {
component.selectedIndex = NaN;
fixture.detectChanges();
}).not.toThrow();
});
it('should show ripples for tab-group labels', () => {
fixture.detectChanges();
const testElement = fixture.nativeElement;
const tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
expect(testElement.querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripples to show up initially.');
dispatchFakeEvent(tabLabel.nativeElement, 'mousedown');
dispatchFakeEvent(tabLabel.nativeElement, 'mouseup');
expect(testElement.querySelectorAll('.mat-ripple-element').length)
.toBe(1, 'Expected one ripple to show up on label mousedown.');
});
it('should allow disabling ripples for tab-group labels', () => {
fixture.componentInstance.disableRipple = true;
fixture.detectChanges();
const testElement = fixture.nativeElement;
const tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
expect(testElement.querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripples to show up initially.');
dispatchFakeEvent(tabLabel.nativeElement, 'mousedown');
dispatchFakeEvent(tabLabel.nativeElement, 'mouseup');
expect(testElement.querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripple to show up on label mousedown.');
});
it('should set the isActive flag on each of the tabs', fakeAsync(() => {
fixture.detectChanges();
tick();
const tabs = fixture.componentInstance.tabs.toArray();
expect(tabs[0].isActive).toBe(false);
expect(tabs[1].isActive).toBe(true);
expect(tabs[2].isActive).toBe(false);
fixture.componentInstance.selectedIndex = 2;
fixture.detectChanges();
tick();
expect(tabs[0].isActive).toBe(false);
expect(tabs[1].isActive).toBe(false);
expect(tabs[2].isActive).toBe(true);
}));
it('should fire animation done event', fakeAsync(() => {
fixture.detectChanges();
spyOn(fixture.componentInstance, 'animationDone');
let tabLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
tabLabel.nativeElement.click();
fixture.detectChanges();
tick();
expect(fixture.componentInstance.animationDone).toHaveBeenCalledTimes(1);
}));
it('should add the proper `aria-setsize` and `aria-posinset`', () => {
fixture.detectChanges();
const labels = Array.from(element.querySelectorAll('.mat-tab-label'));
expect(labels.map(label => label.getAttribute('aria-posinset'))).toEqual(['1', '2', '3']);
expect(labels.every(label => label.getAttribute('aria-setsize') === '3')).toBe(true);
});
it('should emit focusChange event on click', () => {
spyOn(fixture.componentInstance, 'handleFocus');
fixture.detectChanges();
const tabLabels = fixture.debugElement.queryAll(By.css('.mat-tab-label'));
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
tabLabels[1].nativeElement.click();
fixture.detectChanges();
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.handleFocus)
.toHaveBeenCalledWith(jasmine.objectContaining({index: 1}));
});
it('should emit focusChange on arrow key navigation', () => {
spyOn(fixture.componentInstance, 'handleFocus');
fixture.detectChanges();
const tabLabels = fixture.debugElement.queryAll(By.css('.mat-tab-label'));
const tabLabelContainer = fixture.debugElement
.query(By.css('.mat-tab-label-container')).nativeElement as HTMLElement;
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
// In order to verify that the `focusChange` event also fires with the correct
// index, we focus the second tab before testing the keyboard navigation.
tabLabels[1].nativeElement.click();
fixture.detectChanges();
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
dispatchKeyboardEvent(tabLabelContainer, 'keydown', LEFT_ARROW);
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(2);
expect(fixture.componentInstance.handleFocus)
.toHaveBeenCalledWith(jasmine.objectContaining({index: 0}));
});
});
describe('aria labelling', () => {
let fixture: ComponentFixture<TabGroupWithAriaInputs>;
let tab: HTMLElement;
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(TabGroupWithAriaInputs);
fixture.detectChanges();
tick();
tab = fixture.nativeElement.querySelector('.mat-tab-label');
}));
it('should not set aria-label or aria-labelledby attributes if they are not passed in', () => {
expect(tab.hasAttribute('aria-label')).toBe(false);
expect(tab.hasAttribute('aria-labelledby')).toBe(false);
});
it('should set the aria-label attribute', () => {
fixture.componentInstance.ariaLabel = 'Fruit';
fixture.detectChanges();
expect(tab.getAttribute('aria-label')).toBe('Fruit');
});
it('should set the aria-labelledby attribute', () => {
fixture.componentInstance.ariaLabelledby = 'fruit-label';
fixture.detectChanges();
expect(tab.getAttribute('aria-labelledby')).toBe('fruit-label');
});
it('should not be able to set both an aria-label and aria-labelledby', () => {
fixture.componentInstance.ariaLabel = 'Fruit';
fixture.componentInstance.ariaLabelledby = 'fruit-label';
fixture.detectChanges();
expect(tab.getAttribute('aria-label')).toBe('Fruit');
expect(tab.hasAttribute('aria-labelledby')).toBe(false);
});
});
describe('disable tabs', () => {
let fixture: ComponentFixture<DisabledTabsTestApp>;
beforeEach(() => {
fixture = TestBed.createComponent(DisabledTabsTestApp);
});
it('should have one disabled tab', () => {
fixture.detectChanges();
const labels = fixture.debugElement.queryAll(By.css('.mat-tab-disabled'));
expect(labels.length).toBe(1);
expect(labels[0].nativeElement.getAttribute('aria-disabled')).toBe('true');
});
it('should set the disabled flag on tab', () => {
fixture.detectChanges();
const tabs = fixture.componentInstance.tabs.toArray();
let labels = fixture.debugElement.queryAll(By.css('.mat-tab-disabled'));
expect(tabs[2].disabled).toBe(false);
expect(labels.length).toBe(1);
expect(labels[0].nativeElement.getAttribute('aria-disabled')).toBe('true');
fixture.componentInstance.isDisabled = true;
fixture.detectChanges();
expect(tabs[2].disabled).toBe(true);
labels = fixture.debugElement.queryAll(By.css('.mat-tab-disabled'));
expect(labels.length).toBe(2);
expect(labels.every(label => label.nativeElement.getAttribute('aria-disabled') === 'true'))
.toBe(true);
});
});
describe('dynamic binding tabs', () => {
let fixture: ComponentFixture<SimpleDynamicTabsTestApp>;
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(SimpleDynamicTabsTestApp);
fixture.detectChanges();
tick();
fixture.detectChanges();
}));
it('should be able to add a new tab, select it, and have correct origin position',
fakeAsync(() => {
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
let tabs: MatTab[] = component._tabs.toArray();
expect(tabs[0].origin).toBe(null);
expect(tabs[1].origin).toBe(0);
expect(tabs[2].origin).toBe(null);
// Add a new tab on the right and select it, expect an origin >= than 0 (animate right)
fixture.componentInstance.tabs.push({label: 'New tab', content: 'to right of index'});
fixture.componentInstance.selectedIndex = 4;
fixture.detectChanges();
tick();
tabs = component._tabs.toArray();
expect(tabs[3].origin).toBeGreaterThanOrEqual(0);
// Add a new tab in the beginning and select it, expect an origin < than 0 (animate left)
fixture.componentInstance.selectedIndex = 0;
fixture.detectChanges();
tick();
fixture.componentInstance.tabs.push({label: 'New tab', content: 'to left of index'});
fixture.detectChanges();
tick();
tabs = component._tabs.toArray();
expect(tabs[0].origin).toBeLessThan(0);
}));
it('should update selected index if the last tab removed while selected', fakeAsync(() => {
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
const numberOfTabs = component._tabs.length;
fixture.componentInstance.selectedIndex = numberOfTabs - 1;
fixture.detectChanges();
tick();
// Remove last tab while last tab is selected, expect next tab over to be selected
fixture.componentInstance.tabs.pop();
fixture.detectChanges();
tick();
expect(component.selectedIndex).toBe(numberOfTabs - 2);
}));
it('should maintain the selected tab if a new tab is added', () => {
fixture.detectChanges();
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
fixture.componentInstance.selectedIndex = 1;
fixture.detectChanges();
// Add a new tab at the beginning.
fixture.componentInstance.tabs.unshift({label: 'New tab', content: 'at the start'});
fixture.detectChanges();
expect(component.selectedIndex).toBe(2);
expect(component._tabs.toArray()[2].isActive).toBe(true);
});
it('should maintain the selected tab if a tab is removed', () => {
// Select the second tab.
fixture.componentInstance.selectedIndex = 1;
fixture.detectChanges();
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
// Remove the first tab that is right before the selected one.
fixture.componentInstance.tabs.splice(0, 1);
fixture.detectChanges();
// Since the first tab has been removed and the second one was selected before, the selected
// tab moved one position to the right. Meaning that the tab is now the first tab.
expect(component.selectedIndex).toBe(0);
expect(component._tabs.toArray()[0].isActive).toBe(true);
});
it('should be able to select a new tab after creation', fakeAsync(() => {
fixture.detectChanges();
const component: MatTabGroup =
fixture.debugElement.query(By.css('mat-tab-group')).componentInstance;
fixture.componentInstance.tabs.push({label: 'Last tab', content: 'at the end'});
fixture.componentInstance.selectedIndex = 3;
fixture.detectChanges();
tick();
expect(component.selectedIndex).toBe(3);
expect(component._tabs.toArray()[3].isActive).toBe(true);
}));
it('should not fire `selectedTabChange` when the amount of tabs changes', fakeAsync(() => {
fixture.detectChanges();
fixture.componentInstance.selectedIndex = 1;
fixture.detectChanges();
// Add a new tab at the beginning.
spyOn(fixture.componentInstance, 'handleSelection');
fixture.componentInstance.tabs.unshift({label: 'New tab', content: 'at the start'});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(fixture.componentInstance.handleSelection).not.toHaveBeenCalled();
}));
it('should preserve the scroll position when switching between tabs', fakeAsync(() => {
const testComponent = fixture.componentInstance;
// Add a lot of content to make one of the tabs scrollable.
testComponent.tabs[1].content = new Array(500).fill('content!').join('\n\n');
fixture.detectChanges();
tick(500);
// Cap the tab group height.
fixture.debugElement.query(By.css('mat-tab-group')).nativeElement.style.height = `300px`;
const tabElements = fixture.debugElement.queryAll(By.css('.mat-tab-body-content'))
.map(debugElement => debugElement.nativeElement as HTMLElement);
// Focus the tab with the extra content.
testComponent.selectedIndex = 1;
fixture.detectChanges();
tick(500);
// Ensure that there is content and scroll down 100px.
expect(tabElements[1].offsetHeight).toBeGreaterThan(0, 'Expected tab to have some content.');
// Handle some differences in the way browsers determine what element is scrollable.
tabElements[1].scrollTop = tabElements[1].parentElement!.scrollTop = 100;
// Move to another tab.
testComponent.selectedIndex = 0;
fixture.detectChanges();
tick(500);
// Switch back to the tab with the extra content.
testComponent.selectedIndex = 1;
fixture.detectChanges();
tick(500);
expect(tabElements[1].scrollTop || tabElements[1].parentElement!.scrollTop)
.toBe(100, 'Expected scroll position to be restored.');
}));
});
describe('async tabs', () => {
let fixture: ComponentFixture<AsyncTabsTestApp>;
it('should show tabs when they are available', fakeAsync(() => {
fixture = TestBed.createComponent(AsyncTabsTestApp);
expect(fixture.debugElement.queryAll(By.css('.mat-tab-label')).length).toBe(0);
fixture.detectChanges();
tick();
fixture.detectChanges();
tick();
expect(fixture.debugElement.queryAll(By.css('.mat-tab-label')).length).toBe(2);
}));
});
describe('with simple api', () => {
let fixture: ComponentFixture<TabGroupWithSimpleApi>;
let tabGroup: MatTabGroup;
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(TabGroupWithSimpleApi);
fixture.detectChanges();
tick();
tabGroup =
fixture.debugElement.query(By.directive(MatTabGroup)).componentInstance as MatTabGroup;
}));
it('should support a tab-group with the simple api', fakeAsync(() => {
expect(getSelectedLabel(fixture).textContent).toMatch('Junk food');
expect(getSelectedContent(fixture).textContent).toMatch('Pizza, fries');
tabGroup.selectedIndex = 2;
fixture.detectChanges();
tick();
expect(getSelectedLabel(fixture).textContent).toMatch('Fruit');
expect(getSelectedContent(fixture).textContent).toMatch('Apples, grapes');
fixture.componentInstance.otherLabel = 'Chips';
fixture.componentInstance.otherContent = 'Salt, vinegar';
fixture.detectChanges();
expect(getSelectedLabel(fixture).textContent).toMatch('Chips');
expect(getSelectedContent(fixture).textContent).toMatch('Salt, vinegar');
}));
it('should support @ViewChild in the tab content', () => {
expect(fixture.componentInstance.legumes).toBeTruthy();
});
it('should only have the active tab in the DOM', fakeAsync(() => {
expect(fixture.nativeElement.textContent).toContain('Pizza, fries');
expect(fixture.nativeElement.textContent).not.toContain('Peanuts');
tabGroup.selectedIndex = 3;
fixture.detectChanges();
tick();
expect(fixture.nativeElement.textContent).not.toContain('Pizza, fries');
expect(fixture.nativeElement.textContent).toContain('Peanuts');
}));
it('should support setting the header position', () => {
let tabGroupNode = fixture.debugElement.query(By.css('mat-tab-group')).nativeElement;
expect(tabGroupNode.classList).not.toContain('mat-tab-group-inverted-header');
tabGroup.headerPosition = 'below';
fixture.detectChanges();
expect(tabGroupNode.classList).toContain('mat-tab-group-inverted-header');
});
});
describe('lazy loaded tabs', () => {
it('should lazy load the second tab', fakeAsync(() => {
const fixture = TestBed.createComponent(TemplateTabs);
fixture.detectChanges();
tick();
const secondLabel = fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1];
secondLabel.nativeElement.click();
fixture.detectChanges();
tick();
fixture.detectChanges();
const child = fixture.debugElement.query(By.css('.child'));
expect(child.nativeElement).toBeDefined();
}));
});
describe('special cases', () => {
it('should not throw an error when binding isActive to the view', fakeAsync(() => {
const fixture = TestBed.createComponent(TabGroupWithIsActiveBinding);
expect(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
}).not.toThrow();
expect(fixture.nativeElement.textContent).toContain('pizza is active');
}));
});
/**
* Checks that the `selectedIndex` has been updated; checks that the label and body have their
* respective `active` classes
*/
function checkSelectedIndex(expectedIndex: number, fixture: ComponentFixture<any>) {
fixture.detectChanges();
let tabComponent: MatTabGroup = fixture.debugElement
.query(By.css('mat-tab-group')).componentInstance;
expect(tabComponent.selectedIndex).toBe(expectedIndex);
let tabLabelElement = fixture.debugElement
.query(By.css(`.mat-tab-label:nth-of-type(${expectedIndex + 1})`)).nativeElement;
expect(tabLabelElement.classList.contains('mat-tab-label-active')).toBe(true);
let tabContentElement = fixture.debugElement
.query(By.css(`mat-tab-body:nth-of-type(${expectedIndex + 1})`)).nativeElement;
expect(tabContentElement.classList.contains('mat-tab-body-active')).toBe(true);
}
function getSelectedLabel(fixture: ComponentFixture<any>): HTMLElement {
return fixture.nativeElement.querySelector('.mat-tab-label-active');
}
function getSelectedContent(fixture: ComponentFixture<any>): HTMLElement {
return fixture.nativeElement.querySelector('.mat-tab-body-active');
}
});
describe('nested MatTabGroup with enabled animations', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [MatTabsModule, BrowserAnimationsModule],
declarations: [NestedTabs, TabsWithCustomAnimationDuration]
});
TestBed.compileComponents();
}));
it('should not throw when creating a component with nested tab groups', fakeAsync(() => {
expect(() => {
let fixture = TestBed.createComponent(NestedTabs);
fixture.detectChanges();
tick();
}).not.toThrow();
}));
it('should not throw when setting an animationDuration without units', fakeAsync(() => {
expect(() => {
let fixture = TestBed.createComponent(TabsWithCustomAnimationDuration);
fixture.detectChanges();
tick();
}).not.toThrow();
}));
});
@Component({
template: `
<mat-tab-group class="tab-group"
[(selectedIndex)]="selectedIndex"
[headerPosition]="headerPosition"
[disableRipple]="disableRipple"
(animationDone)="animationDone()"
(focusChange)="handleFocus($event)"
(selectedTabChange)="handleSelection($event)">
<mat-tab>
<ng-template mat-tab-label>Tab One</ng-template>
Tab one content
</mat-tab>
<mat-tab>
<ng-template mat-tab-label>Tab Two</ng-template>
<span>Tab </span><span>two</span><span>content</span>
</mat-tab>
<mat-tab>
<ng-template mat-tab-label>Tab Three</ng-template>
Tab three content
</mat-tab>
</mat-tab-group>
`
})
class SimpleTabsTestApp {
@ViewChildren(MatTab) tabs: QueryList<MatTab>;
selectedIndex: number = 1;
focusEvent: any;
selectEvent: any;
disableRipple: boolean = false;
headerPosition: MatTabHeaderPosition = 'above';
handleFocus(event: any) {
this.focusEvent = event;
}
handleSelection(event: any) {
this.selectEvent = event;
}
animationDone() { }
}
@Component({
template: `
<mat-tab-group class="tab-group"
[(selectedIndex)]="selectedIndex"
(focusChange)="handleFocus($event)"
(selectedTabChange)="handleSelection($event)">
<mat-tab *ngFor="let tab of tabs">
<ng-template mat-tab-label>{{tab.label}}</ng-template>
{{tab.content}}
</mat-tab>
</mat-tab-group>
`
})
class SimpleDynamicTabsTestApp {
tabs = [
{label: 'Label 1', content: 'Content 1'},
{label: 'Label 2', content: 'Content 2'},
{label: 'Label 3', content: 'Content 3'},
];
selectedIndex: number = 1;
focusEvent: any;
selectEvent: any;
handleFocus(event: any) {
this.focusEvent = event;
}
handleSelection(event: any) {
this.selectEvent = event;
}
}
@Component({
template: `
<mat-tab-group class="tab-group" [(selectedIndex)]="selectedIndex">
<mat-tab *ngFor="let tab of tabs" label="{{tab.label}}">
{{tab.content}}
</mat-tab>
</mat-tab-group>
`
})
class BindedTabsTestApp {
tabs = [
{ label: 'one', content: 'one' },
{ label: 'two', content: 'two' }
];
selectedIndex = 0;
addNewActiveTab(): void {
this.tabs.push({
label: 'new tab',
content: 'new content'
});
this.selectedIndex = this.tabs.length - 1;
}
}
@Component({
selector: 'test-app',
template: `
<mat-tab-group class="tab-group">
<mat-tab>
<ng-template mat-tab-label>Tab One</ng-template>
Tab one content
</mat-tab>
<mat-tab disabled>
<ng-template mat-tab-label>Tab Two</ng-template>
Tab two content
</mat-tab>
<mat-tab [disabled]="isDisabled">
<ng-template mat-tab-label>Tab Three</ng-template>
Tab three content
</mat-tab>
</mat-tab-group>
`,
})
class DisabledTabsTestApp {
@ViewChildren(MatTab) tabs: QueryList<MatTab>;
isDisabled = false;
}
@Component({
template: `
<mat-tab-group class="tab-group">
<mat-tab *ngFor="let tab of tabs | async">
<ng-template mat-tab-label>{{ tab.label }}</ng-template>
{{ tab.content }}
</mat-tab>
</mat-tab-group>
`
})
class AsyncTabsTestApp implements OnInit {
private _tabs = [
{ label: 'one', content: 'one' },
{ label: 'two', content: 'two' }
];
tabs: Observable<any>;
ngOnInit() {
// Use ngOnInit because there is some issue with scheduling the async task in the constructor.
this.tabs = new Observable((observer: any) => {
setTimeout(() => observer.next(this._tabs));
});
}
}
@Component({
template: `
<mat-tab-group>
<mat-tab label="Junk food"> Pizza, fries </mat-tab>
<mat-tab label="Vegetables"> Broccoli, spinach </mat-tab>
<mat-tab [label]="otherLabel"> {{otherContent}} </mat-tab>
<mat-tab label="Legumes"> <p #legumes>Peanuts</p> </mat-tab>
</mat-tab-group>
`
})
class TabGroupWithSimpleApi {
otherLabel = 'Fruit';
otherContent = 'Apples, grapes';
@ViewChild('legumes') legumes: any;
}
@Component({
selector: 'nested-tabs',
template: `
<mat-tab-group>
<mat-tab label="One">Tab one content</mat-tab>
<mat-tab label="Two">
Tab two content
<mat-tab-group [dynamicHeight]="true">
<mat-tab label="Inner tab one">Inner content one</mat-tab>
<mat-tab label="Inner tab two">Inner content two</mat-tab>
</mat-tab-group>
</mat-tab>
</mat-tab-group>
`,
})
class NestedTabs {}
@Component({
selector: 'template-tabs',
template: `
<mat-tab-group>
<mat-tab label="One">
Eager
</mat-tab>
<mat-tab label="Two">
<ng-template matTabContent>
<div class="child">Hi</div>
</ng-template>
</mat-tab>
</mat-tab-group>
`,
})
class TemplateTabs {}
@Component({
template: `
<mat-tab-group>
<mat-tab [aria-label]="ariaLabel" [aria-labelledby]="ariaLabelledby"></mat-tab>
</mat-tab-group>
`
})
class TabGroupWithAriaInputs {
ariaLabel: string;
ariaLabelledby: string;
}
@Component({
template: `
<mat-tab-group>
<mat-tab label="Junk food" #pizza> Pizza, fries </mat-tab>
<mat-tab label="Vegetables"> Broccoli, spinach </mat-tab>
</mat-tab-group>
<div *ngIf="pizza.isActive">pizza is active</div>
`
})
class TabGroupWithIsActiveBinding {
}
@Component({
template: `
<mat-tab-group animationDuration="500">
<mat-tab label="One">Tab one content</mat-tab>
<mat-tab label="Two">Tab two content</mat-tab>
</mat-tab-group>
`,
})
class TabsWithCustomAnimationDuration {}