Skip to content

Commit

Permalink
fix(material/form-field): allow getting harness by validity (#26232)
Browse files Browse the repository at this point in the history
* fix(material/form-field): allow getting harness by validity

* fixup! fix(material/form-field): allow getting harness by validity

(cherry picked from commit 46a4db2)
  • Loading branch information
mmalerba committed Dec 13, 2022
1 parent fee5d0b commit 67db1a6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/material/form-field/testing/form-field-harness-filters.ts
Expand Up @@ -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. */
isValid?: boolean;
}
5 changes: 5 additions & 0 deletions src/material/form-field/testing/form-field-harness.ts
Expand Up @@ -248,6 +248,11 @@ export class MatFormFieldHarness extends _MatFormFieldHarnessBase<
'hasErrors',
options.hasErrors,
async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors,
)
.addOption(
'isValid',
options.isValid,
async (harness, isValid) => (await harness.isControlValid()) === isValid,
);
}

Expand Down
11 changes: 11 additions & 0 deletions src/material/form-field/testing/shared.spec.ts
Expand Up @@ -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({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({isValid: 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([]);
Expand Down
5 changes: 5 additions & 0 deletions src/material/legacy-form-field/testing/form-field-harness.ts
Expand Up @@ -57,6 +57,11 @@ export class MatLegacyFormFieldHarness extends _MatFormFieldHarnessBase<
'hasErrors',
options.hasErrors,
async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors,
)
.addOption(
'isValid',
options.isValid,
async (harness, isValid) => (await harness.isControlValid()) === isValid,
);
}

Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/form-field-testing.md
Expand Up @@ -28,6 +28,7 @@ export type FormFieldControlHarness = MatInputHarness | MatSelectHarness | MatDa
export interface FormFieldHarnessFilters extends BaseHarnessFilters {
floatingLabelText?: string | RegExp;
hasErrors?: boolean;
isValid?: boolean;
}

// @public
Expand Down

0 comments on commit 67db1a6

Please sign in to comment.