Skip to content

fix(material/form-field): allow getting harness by validity #26232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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