Skip to content

Commit 67db1a6

Browse files
committed
fix(material/form-field): allow getting harness by validity (#26232)
* fix(material/form-field): allow getting harness by validity * fixup! fix(material/form-field): allow getting harness by validity (cherry picked from commit 46a4db2)
1 parent fee5d0b commit 67db1a6

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

src/material/form-field/testing/form-field-harness-filters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ export interface FormFieldHarnessFilters extends BaseHarnessFilters {
1414
floatingLabelText?: string | RegExp;
1515
/** Filters based on whether the form field has error messages. */
1616
hasErrors?: boolean;
17+
/** Filters based on whether the form field value is valid. */
18+
isValid?: boolean;
1719
}

src/material/form-field/testing/form-field-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ export class MatFormFieldHarness extends _MatFormFieldHarnessBase<
248248
'hasErrors',
249249
options.hasErrors,
250250
async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors,
251+
)
252+
.addOption(
253+
'isValid',
254+
options.isValid,
255+
async (harness, isValid) => (await harness.isControlValid()) === isValid,
251256
);
252257
}
253258

src/material/form-field/testing/shared.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ export function runHarnessTests(
197197
);
198198
});
199199

200+
it('should be able to get form-field by validity', async () => {
201+
let invalid = await loader.getAllHarnesses(formFieldHarness.with({isValid: false}));
202+
expect(invalid.length).toBe(0);
203+
204+
fixture.componentInstance.requiredControl.setValue('');
205+
dispatchFakeEvent(fixture.nativeElement.querySelector('#with-errors input'), 'blur');
206+
207+
invalid = await loader.getAllHarnesses(formFieldHarness.with({isValid: false}));
208+
expect(invalid.length).toBe(1);
209+
});
210+
200211
it('should be able to get error harnesses from the form-field harness', async () => {
201212
const formFields = await loader.getAllHarnesses(formFieldHarness);
202213
expect(await formFields[1].getErrors()).toEqual([]);

src/material/legacy-form-field/testing/form-field-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export class MatLegacyFormFieldHarness extends _MatFormFieldHarnessBase<
5757
'hasErrors',
5858
options.hasErrors,
5959
async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors,
60+
)
61+
.addOption(
62+
'isValid',
63+
options.isValid,
64+
async (harness, isValid) => (await harness.isControlValid()) === isValid,
6065
);
6166
}
6267

tools/public_api_guard/material/form-field-testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type FormFieldControlHarness = MatInputHarness | MatSelectHarness | MatDa
2828
export interface FormFieldHarnessFilters extends BaseHarnessFilters {
2929
floatingLabelText?: string | RegExp;
3030
hasErrors?: boolean;
31+
isValid?: boolean;
3132
}
3233

3334
// @public

0 commit comments

Comments
 (0)