Skip to content

Commit 4d0a517

Browse files
committed
fix(material/input): input harness selector not matching matNativeControl
The input harness currently only matches `[matInput]`, but does not match `[matNativeControl]` usages. Since both names, resolve to the same directive, we should ensure that the harness works for both possible usages.
1 parent 97a7e2b commit 4d0a517

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {InputHarnessFilters} from './input-harness-filters';
1212

1313
/** Harness for interacting with a standard Material inputs in tests. */
1414
export class MatInputHarness extends MatFormFieldControlHarness {
15-
static hostSelector = '[matInput]';
15+
static hostSelector = '.mat-input-element';
1616

1717
/**
1818
* Gets a `HarnessPredicate` that can be used to search for a `MatInputHarness` that meets

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function runHarnessTests(
2828

2929
it('should load all input harnesses', async () => {
3030
const inputs = await loader.getAllHarnesses(inputHarness);
31-
expect(inputs.length).toBe(3);
31+
expect(inputs.length).toBe(4);
3232
});
3333

3434
it('should load input with specific id', async () => {
@@ -49,48 +49,55 @@ export function runHarnessTests(
4949

5050
it('should be able to get id of input', async () => {
5151
const inputs = await loader.getAllHarnesses(inputHarness);
52-
expect(inputs.length).toBe(3);
52+
expect(inputs.length).toBe(4);
5353
expect(await inputs[0].getId()).toMatch(/mat-input-\d+/);
5454
expect(await inputs[1].getId()).toMatch(/mat-input-\d+/);
5555
expect(await inputs[2].getId()).toBe('myTextarea');
56+
expect(await inputs[3].getId()).toBe('nativeControl');
5657
});
5758

5859
it('should be able to get name of input', async () => {
5960
const inputs = await loader.getAllHarnesses(inputHarness);
60-
expect(inputs.length).toBe(3);
61+
expect(inputs.length).toBe(4);
6162
expect(await inputs[0].getName()).toBe('favorite-food');
6263
expect(await inputs[1].getName()).toBe('');
6364
expect(await inputs[2].getName()).toBe('');
65+
expect(await inputs[3].getName()).toBe('');
6466
});
6567

6668
it('should be able to get value of input', async () => {
6769
const inputs = await loader.getAllHarnesses(inputHarness);
68-
expect(inputs.length).toBe(3);
70+
expect(inputs.length).toBe(4);
6971
expect(await inputs[0].getValue()).toBe('Sushi');
7072
expect(await inputs[1].getValue()).toBe('');
7173
expect(await inputs[2].getValue()).toBe('');
74+
expect(await inputs[3].getValue()).toBe('');
7275
});
7376

7477
it('should be able to set value of input', async () => {
7578
const inputs = await loader.getAllHarnesses(inputHarness);
76-
expect(inputs.length).toBe(3);
79+
expect(inputs.length).toBe(4);
7780
expect(await inputs[0].getValue()).toBe('Sushi');
7881
expect(await inputs[1].getValue()).toBe('');
82+
expect(await inputs[3].getValue()).toBe('');
7983

8084
await inputs[0].setValue('');
8185
await inputs[2].setValue('new-value');
86+
await inputs[3].setValue('new-value');
8287

8388
expect(await inputs[0].getValue()).toBe('');
8489
expect(await inputs[2].getValue()).toBe('new-value');
90+
expect(await inputs[3].getValue()).toBe('new-value');
8591
});
8692

8793
it('should be able to get disabled state', async () => {
8894
const inputs = await loader.getAllHarnesses(inputHarness);
89-
expect(inputs.length).toBe(3);
95+
expect(inputs.length).toBe(4);
9096

9197
expect(await inputs[0].isDisabled()).toBe(false);
9298
expect(await inputs[1].isDisabled()).toBe(false);
9399
expect(await inputs[2].isDisabled()).toBe(false);
100+
expect(await inputs[3].isDisabled()).toBe(false);
94101

95102
fixture.componentInstance.disabled = true;
96103

@@ -99,11 +106,12 @@ export function runHarnessTests(
99106

100107
it('should be able to get readonly state', async () => {
101108
const inputs = await loader.getAllHarnesses(inputHarness);
102-
expect(inputs.length).toBe(3);
109+
expect(inputs.length).toBe(4);
103110

104111
expect(await inputs[0].isReadonly()).toBe(false);
105112
expect(await inputs[1].isReadonly()).toBe(false);
106113
expect(await inputs[2].isReadonly()).toBe(false);
114+
expect(await inputs[3].isReadonly()).toBe(false);
107115

108116
fixture.componentInstance.readonly = true;
109117

@@ -112,11 +120,12 @@ export function runHarnessTests(
112120

113121
it('should be able to get required state', async () => {
114122
const inputs = await loader.getAllHarnesses(inputHarness);
115-
expect(inputs.length).toBe(3);
123+
expect(inputs.length).toBe(4);
116124

117125
expect(await inputs[0].isRequired()).toBe(false);
118126
expect(await inputs[1].isRequired()).toBe(false);
119127
expect(await inputs[2].isRequired()).toBe(false);
128+
expect(await inputs[3].isRequired()).toBe(false);
120129

121130
fixture.componentInstance.required = true;
122131

@@ -125,18 +134,20 @@ export function runHarnessTests(
125134

126135
it('should be able to get placeholder of input', async () => {
127136
const inputs = await loader.getAllHarnesses(inputHarness);
128-
expect(inputs.length).toBe(3);
137+
expect(inputs.length).toBe(4);
129138
expect(await inputs[0].getPlaceholder()).toBe('Favorite food');
130139
expect(await inputs[1].getPlaceholder()).toBe('');
131140
expect(await inputs[2].getPlaceholder()).toBe('Leave a comment');
141+
expect(await inputs[3].getPlaceholder()).toBe('Native control');
132142
});
133143

134144
it('should be able to get type of input', async () => {
135145
const inputs = await loader.getAllHarnesses(inputHarness);
136-
expect(inputs.length).toBe(3);
146+
expect(inputs.length).toBe(4);
137147
expect(await inputs[0].getType()).toBe('text');
138148
expect(await inputs[1].getType()).toBe('number');
139149
expect(await inputs[2].getType()).toBe('textarea');
150+
expect(await inputs[3].getType()).toBe('text');
140151

141152
fixture.componentInstance.inputType = 'text';
142153

@@ -180,6 +191,10 @@ function getActiveElementTagName() {
180191
<mat-form-field>
181192
<textarea id="myTextarea" matInput placeholder="Leave a comment"></textarea>
182193
</mat-form-field>
194+
195+
<mat-form-field>
196+
<input matNativeControl placeholder="Native control" id="nativeControl">
197+
</mat-form-field>
183198
`
184199
})
185200
class InputHarnessTest {

0 commit comments

Comments
 (0)