Skip to content

Commit

Permalink
fix(material/form-field): allow getting harness by validity
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Dec 9, 2022
1 parent 03264d8 commit 3475e30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/material/form-field/testing/form-field-harness-filters.ts
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. */
valid?: 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(
'valid',
options.valid,
async (harness, valid) => (await harness.isControlValid()) === valid,
);
}

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({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([]);
Expand Down
5 changes: 5 additions & 0 deletions src/material/legacy-form-field/testing/form-field-harness.ts
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(
'valid',
options.valid,
async (harness, valid) => (await harness.isControlValid()) === valid,
);
}

Expand Down

0 comments on commit 3475e30

Please sign in to comment.