|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {ComponentHarness, HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
1 | 5 | import {MatTabsModule} from '@angular/material/tabs';
|
2 |
| -import {runTabGroupHarnessTests} from './tab-group-shared.spec'; |
| 6 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
3 | 7 | import {MatTabGroupHarness} from './tab-group-harness';
|
4 | 8 | import {MatTabHarness} from './tab-harness';
|
5 | 9 |
|
6 |
| -describe('MDC-based MatTabGroupHarness', () => { |
7 |
| - runTabGroupHarnessTests(MatTabsModule, MatTabGroupHarness, MatTabHarness); |
| 10 | +describe('MatTabGroupHarness', () => { |
| 11 | + let fixture: ComponentFixture<TabGroupHarnessTest>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [MatTabsModule, NoopAnimationsModule], |
| 17 | + declarations: [TabGroupHarnessTest], |
| 18 | + }).compileComponents(); |
| 19 | + |
| 20 | + fixture = TestBed.createComponent(TabGroupHarnessTest); |
| 21 | + fixture.detectChanges(); |
| 22 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should load harness for tab-group', async () => { |
| 26 | + const tabGroups = await loader.getAllHarnesses(MatTabGroupHarness); |
| 27 | + expect(tabGroups.length).toBe(1); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should load harness for tab-group with selected tab label', async () => { |
| 31 | + const tabGroups = await loader.getAllHarnesses( |
| 32 | + MatTabGroupHarness.with({ |
| 33 | + selectedTabLabel: 'First', |
| 34 | + }), |
| 35 | + ); |
| 36 | + expect(tabGroups.length).toBe(1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should load harness for tab-group with matching tab label regex', async () => { |
| 40 | + const tabGroups = await loader.getAllHarnesses( |
| 41 | + MatTabGroupHarness.with({ |
| 42 | + selectedTabLabel: /f.*st/i, |
| 43 | + }), |
| 44 | + ); |
| 45 | + expect(tabGroups.length).toBe(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be able to get tabs of tab-group', async () => { |
| 49 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 50 | + const tabs = await tabGroup.getTabs(); |
| 51 | + expect(tabs.length).toBe(3); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should be able to get filtered tabs', async () => { |
| 55 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 56 | + const tabs = await tabGroup.getTabs({label: 'Third'}); |
| 57 | + expect(tabs.length).toBe(1); |
| 58 | + expect(await tabs[0].getLabel()).toBe('Third'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should be able to select tab from tab-group', async () => { |
| 62 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 63 | + expect(await (await tabGroup.getSelectedTab()).getLabel()).toBe('First'); |
| 64 | + await tabGroup.selectTab({label: 'Second'}); |
| 65 | + expect(await (await tabGroup.getSelectedTab()).getLabel()).toBe('Second'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw error when attempting to select invalid tab', async () => { |
| 69 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 70 | + await expectAsync(tabGroup.selectTab({label: 'Fake'})).toBeRejectedWithError( |
| 71 | + /Cannot find mat-tab matching filter {"label":"Fake"}/, |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should be able to get label of tabs', async () => { |
| 76 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 77 | + const tabs = await tabGroup.getTabs(); |
| 78 | + expect(await tabs[0].getLabel()).toBe('First'); |
| 79 | + expect(await tabs[1].getLabel()).toBe('Second'); |
| 80 | + expect(await tabs[2].getLabel()).toBe('Third'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should be able to get aria-label of tabs', async () => { |
| 84 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 85 | + const tabs = await tabGroup.getTabs(); |
| 86 | + expect(await tabs[0].getAriaLabel()).toBe('First tab'); |
| 87 | + expect(await tabs[1].getAriaLabel()).toBe('Second tab'); |
| 88 | + expect(await tabs[2].getAriaLabel()).toBe(null); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should be able to get aria-labelledby of tabs', async () => { |
| 92 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 93 | + const tabs = await tabGroup.getTabs(); |
| 94 | + expect(await tabs[0].getAriaLabelledby()).toBe(null); |
| 95 | + expect(await tabs[1].getAriaLabelledby()).toBe(null); |
| 96 | + expect(await tabs[2].getAriaLabelledby()).toBe('tabLabelId'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should be able to get harness for content element of active tab', async () => { |
| 100 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 101 | + const tabs = await tabGroup.getTabs(); |
| 102 | + expect(await tabs[0].getTextContent()).toBe('Content 1'); |
| 103 | + const tabContentHarness = await tabs[0].getHarness(TestTabContentHarness); |
| 104 | + expect(await (await tabContentHarness.host()).text()).toBe('Content 1'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should be able to get disabled state of tab', async () => { |
| 108 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 109 | + const tabs = await tabGroup.getTabs(); |
| 110 | + expect(await tabs[0].isDisabled()).toBe(false); |
| 111 | + expect(await tabs[1].isDisabled()).toBe(false); |
| 112 | + expect(await tabs[2].isDisabled()).toBe(false); |
| 113 | + |
| 114 | + fixture.componentInstance.isDisabled = true; |
| 115 | + fixture.detectChanges(); |
| 116 | + |
| 117 | + expect(await tabs[0].isDisabled()).toBe(false); |
| 118 | + expect(await tabs[1].isDisabled()).toBe(false); |
| 119 | + expect(await tabs[2].isDisabled()).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should be able to select specific tab', async () => { |
| 123 | + const tabGroup = await loader.getHarness(MatTabGroupHarness); |
| 124 | + const tabs = await tabGroup.getTabs(); |
| 125 | + expect(await tabs[0].isSelected()).toBe(true); |
| 126 | + expect(await tabs[1].isSelected()).toBe(false); |
| 127 | + expect(await tabs[2].isSelected()).toBe(false); |
| 128 | + |
| 129 | + await tabs[1].select(); |
| 130 | + expect(await tabs[0].isSelected()).toBe(false); |
| 131 | + expect(await tabs[1].isSelected()).toBe(true); |
| 132 | + expect(await tabs[2].isSelected()).toBe(false); |
| 133 | + |
| 134 | + // Should not be able to select third tab if disabled. |
| 135 | + fixture.componentInstance.isDisabled = true; |
| 136 | + fixture.detectChanges(); |
| 137 | + |
| 138 | + await tabs[2].select(); |
| 139 | + expect(await tabs[0].isSelected()).toBe(false); |
| 140 | + expect(await tabs[1].isSelected()).toBe(true); |
| 141 | + expect(await tabs[2].isSelected()).toBe(false); |
| 142 | + |
| 143 | + // Should be able to select third tab if not disabled. |
| 144 | + fixture.componentInstance.isDisabled = false; |
| 145 | + fixture.detectChanges(); |
| 146 | + await tabs[2].select(); |
| 147 | + expect(await tabs[0].isSelected()).toBe(false); |
| 148 | + expect(await tabs[1].isSelected()).toBe(false); |
| 149 | + expect(await tabs[2].isSelected()).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should be able to get tabs by selected state', async () => { |
| 153 | + const selectedTabs = await loader.getAllHarnesses(MatTabHarness.with({selected: true})); |
| 154 | + const unselectedTabs = await loader.getAllHarnesses(MatTabHarness.with({selected: false})); |
| 155 | + expect(await parallel(() => selectedTabs.map(t => t.getLabel()))).toEqual(['First']); |
| 156 | + expect(await parallel(() => unselectedTabs.map(t => t.getLabel()))).toEqual([ |
| 157 | + 'Second', |
| 158 | + 'Third', |
| 159 | + ]); |
| 160 | + }); |
8 | 161 | });
|
| 162 | + |
| 163 | +@Component({ |
| 164 | + template: ` |
| 165 | + <mat-tab-group> |
| 166 | + <mat-tab label="First" aria-label="First tab"> |
| 167 | + <span class="test-tab-content">Content 1</span> |
| 168 | + </mat-tab> |
| 169 | + <mat-tab label="Second" aria-label="Second tab"> |
| 170 | + <span class="test-tab-content">Content 2</span> |
| 171 | + </mat-tab> |
| 172 | + <mat-tab label="Third" aria-labelledby="tabLabelId" [disabled]="isDisabled"> |
| 173 | + <ng-template matTabLabel>Third</ng-template> |
| 174 | + <span class="test-tab-content">Content 3</span> |
| 175 | + </mat-tab> |
| 176 | + </mat-tab-group> |
| 177 | + `, |
| 178 | +}) |
| 179 | +class TabGroupHarnessTest { |
| 180 | + isDisabled = false; |
| 181 | +} |
| 182 | + |
| 183 | +class TestTabContentHarness extends ComponentHarness { |
| 184 | + static hostSelector = '.test-tab-content'; |
| 185 | +} |
0 commit comments