user-event is a companion library for Testing Library that provides more
advanced simulation of browser interactions than the built-in
fireEvent method.
Note: All userEvent methods are synchronous with one exception: when delay
option used with userEvent.type as described below. We also discourage using
userEvent inside before/after blocks at all, for important reasons described
in
"Avoid Nesting When You're Testing".
options.delay is the number of milliseconds that pass between two characters
are typed. By default it's 0. You can use this option if your component has a
different behavior for fast or slow users. If you do this, you need to make sure
to await!
To be clear, userEvent.typealways returns a promise, but you only need
to await the promise it returns if you're using the delay option.
Otherwise everything runs synchronously and you can ignore the promise.
type will click the element before typing. To disable this, set the
skipClick option to true.
The following special character strings are supported:
Text string
Key
Modifier
Notes
{enter}
Enter
N/A
Will insert a newline character (<textarea /> only).
{space}
' '
N/A
{esc}
Escape
N/A
{backspace}
Backspace
N/A
Will delete the previous character (or the characters within the selectedRange, see example below).
{del}
Delete
N/A
Will delete the next character (or the characters within the selectedRange, see example below)
{selectall}
N/A
N/A
Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not email, password, number, among others)
{arrowleft}
ArrowLeft
N/A
{arrowright}
ArrowRight
N/A
{arrowup}
ArrowUp
N/A
{arrowdown}
ArrowDown
N/A
{shift}
Shift
shiftKey
Does not capitalize following characters.
{ctrl}
Control
ctrlKey
{alt}
Alt
altKey
{meta}
OS
metaKey
{capslock}
CapsLock
modifierCapsLock
Fires both keydown and keyup when used (simulates a user clicking their "Caps Lock" button to enable caps lock).
A note about modifiers: Modifier keys ({shift}, {ctrl}, {alt},
{meta}) will activate their corresponding event modifiers for the duration
of type command or until they are closed (via {/shift}, {/ctrl}, etc.). If
they are not closed explicitly, then events will be fired to close them
automatically (to disable this, set the skipAutoClose option to true).
We take the same
stance as Cypress
in that we do not simulate the behavior that happens with modifier key
combinations as different operating systems function differently in this
regard.
Uploads file to an <input>. For uploading multiple files use <input> with
multiple attribute and the second upload argument must be array then. Also
it's possible to initialize click or change event with using third argument.
Fires a tab event changing the document.activeElement in the same way the
browser does.
Options:
shift (default false) can be true or false to invert tab direction.
focusTrap (default document) a container element to restrict the tabbing
within.
A note about tab:
jsdom does not support tabbing,
so this feature is a way to enable tests to verify tabbing from the end user's
perspective. However, this limitation in jsdom will mean that components like
focus-trap-react will not
work with userEvent.tab() or jsdom. For that reason, the focusTrap option
is available to let you ensure your user is restricted within a focus-trap.