Skip to content

Commit 01ec8cd

Browse files
committed
docs(material/input): add harness example for input
1 parent 71b7b15 commit 01ec8cd

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<mat-form-field>
2+
<input matInput placeholder="Favorite food" value="Sushi" name="favorite-food">
3+
</mat-form-field>
4+
5+
<mat-form-field>
6+
<input matInput [type]="inputType"
7+
[disabled]="disabled">
8+
</mat-form-field>
9+
10+
<mat-form-field>
11+
<textarea id="myTextarea" matInput placeholder="Leave a comment"></textarea>
12+
</mat-form-field>
13+
14+
<mat-form-field>
15+
<input matNativeControl placeholder="Native control" id="nativeControl">
16+
</mat-form-field>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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(4);
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(4);
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+
expect(await inputs[3].isDisabled()).toBe(false);
59+
60+
fixture.componentInstance.disabled = true;
61+
62+
expect(await inputs[1].isDisabled()).toBe(true);
63+
});
64+
65+
it('should be able to get type of input', async () => {
66+
const inputs = await loader.getAllHarnesses(MatInputHarness);
67+
expect(inputs.length).toBe(4);
68+
69+
expect(await inputs[0].getType()).toBe('text');
70+
expect(await inputs[1].getType()).toBe('number');
71+
expect(await inputs[2].getType()).toBe('textarea');
72+
expect(await inputs[3].getType()).toBe('text');
73+
74+
fixture.componentInstance.inputType = 'text';
75+
76+
expect(await inputs[1].getType()).toBe('text');
77+
});
78+
});
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+
@Component({
4+
selector: 'input-harness-example',
5+
templateUrl: 'input-harness-example.html'
6+
})
7+
export class InputHarnessExample {
8+
inputType = 'number';
9+
disabled = false;
10+
}

0 commit comments

Comments
 (0)