Skip to content

Commit 20e5ea9

Browse files
authored
fix(input/testing): filtering by regex not working (#20148)
The types of the `value` and `placeholder` filters allow both a string and a regex, but passing in a regex doesn't actually work because we were comparing the values directly instead of going through `stringMatches`.
1 parent 8eb6754 commit 20e5ea9

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/material/input/testing/input-harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export class MatInputHarness extends MatFormFieldControlHarness {
2525
*/
2626
static with(options: InputHarnessFilters = {}): HarnessPredicate<MatInputHarness> {
2727
return new HarnessPredicate(MatInputHarness, options)
28-
.addOption('value', options.value, async (harness, value) => {
29-
return (await harness.getValue()) === value;
28+
.addOption('value', options.value, (harness, value) => {
29+
return HarnessPredicate.stringMatches(harness.getValue(), value);
3030
})
31-
.addOption('placeholder', options.placeholder, async (harness, placeholder) => {
32-
return (await harness.getPlaceholder()) === placeholder;
31+
.addOption('placeholder', options.placeholder, (harness, placeholder) => {
32+
return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
3333
});
3434
}
3535

src/material/input/testing/shared.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,28 @@ export function runHarnessTests(
4343
expect(inputs.length).toBe(1);
4444
});
4545

46-
it('should load input with specific value', async () => {
46+
it('should load input with a specific value', async () => {
4747
const inputs = await loader.getAllHarnesses(inputHarness.with({value: 'Sushi'}));
4848
expect(inputs.length).toBe(1);
4949
});
5050

51+
it('should load input with a value that matches a regex', async () => {
52+
const inputs = await loader.getAllHarnesses(inputHarness.with({value: /shi$/}));
53+
expect(inputs.length).toBe(1);
54+
expect(await inputs[0].getValue()).toBe('Sushi');
55+
});
56+
57+
it('should load input with a specific placeholder', async () => {
58+
const inputs = await loader.getAllHarnesses(inputHarness.with({placeholder: 'Favorite food'}));
59+
expect(inputs.length).toBe(1);
60+
});
61+
62+
it('should load input with a placeholder that matches a regex', async () => {
63+
const inputs = await loader.getAllHarnesses(inputHarness.with({placeholder: / food$/}));
64+
expect(inputs.length).toBe(1);
65+
expect(await inputs[0].getPlaceholder()).toBe('Favorite food');
66+
});
67+
5168
it('should be able to get id of input', async () => {
5269
const inputs = await loader.getAllHarnesses(inputHarness);
5370
expect(inputs.length).toBe(6);

0 commit comments

Comments
 (0)