From 3475e30dae97ab410bddca565b6c75396784b4d8 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Fri, 9 Dec 2022 22:37:04 +0000 Subject: [PATCH 1/2] fix(material/form-field): allow getting harness by validity --- .../form-field/testing/form-field-harness-filters.ts | 2 ++ src/material/form-field/testing/form-field-harness.ts | 5 +++++ src/material/form-field/testing/shared.spec.ts | 11 +++++++++++ .../legacy-form-field/testing/form-field-harness.ts | 5 +++++ 4 files changed, 23 insertions(+) diff --git a/src/material/form-field/testing/form-field-harness-filters.ts b/src/material/form-field/testing/form-field-harness-filters.ts index 64809f875db5..f0039ea64df4 100644 --- a/src/material/form-field/testing/form-field-harness-filters.ts +++ b/src/material/form-field/testing/form-field-harness-filters.ts @@ -14,4 +14,6 @@ export interface FormFieldHarnessFilters extends BaseHarnessFilters { floatingLabelText?: string | RegExp; /** Filters based on whether the form field has error messages. */ hasErrors?: boolean; + /** Filters based on whether the form field value is valid. */ + valid?: boolean; } diff --git a/src/material/form-field/testing/form-field-harness.ts b/src/material/form-field/testing/form-field-harness.ts index cd523d483061..448841c05b96 100644 --- a/src/material/form-field/testing/form-field-harness.ts +++ b/src/material/form-field/testing/form-field-harness.ts @@ -248,6 +248,11 @@ export class MatFormFieldHarness extends _MatFormFieldHarnessBase< 'hasErrors', options.hasErrors, async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors, + ) + .addOption( + 'valid', + options.valid, + async (harness, valid) => (await harness.isControlValid()) === valid, ); } diff --git a/src/material/form-field/testing/shared.spec.ts b/src/material/form-field/testing/shared.spec.ts index a0379870a745..81ae034766f5 100644 --- a/src/material/form-field/testing/shared.spec.ts +++ b/src/material/form-field/testing/shared.spec.ts @@ -197,6 +197,17 @@ export function runHarnessTests( ); }); + it('should be able to get form-field by validity', async () => { + let invalid = await loader.getAllHarnesses(formFieldHarness.with({valid: false})); + expect(invalid.length).toBe(0); + + fixture.componentInstance.requiredControl.setValue(''); + dispatchFakeEvent(fixture.nativeElement.querySelector('#with-errors input'), 'blur'); + + invalid = await loader.getAllHarnesses(formFieldHarness.with({valid: false})); + expect(invalid.length).toBe(1); + }); + it('should be able to get error harnesses from the form-field harness', async () => { const formFields = await loader.getAllHarnesses(formFieldHarness); expect(await formFields[1].getErrors()).toEqual([]); diff --git a/src/material/legacy-form-field/testing/form-field-harness.ts b/src/material/legacy-form-field/testing/form-field-harness.ts index a38344ddc094..8431a6ab20b9 100644 --- a/src/material/legacy-form-field/testing/form-field-harness.ts +++ b/src/material/legacy-form-field/testing/form-field-harness.ts @@ -57,6 +57,11 @@ export class MatLegacyFormFieldHarness extends _MatFormFieldHarnessBase< 'hasErrors', options.hasErrors, async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors, + ) + .addOption( + 'valid', + options.valid, + async (harness, valid) => (await harness.isControlValid()) === valid, ); } From 7b2e72860709982a9df006c39b4b2a187a232a3b Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Mon, 12 Dec 2022 22:34:57 +0000 Subject: [PATCH 2/2] fixup! fix(material/form-field): allow getting harness by validity --- .../form-field/testing/form-field-harness-filters.ts | 2 +- src/material/form-field/testing/form-field-harness.ts | 6 +++--- src/material/form-field/testing/shared.spec.ts | 4 ++-- .../legacy-form-field/testing/form-field-harness.ts | 6 +++--- tools/public_api_guard/material/form-field-testing.md | 1 + 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/material/form-field/testing/form-field-harness-filters.ts b/src/material/form-field/testing/form-field-harness-filters.ts index f0039ea64df4..ead796b38adf 100644 --- a/src/material/form-field/testing/form-field-harness-filters.ts +++ b/src/material/form-field/testing/form-field-harness-filters.ts @@ -15,5 +15,5 @@ export interface FormFieldHarnessFilters extends BaseHarnessFilters { /** Filters based on whether the form field has error messages. */ hasErrors?: boolean; /** Filters based on whether the form field value is valid. */ - valid?: boolean; + isValid?: boolean; } diff --git a/src/material/form-field/testing/form-field-harness.ts b/src/material/form-field/testing/form-field-harness.ts index 448841c05b96..3793c5f4a4ba 100644 --- a/src/material/form-field/testing/form-field-harness.ts +++ b/src/material/form-field/testing/form-field-harness.ts @@ -250,9 +250,9 @@ export class MatFormFieldHarness extends _MatFormFieldHarnessBase< async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors, ) .addOption( - 'valid', - options.valid, - async (harness, valid) => (await harness.isControlValid()) === valid, + 'isValid', + options.isValid, + async (harness, isValid) => (await harness.isControlValid()) === isValid, ); } diff --git a/src/material/form-field/testing/shared.spec.ts b/src/material/form-field/testing/shared.spec.ts index 81ae034766f5..b4e5e0a786f9 100644 --- a/src/material/form-field/testing/shared.spec.ts +++ b/src/material/form-field/testing/shared.spec.ts @@ -198,13 +198,13 @@ export function runHarnessTests( }); it('should be able to get form-field by validity', async () => { - let invalid = await loader.getAllHarnesses(formFieldHarness.with({valid: false})); + let invalid = await loader.getAllHarnesses(formFieldHarness.with({isValid: false})); expect(invalid.length).toBe(0); fixture.componentInstance.requiredControl.setValue(''); dispatchFakeEvent(fixture.nativeElement.querySelector('#with-errors input'), 'blur'); - invalid = await loader.getAllHarnesses(formFieldHarness.with({valid: false})); + invalid = await loader.getAllHarnesses(formFieldHarness.with({isValid: false})); expect(invalid.length).toBe(1); }); diff --git a/src/material/legacy-form-field/testing/form-field-harness.ts b/src/material/legacy-form-field/testing/form-field-harness.ts index 8431a6ab20b9..535763a5cdc7 100644 --- a/src/material/legacy-form-field/testing/form-field-harness.ts +++ b/src/material/legacy-form-field/testing/form-field-harness.ts @@ -59,9 +59,9 @@ export class MatLegacyFormFieldHarness extends _MatFormFieldHarnessBase< async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors, ) .addOption( - 'valid', - options.valid, - async (harness, valid) => (await harness.isControlValid()) === valid, + 'isValid', + options.isValid, + async (harness, isValid) => (await harness.isControlValid()) === isValid, ); } diff --git a/tools/public_api_guard/material/form-field-testing.md b/tools/public_api_guard/material/form-field-testing.md index 2e92006fbc4c..63bc03460e3c 100644 --- a/tools/public_api_guard/material/form-field-testing.md +++ b/tools/public_api_guard/material/form-field-testing.md @@ -28,6 +28,7 @@ export type FormFieldControlHarness = MatInputHarness | MatSelectHarness | MatDa export interface FormFieldHarnessFilters extends BaseHarnessFilters { floatingLabelText?: string | RegExp; hasErrors?: boolean; + isValid?: boolean; } // @public