@@ -52,16 +52,17 @@ change the state of the checkbox.
5252
5353- [ Installation] ( #installation )
5454- [ API] ( #api )
55- - [ ` click(element) ` ] ( #clickelement )
56- - [ ` dblClick(element) ` ] ( #dblclickelement )
57- - [ ` async type(element, text, [options])` ] ( #async- typeelement-text-options )
55+ - [ ` click(element, eventInit, options ) ` ] ( #clickelement-eventinit-options )
56+ - [ ` dblClick(element, eventInit, options ) ` ] ( #dblclickelement-eventinit-options )
57+ - [ ` type(element, text, [options]) ` ] ( #typeelement-text-options )
5858 - [ ` upload(element, file, [{ clickInit, changeInit }]) ` ] ( #uploadelement-file--clickinit-changeinit- )
5959 - [ ` clear(element) ` ] ( #clearelement )
6060 - [ ` selectOptions(element, values) ` ] ( #selectoptionselement-values )
61- - [ ` toggleSelectOptions (element, values)` ] ( #toggleselectoptionselement -values )
61+ - [ ` deselectOptions (element, values)` ] ( #deselectoptionselement -values )
6262 - [ ` tab({shift, focusTrap}) ` ] ( #tabshift-focustrap )
63- - [ ` async hover(element) ` ] ( #async-hoverelement )
64- - [ ` async unhover(element) ` ] ( #async-unhoverelement )
63+ - [ ` hover(element) ` ] ( #hoverelement )
64+ - [ ` unhover(element) ` ] ( #unhoverelement )
65+ - [ ` paste(element, text, eventInit, options) ` ] ( #pasteelement-text-eventinit-options )
6566- [ Issues] ( #issues )
6667- [ Contributors ✨] ( #contributors- )
6768- [ LICENSE] ( #license )
@@ -94,7 +95,7 @@ var userEvent = require('@testing-library/user-event')
9495
9596## API
9697
97- ### ` click(element) `
98+ ### ` click(element, eventInit, options ) `
9899
99100Clicks ` element ` , depending on what ` element ` is it can have different side
100101effects.
@@ -127,7 +128,10 @@ See the
127128[ ` MouseEvent ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent )
128129constructor documentation for more options.
129130
130- ### ` dblClick(element) `
131+ Note that ` click ` will trigger hover events before clicking. To disable this,
132+ set the ` skipHover ` option to ` true ` .
133+
134+ ### ` dblClick(element, eventInit, options) `
131135
132136Clicks ` element ` twice, depending on what ` element ` is it can have different
133137side effects.
@@ -147,7 +151,7 @@ test('double click', () => {
147151})
148152```
149153
150- ### ` async type(element, text, [options])`
154+ ### ` type(element, text, [options]) `
151155
152156Writes ` text ` inside an ` <input> ` or a ` <textarea> ` .
153157
@@ -156,38 +160,43 @@ import React from 'react'
156160import {render , screen } from ' @testing-library/react'
157161import userEvent from ' @testing-library/user-event'
158162
159- test (' type' , async () => {
163+ test (' type' , () => {
160164 render (< textarea / > )
161165
162- await userEvent .type (screen .getByRole (' textbox' ), ' Hello,{enter}World!' )
166+ userEvent .type (screen .getByRole (' textbox' ), ' Hello,{enter}World!' )
163167 expect (screen .getByRole (' textbox' )).toHaveValue (' Hello,\n World!' )
164168})
165169```
166170
167- If ` options.allAtOnce ` is ` true ` , ` type ` will write ` text ` at once rather than
168- one character at the time. ` false ` is the default value.
169-
170171` options.delay ` is the number of milliseconds that pass between two characters
171172are typed. By default it's 0. You can use this option if your component has a
172- different behavior for fast or slow users.
173+ different behavior for fast or slow users. If you do this, you need to make sure
174+ to ` await ` !
175+
176+ ` type ` will click the element before typing. To disable this, set the
177+ ` skipClick ` option to ` true ` .
173178
174179#### Special characters
175180
176181The following special character strings are supported:
177182
178- | Text string | Key | Modifier | Notes |
179- | ------------- | --------- | ---------- | ---------------------------------------------------------------------------------- |
180- | ` {enter} ` | Enter | N/A | Will insert a newline character (` <textarea /> ` only). |
181- | ` {esc} ` | Escape | N/A | |
182- | ` {backspace} ` | Backspace | N/A | Will delete the previous character (or the characters within the ` selectedRange ` ). |
183- | ` {shift} ` | Shift | ` shiftKey ` | Does ** not** capitalize following characters. |
184- | ` {ctrl} ` | Control | ` ctrlKey ` | |
185- | ` {alt} ` | Alt | ` altKey ` | |
186- | ` {meta} ` | OS | ` metaKey ` | |
183+ | Text string | Key | Modifier | Notes |
184+ | ------------- | --------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
185+ | ` {enter} ` | Enter | N/A | Will insert a newline character (` <textarea /> ` only). |
186+ | ` {esc} ` | Escape | N/A | |
187+ | ` {backspace} ` | Backspace | N/A | Will delete the previous character (or the characters within the ` selectedRange ` ). |
188+ | ` {del} ` | Delete | N/A | Will delete the next character (or the characters within the ` selectedRange ` ) |
189+ | ` {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) |
190+ | ` {shift} ` | Shift | ` shiftKey ` | Does ** not** capitalize following characters. |
191+ | ` {ctrl} ` | Control | ` ctrlKey ` | |
192+ | ` {alt} ` | Alt | ` altKey ` | |
193+ | ` {meta} ` | OS | ` metaKey ` | |
187194
188195> ** A note about modifiers:** Modifier keys (` {shift} ` , ` {ctrl} ` , ` {alt} ` ,
189196> ` {meta} ` ) will activate their corresponding event modifiers for the duration
190- > of type command or until they are closed (via ` {/shift} ` , ` {/ctrl} ` , etc.).
197+ > of type command or until they are closed (via ` {/shift} ` , ` {/ctrl} ` , etc.). If
198+ > they are not closed explicitly, then events will be fired to close them
199+ > automatically (to disable this, set the ` skipAutoClose ` option to ` true ` ).
191200
192201<!-- space out these notes -->
193202
@@ -308,16 +317,17 @@ userEvent.selectOptions(screen.getByTestId('select-multiple'), [
308317])
309318```
310319
311- ### ` toggleSelectOptions (element, values)`
320+ ### ` deselectOptions (element, values)`
312321
313- Toggle the specified option(s) of a ` <select multiple> ` element.
322+ Remove the selection for the specified option(s) of a ` <select multiple> `
323+ element.
314324
315325``` jsx
316326import * as React from ' react'
317327import {render , screen } from ' @testing-library/react'
318328import userEvent from ' @testing-library/user-event'
319329
320- test (' toggleSelectOptions ' , () => {
330+ test (' deselectOptions ' , () => {
321331 render (
322332 < select multiple>
323333 < option value= " 1" > A < / option>
@@ -326,14 +336,12 @@ test('toggleSelectOptions', () => {
326336 < / select> ,
327337 )
328338
329- userEvent .toggleSelectOptions (screen .getByRole (' listbox' ), [' 1' , ' 3' ])
330-
331- expect (screen .getByText (' A' ).selected ).toBe (true )
332- expect (screen .getByText (' C' ).selected ).toBe (true )
333-
334- userEvent .toggleSelectOptions (screen .getByRole (' listbox' ), [' 1' ])
335-
336- expect (screen .getByText (' A' ).selected ).toBe (false )
339+ userEvent .selectOptions (screen .getByRole (' listbox' ), ' 2' )
340+ expect (screen .getByText (' B' ).selected ).toBe (true )
341+ userEvent .deselectOptions (screen .getByRole (' listbox' ), ' 2' )
342+ expect (screen .getByText (' B' ).selected ).toBe (false )
343+ // can do multiple at once as well:
344+ // userEvent.deselectOptions(screen.getByRole('listbox'), ['1', '2'])
337345})
338346```
339347
@@ -397,7 +405,7 @@ it('should cycle elements in document tab order', () => {
397405})
398406```
399407
400- ### ` async hover(element)`
408+ ### ` hover(element) `
401409
402410Hovers over ` element ` .
403411
@@ -407,26 +415,43 @@ import {render, screen} from '@testing-library/react'
407415import userEvent from ' @testing-library/user-event'
408416import Tooltip from ' ../tooltip'
409417
410- test (' hover' , async () => {
418+ test (' hover' , () => {
411419 const messageText = ' Hello'
412420 render (
413421 < Tooltip messageText= {messageText}>
414422 < TrashIcon aria- label= " Delete" / >
415423 < / Tooltip> ,
416424 )
417425
418- await userEvent .hover (screen .getByLabelText (/ delete/ i ))
426+ userEvent .hover (screen .getByLabelText (/ delete/ i ))
419427 expect (screen .getByText (messageText)).toBeInTheDocument ()
420- await userEvent .unhover (screen .getByLabelText (/ delete/ i ))
428+ userEvent .unhover (screen .getByLabelText (/ delete/ i ))
421429 expect (screen .queryByText (messageText)).not .toBeInTheDocument ()
422430})
423431```
424432
425- ### ` async unhover(element)`
433+ ### ` unhover(element) `
426434
427435Unhovers out of ` element ` .
428436
429- > See [ above] ( #async-hoverelement ) for an example
437+ > See [ above] ( #hoverelement ) for an example
438+
439+ ### ` paste(element, text, eventInit, options) `
440+
441+ Allows you to simulate the user pasting some text into an input.
442+
443+ ``` javascript
444+ test (' should paste text in input' , () => {
445+ render (< MyInput / > )
446+
447+ const text = ' Hello, world!'
448+ userEvent .paste (getByRole (' textbox' , {name: / paste your greeting/ i }), text)
449+ expect (element).toHaveValue (text)
450+ })
451+ ```
452+
453+ You can use the ` eventInit ` if what you're pasting should have ` clipboardData `
454+ (like ` files ` ).
430455
431456## Issues
432457
@@ -503,6 +528,7 @@ Thanks goes to these people ([emoji key][emojis]):
503528
504529<!-- markdownlint-enable -->
505530<!-- prettier-ignore-end -->
531+
506532<!-- ALL-CONTRIBUTORS-LIST:END -->
507533
508534This project follows the [ all-contributors] [ all-contributors ] specification.
0 commit comments