Skip to content

Commit 1df54cc

Browse files
authored
docs(material/form-field): add harness example for form-field (#21471)
1 parent 988c945 commit 1df54cc

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

src/components-examples/material/form-field/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/form-field",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/checkbox",
1618
"//src/material/form-field",
19+
"//src/material/form-field/testing",
1720
"//src/material/icon",
1821
"//src/material/input",
22+
"//src/material/input/testing",
1923
"//src/material/radio",
2024
"//src/material/select",
2125
"@npm//@angular/forms",
26+
"@npm//@angular/platform-browser",
27+
"@npm//@angular/platform-browser-dynamic",
28+
"@npm//@types/jasmine",
2229
],
2330
)
2431

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<mat-form-field id="with-errors">
2+
<span class="custom-control">Custom control harness</span>
3+
<input matInput [formControl]="requiredControl">
4+
5+
<mat-error>Error</mat-error>
6+
<mat-hint align="start">Hint</mat-hint>
7+
</mat-form-field>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatFormFieldHarness} from '@angular/material/form-field/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatFormFieldModule} from '@angular/material/form-field';
8+
import {FormFieldHarnessExample} from './form-field-harness-example';
9+
import {MatInputModule} from '@angular/material/input';
10+
import {ReactiveFormsModule} from '@angular/forms';
11+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
12+
import {MatInputHarness} from '@angular/material/input/testing';
13+
14+
describe('FormFieldHarnessExample', () => {
15+
let fixture: ComponentFixture<FormFieldHarnessExample>;
16+
let loader: HarnessLoader;
17+
18+
beforeAll(() => {
19+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
20+
});
21+
22+
beforeEach(
23+
waitForAsync(() => {
24+
TestBed.configureTestingModule({
25+
imports: [MatFormFieldModule, MatInputModule, ReactiveFormsModule, NoopAnimationsModule],
26+
declarations: [FormFieldHarnessExample]
27+
}).compileComponents();
28+
fixture = TestBed.createComponent(FormFieldHarnessExample);
29+
fixture.detectChanges();
30+
loader = TestbedHarnessEnvironment.loader(fixture);
31+
})
32+
);
33+
34+
it('should be able to load harnesses', async () => {
35+
const formFields = await loader.getAllHarnesses(MatFormFieldHarness);
36+
expect(formFields.length).toBe(1);
37+
});
38+
39+
it('should be able to get control of form-field', async () => {
40+
const formField = await loader.getHarness(MatFormFieldHarness);
41+
expect(await formField.getControl() instanceof MatInputHarness).toBe(true);
42+
});
43+
44+
it('should be able to get error messages and hints of form-field', async () => {
45+
const formField = await loader.getHarness(MatFormFieldHarness);
46+
expect(await formField.getTextErrors()).toEqual([]);
47+
expect(await formField.getTextHints()).toEqual(['Hint']);
48+
49+
fixture.componentInstance.requiredControl.setValue('');
50+
(await formField.getControl())?.blur();
51+
expect(await formField.getTextErrors()).toEqual(['Error 1']);
52+
expect(await formField.getTextHints()).toEqual([]);
53+
});
54+
55+
it('should be able to check if form field is invalid', async () => {
56+
const formField = await loader.getHarness(MatFormFieldHarness);
57+
expect(await formField.isControlValid()).toBe(true);
58+
59+
fixture.componentInstance.requiredControl.setValue('');
60+
expect(await formField.isControlValid()).toBe(false);
61+
});
62+
});
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+
import {FormControl, Validators} from '@angular/forms';
3+
4+
/**
5+
* @title Testing with MatFormFieldHarness
6+
*/
7+
@Component({
8+
selector: 'form-field-harness-example',
9+
templateUrl: 'form-field-harness-example.html',
10+
})
11+
export class FormFieldHarnessExample {
12+
requiredControl = new FormControl('Initial value', [Validators.required]);
13+
}

src/components-examples/material/form-field/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import {
2121
FormFieldPrefixSuffixExample
2222
} from './form-field-prefix-suffix/form-field-prefix-suffix-example';
2323
import {FormFieldThemingExample} from './form-field-theming/form-field-theming-example';
24+
import {FormFieldHarnessExample} from './form-field-harness/form-field-harness-example';
2425

2526
export {
2627
FormFieldAppearanceExample,
2728
FormFieldCustomControlExample,
2829
FormFieldErrorExample,
30+
FormFieldHarnessExample,
2931
FormFieldHintExample,
3032
FormFieldLabelExample,
3133
FormFieldOverviewExample,
@@ -38,6 +40,7 @@ const EXAMPLES = [
3840
FormFieldAppearanceExample,
3941
FormFieldCustomControlExample,
4042
FormFieldErrorExample,
43+
FormFieldHarnessExample,
4144
FormFieldHintExample,
4245
FormFieldLabelExample,
4346
FormFieldOverviewExample,

0 commit comments

Comments
 (0)