Skip to content

Commit c097825

Browse files
authored
docs(material/input): add harness example for input (#21416)
1 parent 704c4be commit c097825

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/input",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/icon",
1618
"//src/material/input",
19+
"//src/material/input/testing",
1720
"@npm//@angular/forms",
21+
"@npm//@angular/platform-browser",
22+
"@npm//@angular/platform-browser-dynamic",
23+
"@npm//@types/jasmine",
1824
],
1925
)
2026

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {InputFormExample} from './input-form/input-form-example';
1313
import {InputHintExample} from './input-hint/input-hint-example';
1414
import {InputOverviewExample} from './input-overview/input-overview-example';
1515
import {InputPrefixSuffixExample} from './input-prefix-suffix/input-prefix-suffix-example';
16+
import {InputHarnessExample} from './input-harness/input-harness-example';
1617

1718
export {
1819
InputClearableExample,
1920
InputErrorStateMatcherExample,
2021
InputErrorsExample,
2122
InputFormExample,
23+
InputHarnessExample,
2224
InputHintExample,
2325
InputOverviewExample,
2426
InputPrefixSuffixExample,
@@ -29,6 +31,7 @@ const EXAMPLES = [
2931
InputErrorStateMatcherExample,
3032
InputErrorsExample,
3133
InputFormExample,
34+
InputHarnessExample,
3235
InputHintExample,
3336
InputOverviewExample,
3437
InputPrefixSuffixExample,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<mat-form-field>
2+
<mat-label>Favorite food</mat-label>
3+
<input matInput value="Sushi" name="favorite-food">
4+
</mat-form-field>
5+
6+
<mat-form-field>
7+
<input matInput [type]="inputType"
8+
[disabled]="disabled">
9+
</mat-form-field>
10+
11+
<mat-form-field>
12+
<mat-label>Leave a comment</mat-label>
13+
<textarea matInput></textarea>
14+
</mat-form-field>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatInputHarness} from '@angular/material/input/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatInputModule} from '@angular/material/input';
8+
import {InputHarnessExample} from './input-harness-example';
9+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10+
import {ReactiveFormsModule} from '@angular/forms';
11+
12+
describe('InputHarnessExample', () => {
13+
let fixture: ComponentFixture<InputHarnessExample>;
14+
let loader: HarnessLoader;
15+
16+
beforeAll(() => {
17+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
18+
});
19+
20+
beforeEach(
21+
waitForAsync(() => {
22+
TestBed.configureTestingModule({
23+
imports: [MatInputModule, NoopAnimationsModule, ReactiveFormsModule],
24+
declarations: [InputHarnessExample]
25+
}).compileComponents();
26+
fixture = TestBed.createComponent(InputHarnessExample);
27+
fixture.detectChanges();
28+
loader = TestbedHarnessEnvironment.loader(fixture);
29+
})
30+
);
31+
it('should load all input harnesses', async () => {
32+
const inputs = await loader.getAllHarnesses(MatInputHarness);
33+
expect(inputs.length).toBe(3);
34+
});
35+
36+
it('should load input with a specific value', async () => {
37+
const inputs = await loader.getAllHarnesses(MatInputHarness.with({value: 'Sushi'}));
38+
expect(inputs.length).toBe(1);
39+
});
40+
41+
it('should be able to set value of input', async () => {
42+
const inputs = await loader.getAllHarnesses(MatInputHarness);
43+
const input = inputs[0];
44+
expect(await input.getValue()).toBe('Sushi');
45+
46+
await input.setValue('');
47+
48+
expect(await input.getValue()).toBe('');
49+
});
50+
51+
it('should be able to get disabled state', async () => {
52+
const inputs = await loader.getAllHarnesses(MatInputHarness);
53+
expect(inputs.length).toBe(3);
54+
55+
expect(await inputs[0].isDisabled()).toBe(false);
56+
expect(await inputs[1].isDisabled()).toBe(false);
57+
expect(await inputs[2].isDisabled()).toBe(false);
58+
59+
fixture.componentInstance.disabled = true;
60+
61+
expect(await inputs[1].isDisabled()).toBe(true);
62+
});
63+
64+
it('should be able to get type of input', async () => {
65+
const inputs = await loader.getAllHarnesses(MatInputHarness);
66+
expect(inputs.length).toBe(3);
67+
68+
expect(await inputs[0].getType()).toBe('text');
69+
expect(await inputs[1].getType()).toBe('number');
70+
expect(await inputs[2].getType()).toBe('textarea');
71+
72+
fixture.componentInstance.inputType = 'text';
73+
74+
expect(await inputs[1].getType()).toBe('text');
75+
});
76+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatInputHarness
5+
*/
6+
@Component({
7+
selector: 'input-harness-example',
8+
templateUrl: 'input-harness-example.html'
9+
})
10+
export class InputHarnessExample {
11+
inputType = 'number';
12+
disabled = false;
13+
}

0 commit comments

Comments
 (0)