Skip to content

Commit 6b414dc

Browse files
committed
docs(material/select): add harness example for select
1 parent 71b7b15 commit 6b414dc

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/select",
1313
deps = [
14+
"//src/cdk/overlay",
15+
"//src/cdk/testing",
16+
"//src/cdk/testing/testbed",
1417
"//src/material/checkbox",
18+
"//src/material/form-field",
1519
"//src/material/input",
1620
"//src/material/select",
21+
"//src/material/select/testing",
1722
"@npm//@angular/forms",
23+
"@npm//@angular/platform-browser",
24+
"@npm//@angular/platform-browser-dynamic",
25+
"@npm//@types/jasmine",
1826
],
1927
)
2028

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import {SelectResetExample} from './select-reset/select-reset-example';
2020
import {SelectValueBindingExample} from './select-value-binding/select-value-binding-example';
2121
import {SelectReactiveFormExample} from './select-reactive-form/select-reactive-form-example';
2222
import {SelectInitialValueExample} from './select-initial-value/select-initial-value-example';
23+
import {SelectHarnessExample} from './select-harness/select-harness-example';
24+
import {MatFormFieldModule} from '@angular/material/form-field';
2325

2426
export {
2527
SelectCustomTriggerExample,
2628
SelectDisabledExample,
2729
SelectErrorStateMatcherExample,
2830
SelectFormExample,
31+
SelectHarnessExample,
2932
SelectHintErrorExample,
3033
SelectInitialValueExample,
3134
SelectMultipleExample,
@@ -43,6 +46,7 @@ const EXAMPLES = [
4346
SelectDisabledExample,
4447
SelectErrorStateMatcherExample,
4548
SelectFormExample,
49+
SelectHarnessExample,
4650
SelectHintErrorExample,
4751
SelectInitialValueExample,
4852
SelectMultipleExample,
@@ -63,6 +67,7 @@ const EXAMPLES = [
6367
MatInputModule,
6468
MatSelectModule,
6569
ReactiveFormsModule,
70+
MatFormFieldModule,
6671
],
6772
declarations: EXAMPLES,
6873
exports: EXAMPLES,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<mat-form-field appearance="fill">
2+
<mat-label>Favorite food</mat-label>
3+
<mat-select>
4+
<mat-option *ngFor="let food of foods" [value]="food.value">
5+
{{food.viewValue}}
6+
</mat-option>
7+
</mat-select>
8+
</mat-form-field>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatSelectHarness} from '@angular/material/select/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatSelectModule} from '@angular/material/select';
8+
import {SelectHarnessExample} from './select-harness-example';
9+
import {MatFormFieldModule} from '@angular/material/form-field';
10+
import {ReactiveFormsModule} from '@angular/forms';
11+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
12+
import {OverlayContainer} from '@angular/cdk/overlay';
13+
14+
describe('SelectHarnessExample', () => {
15+
let fixture: ComponentFixture<SelectHarnessExample>;
16+
let loader: HarnessLoader;
17+
let overlayContainer: OverlayContainer;
18+
19+
beforeAll(() => {
20+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
21+
});
22+
23+
beforeEach(
24+
waitForAsync(() => {
25+
TestBed.configureTestingModule({
26+
imports: [MatSelectModule, MatFormFieldModule, ReactiveFormsModule, NoopAnimationsModule],
27+
declarations: [SelectHarnessExample]
28+
}).compileComponents();
29+
})
30+
);
31+
32+
beforeEach(() => {
33+
fixture = TestBed.createComponent(SelectHarnessExample);
34+
fixture.detectChanges();
35+
loader = TestbedHarnessEnvironment.loader(fixture);
36+
inject([OverlayContainer], (oc: OverlayContainer) => {
37+
overlayContainer = oc;
38+
})();
39+
});
40+
41+
afterEach(() => {
42+
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
43+
overlayContainer.ngOnDestroy();
44+
overlayContainer = null!;
45+
});
46+
47+
it('should load all select harnesses', async () => {
48+
const selects = await loader.getAllHarnesses(MatSelectHarness);
49+
expect(selects.length).toBe(1);
50+
});
51+
52+
it('should be able to check whether a select is in multi-selection mode', async () => {
53+
const select = await loader.getHarness(MatSelectHarness);
54+
55+
expect(await select.isMultiple()).toBe(false);
56+
});
57+
58+
it('should be able to open and close a select', async () => {
59+
const select = await loader.getHarness(MatSelectHarness);
60+
61+
expect(await select.isOpen()).toBe(false);
62+
63+
await select.open();
64+
expect(await select.isOpen()).toBe(true);
65+
66+
await select.close();
67+
expect(await select.isOpen()).toBe(false);
68+
});
69+
70+
it('should be able to get the value text from a select', async () => {
71+
const select = await loader.getHarness(MatSelectHarness);
72+
await select.open();
73+
const options = await select.getOptions();
74+
75+
await options[2].click();
76+
77+
expect(await select.getValueText()).toBe('Tacos');
78+
});
79+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Component} from '@angular/core';
2+
3+
interface Food {
4+
value: string;
5+
viewValue: string;
6+
}
7+
8+
/**
9+
* @title Testing with MatSelectHarness
10+
*/
11+
@Component({
12+
selector: 'select-harness-example',
13+
templateUrl: 'select-harness-example.html',
14+
})
15+
export class SelectHarnessExample {
16+
foods: Food[] = [
17+
{value: 'steak-0', viewValue: 'Steak'},
18+
{value: 'pizza-1', viewValue: 'Pizza'},
19+
{value: 'tacos-2', viewValue: 'Tacos'}
20+
];
21+
}

0 commit comments

Comments
 (0)