User-event: Document how to delete text with type method

Created on 20 Feb 2020  路  4Comments  路  Source: testing-library/user-event

The change to the type event to append to existing values has broken our tests which are written expecting to replace existing values.

Is it possible to document how to clear/delete existing values? I tried using the \b character but this resulted in some very strange behavior (I imagine this is not being translated until the test reporting.)

Most helpful comment

All 4 comments

The only reliable way I have at the moment is to clear using fireEvent.

e.g. fireEvent.change(getByTestId('input-element'), { target: { value: '' } });

One other thing I tried was to use userEvent.dblClickto attempt to select all text - but alas this did not work, perhaps this could be a nice addition?

A way this library is doing it in its own test files is by calling input.value = '';

https://github.com/testing-library/user-event/blob/38e3c227dd228c6ccc7624683eca3fae119a66e3/__tests__/react/type.js#L194

By calling that, you can basically simulate previous behavior.

If it's really important to test full user interaction, it would require writing custom selectAll/clear method I guess.

Referenced PR https://github.com/testing-library/user-event/pull/221

Slightly related #232 (type should support replacing the current input)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Etheryte picture Etheryte  路  3Comments

sarahdayan picture sarahdayan  路  6Comments

kentcdodds picture kentcdodds  路  5Comments

dvargas92495 picture dvargas92495  路  4Comments

wKovacs64 picture wKovacs64  路  6Comments