@@ -52,16 +52,17 @@ change the state of the checkbox.
52
52
53
53
- [ Installation] ( #installation )
54
54
- [ 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 )
58
58
- [ ` upload(element, file, [{ clickInit, changeInit }]) ` ] ( #uploadelement-file--clickinit-changeinit- )
59
59
- [ ` clear(element) ` ] ( #clearelement )
60
60
- [ ` selectOptions(element, values) ` ] ( #selectoptionselement-values )
61
- - [ ` toggleSelectOptions (element, values)` ] ( #toggleselectoptionselement -values )
61
+ - [ ` deselectOptions (element, values)` ] ( #deselectoptionselement -values )
62
62
- [ ` 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 )
65
66
- [ Issues] ( #issues )
66
67
- [ Contributors ✨] ( #contributors- )
67
68
- [ LICENSE] ( #license )
@@ -94,7 +95,7 @@ var userEvent = require('@testing-library/user-event')
94
95
95
96
## API
96
97
97
- ### ` click(element) `
98
+ ### ` click(element, eventInit, options ) `
98
99
99
100
Clicks ` element ` , depending on what ` element ` is it can have different side
100
101
effects.
@@ -127,7 +128,10 @@ See the
127
128
[ ` MouseEvent ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent )
128
129
constructor documentation for more options.
129
130
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) `
131
135
132
136
Clicks ` element ` twice, depending on what ` element ` is it can have different
133
137
side effects.
@@ -147,7 +151,7 @@ test('double click', () => {
147
151
})
148
152
```
149
153
150
- ### ` async type(element, text, [options])`
154
+ ### ` type(element, text, [options]) `
151
155
152
156
Writes ` text ` inside an ` <input> ` or a ` <textarea> ` .
153
157
@@ -156,38 +160,43 @@ import React from 'react'
156
160
import {render , screen } from ' @testing-library/react'
157
161
import userEvent from ' @testing-library/user-event'
158
162
159
- test (' type' , async () => {
163
+ test (' type' , () => {
160
164
render (< textarea / > )
161
165
162
- await userEvent .type (screen .getByRole (' textbox' ), ' Hello,{enter}World!' )
166
+ userEvent .type (screen .getByRole (' textbox' ), ' Hello,{enter}World!' )
163
167
expect (screen .getByRole (' textbox' )).toHaveValue (' Hello,\n World!' )
164
168
})
165
169
```
166
170
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
-
170
171
` options.delay ` is the number of milliseconds that pass between two characters
171
172
are 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 ` .
173
178
174
179
#### Special characters
175
180
176
181
The following special character strings are supported:
177
182
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 ` | |
187
194
188
195
> ** A note about modifiers:** Modifier keys (` {shift} ` , ` {ctrl} ` , ` {alt} ` ,
189
196
> ` {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 ` ).
191
200
192
201
<!-- space out these notes -->
193
202
@@ -308,16 +317,17 @@ userEvent.selectOptions(screen.getByTestId('select-multiple'), [
308
317
])
309
318
```
310
319
311
- ### ` toggleSelectOptions (element, values)`
320
+ ### ` deselectOptions (element, values)`
312
321
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.
314
324
315
325
``` jsx
316
326
import * as React from ' react'
317
327
import {render , screen } from ' @testing-library/react'
318
328
import userEvent from ' @testing-library/user-event'
319
329
320
- test (' toggleSelectOptions ' , () => {
330
+ test (' deselectOptions ' , () => {
321
331
render (
322
332
< select multiple>
323
333
< option value= " 1" > A < / option>
@@ -326,14 +336,12 @@ test('toggleSelectOptions', () => {
326
336
< / select> ,
327
337
)
328
338
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'])
337
345
})
338
346
```
339
347
@@ -397,7 +405,7 @@ it('should cycle elements in document tab order', () => {
397
405
})
398
406
```
399
407
400
- ### ` async hover(element)`
408
+ ### ` hover(element) `
401
409
402
410
Hovers over ` element ` .
403
411
@@ -407,26 +415,43 @@ import {render, screen} from '@testing-library/react'
407
415
import userEvent from ' @testing-library/user-event'
408
416
import Tooltip from ' ../tooltip'
409
417
410
- test (' hover' , async () => {
418
+ test (' hover' , () => {
411
419
const messageText = ' Hello'
412
420
render (
413
421
< Tooltip messageText= {messageText}>
414
422
< TrashIcon aria- label= " Delete" / >
415
423
< / Tooltip> ,
416
424
)
417
425
418
- await userEvent .hover (screen .getByLabelText (/ delete/ i ))
426
+ userEvent .hover (screen .getByLabelText (/ delete/ i ))
419
427
expect (screen .getByText (messageText)).toBeInTheDocument ()
420
- await userEvent .unhover (screen .getByLabelText (/ delete/ i ))
428
+ userEvent .unhover (screen .getByLabelText (/ delete/ i ))
421
429
expect (screen .queryByText (messageText)).not .toBeInTheDocument ()
422
430
})
423
431
```
424
432
425
- ### ` async unhover(element)`
433
+ ### ` unhover(element) `
426
434
427
435
Unhovers out of ` element ` .
428
436
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 ` ).
430
455
431
456
## Issues
432
457
@@ -503,6 +528,7 @@ Thanks goes to these people ([emoji key][emojis]):
503
528
504
529
<!-- markdownlint-enable -->
505
530
<!-- prettier-ignore-end -->
531
+
506
532
<!-- ALL-CONTRIBUTORS-LIST:END -->
507
533
508
534
This project follows the [ all-contributors] [ all-contributors ] specification.
0 commit comments