Skip to content

Commit

Permalink
fix: type with empty value should clear the input (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Sep 20, 2019
1 parent ef97beb commit ce4416f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion projects/testing-library/src/lib/user-events/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createType(fireEvent: FireFunction & FireObject) {
const { allAtOnce = false, delay = 0 } = options || {};
const initialValue = (element as HTMLInputElement).value;

if (allAtOnce) {
if (allAtOnce || value === '') {
fireEvent.input(element, { target: { value } });
element.addEventListener('blur', createFireChangeEvent(initialValue));
return;
Expand Down
26 changes: 25 additions & 1 deletion projects/testing-library/tests/user-events/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('options', () => {
});
});

test('should not type when event.preventDefault() is called', async () => {
test('does not type when event.preventDefault() is called', async () => {
@Component({
selector: 'fixture',
template: `
Expand Down Expand Up @@ -219,3 +219,27 @@ test('should not type when event.preventDefault() is called', async () => {

expect(inputControl.value).toBe('');
});

test('can clear an input field', async () => {
@Component({
selector: 'fixture',
template: `
<input type="text" data-testid="input" [value]="initialValue" />
`,
})
class FixtureComponent {
@Input() initialValue = '';
}

const component = await render(FixtureComponent, {
componentProperties: {
initialValue: 'an initial value',
},
});

const inputControl = component.getByTestId('input') as HTMLInputElement;
expect(inputControl.value).toBe('an initial value');

component.type(inputControl, '');
expect(inputControl.value).toBe('');
});

0 comments on commit ce4416f

Please sign in to comment.