Skip to content

Commit adc4141

Browse files
committed
docs(material/select): add harness example for select (#21464)
(cherry picked from commit dc5b773)
1 parent aa98f9c commit adc4141

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/select",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/checkbox",
17+
"//src/material/form-field",
1518
"//src/material/input",
1619
"//src/material/select",
20+
"//src/material/select/testing",
1721
"@npm//@angular/forms",
22+
"@npm//@angular/platform-browser",
23+
"@npm//@angular/platform-browser-dynamic",
24+
"@npm//@types/jasmine",
1825
],
1926
)
2027

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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {TestBed, ComponentFixture, waitForAsync} 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 {NoopAnimationsModule} from '@angular/platform-browser/animations';
10+
11+
describe('SelectHarnessExample', () => {
12+
let fixture: ComponentFixture<SelectHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatSelectModule, NoopAnimationsModule],
23+
declarations: [SelectHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(SelectHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load all select harnesses', async () => {
32+
const selects = await loader.getAllHarnesses(MatSelectHarness);
33+
expect(selects.length).toBe(1);
34+
});
35+
36+
it('should be able to check whether a select is in multi-selection mode', async () => {
37+
const select = await loader.getHarness(MatSelectHarness);
38+
39+
expect(await select.isMultiple()).toBe(false);
40+
});
41+
42+
it('should be able to open and close a select', async () => {
43+
const select = await loader.getHarness(MatSelectHarness);
44+
45+
expect(await select.isOpen()).toBe(false);
46+
47+
await select.open();
48+
expect(await select.isOpen()).toBe(true);
49+
50+
await select.close();
51+
expect(await select.isOpen()).toBe(false);
52+
});
53+
54+
it('should be able to get the value text from a select', async () => {
55+
const select = await loader.getHarness(MatSelectHarness);
56+
await select.open();
57+
const options = await select.getOptions();
58+
59+
await options[2].click();
60+
61+
expect(await select.getValueText()).toBe('Tacos');
62+
});
63+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatSelectHarness
5+
*/
6+
@Component({
7+
selector: 'select-harness-example',
8+
templateUrl: 'select-harness-example.html',
9+
})
10+
export class SelectHarnessExample {
11+
foods = [
12+
{value: 'steak-0', viewValue: 'Steak'},
13+
{value: 'pizza-1', viewValue: 'Pizza'},
14+
{value: 'tacos-2', viewValue: 'Tacos'}
15+
];
16+
}

0 commit comments

Comments
 (0)