Skip to content

Commit 6945570

Browse files
authored
docs(material/tabs): add harness example for tabs (#21404)
1 parent 71b7b15 commit 6945570

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

src/components-examples/material/tabs/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/tabs",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/button-toggle",
1618
"//src/material/checkbox",
1719
"//src/material/icon",
1820
"//src/material/input",
1921
"//src/material/tabs",
22+
"//src/material/tabs/testing",
2023
"@npm//@angular/forms",
24+
"@npm//@angular/platform-browser",
25+
"@npm//@angular/platform-browser-dynamic",
26+
"@npm//@types/jasmine",
2127
],
2228
)
2329

src/components-examples/material/tabs/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {TabGroupCustomLabelExample} from './tab-group-custom-label/tab-group-cus
1515
import {
1616
TabGroupDynamicHeightExample
1717
} from './tab-group-dynamic-height/tab-group-dynamic-height-example';
18+
import {TabGroupHarnessExample} from './tab-group-harness/tab-group-harness-example';
1819
import {TabGroupDynamicExample} from './tab-group-dynamic/tab-group-dynamic-example';
1920
import {TabGroupHeaderBelowExample} from './tab-group-header-below/tab-group-header-below-example';
2021
import {TabGroupLazyLoadedExample} from './tab-group-lazy-loaded/tab-group-lazy-loaded-example';
@@ -30,6 +31,7 @@ export {
3031
TabGroupCustomLabelExample,
3132
TabGroupDynamicExample,
3233
TabGroupDynamicHeightExample,
34+
TabGroupHarnessExample,
3335
TabGroupHeaderBelowExample,
3436
TabGroupLazyLoadedExample,
3537
TabGroupStretchedExample,
@@ -45,6 +47,7 @@ const EXAMPLES = [
4547
TabGroupCustomLabelExample,
4648
TabGroupDynamicExample,
4749
TabGroupDynamicHeightExample,
50+
TabGroupHarnessExample,
4851
TabGroupHeaderBelowExample,
4952
TabGroupLazyLoadedExample,
5053
TabGroupStretchedExample,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<mat-tab-group>
2+
<mat-tab label="Profile" aria-label="Profile tab">
3+
<span class="test-tab-content">Your personal information</span>
4+
</mat-tab>
5+
<mat-tab label="Settings" aria-label="Settings tab">
6+
<span class="test-tab-content">Privacy settings</span>
7+
</mat-tab>
8+
<mat-tab label="FAQ" aria-label="FAQ tab">
9+
<span class="test-tab-content">How to update profile picture</span>
10+
</mat-tab>
11+
</mat-tab-group>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatTabGroupHarness} from '@angular/material/tabs/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatTabsModule} from '@angular/material/tabs';
8+
import {TabGroupHarnessExample} from './tab-group-harness-example';
9+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10+
11+
describe('TabGroupHarnessExample', () => {
12+
let fixture: ComponentFixture<TabGroupHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatTabsModule, NoopAnimationsModule],
23+
declarations: [TabGroupHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(TabGroupHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load harness for tab-group', async () => {
32+
const tabGroups = await loader.getAllHarnesses(MatTabGroupHarness);
33+
expect(tabGroups.length).toBe(1);
34+
});
35+
36+
it('should load harness for tab-group with selected tab label', async () => {
37+
const tabGroups = await loader.getAllHarnesses(MatTabGroupHarness.with({
38+
selectedTabLabel: 'Profile',
39+
}));
40+
expect(tabGroups.length).toBe(1);
41+
});
42+
43+
it('should be able to get tabs of tab-group', async () => {
44+
const tabGroup = await loader.getHarness(MatTabGroupHarness);
45+
const tabs = await tabGroup.getTabs();
46+
expect(tabs.length).toBe(3);
47+
});
48+
49+
it('should be able to select tab from tab-group', async () => {
50+
const tabGroup = await loader.getHarness(MatTabGroupHarness);
51+
expect(await (await tabGroup.getSelectedTab()).getLabel()).toBe('Profile');
52+
await tabGroup.selectTab({label: 'FAQ'});
53+
expect(await (await tabGroup.getSelectedTab()).getLabel()).toBe('FAQ');
54+
});
55+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatTabGroupHarness
5+
*/
6+
@Component({
7+
selector: 'tab-group-harness-example',
8+
templateUrl: 'tab-group-harness-example.html'
9+
})
10+
export class TabGroupHarnessExample {}

0 commit comments

Comments
 (0)