Skip to content

Commit ce4416f

Browse files
fix: type with empty value should clear the input (#50)
1 parent ef97beb commit ce4416f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

projects/testing-library/src/lib/user-events/type.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function createType(fireEvent: FireFunction & FireObject) {
4040
const { allAtOnce = false, delay = 0 } = options || {};
4141
const initialValue = (element as HTMLInputElement).value;
4242

43-
if (allAtOnce) {
43+
if (allAtOnce || value === '') {
4444
fireEvent.input(element, { target: { value } });
4545
element.addEventListener('blur', createFireChangeEvent(initialValue));
4646
return;

projects/testing-library/tests/user-events/type.spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('options', () => {
178178
});
179179
});
180180

181-
test('should not type when event.preventDefault() is called', async () => {
181+
test('does not type when event.preventDefault() is called', async () => {
182182
@Component({
183183
selector: 'fixture',
184184
template: `
@@ -219,3 +219,27 @@ test('should not type when event.preventDefault() is called', async () => {
219219

220220
expect(inputControl.value).toBe('');
221221
});
222+
223+
test('can clear an input field', async () => {
224+
@Component({
225+
selector: 'fixture',
226+
template: `
227+
<input type="text" data-testid="input" [value]="initialValue" />
228+
`,
229+
})
230+
class FixtureComponent {
231+
@Input() initialValue = '';
232+
}
233+
234+
const component = await render(FixtureComponent, {
235+
componentProperties: {
236+
initialValue: 'an initial value',
237+
},
238+
});
239+
240+
const inputControl = component.getByTestId('input') as HTMLInputElement;
241+
expect(inputControl.value).toBe('an initial value');
242+
243+
component.type(inputControl, '');
244+
expect(inputControl.value).toBe('');
245+
});

0 commit comments

Comments
 (0)