@@ -3,28 +3,40 @@ id: ecosystem-user-event
3
3
title : user-event
4
4
---
5
5
6
+ import Tabs from ' @theme/Tabs'
7
+ import TabItem from ' @theme/TabItem'
8
+
6
9
[ ` user-event ` ] [ gh ] is a companion library for Testing Library that provides more
7
10
advanced simulation of browser interactions than the built-in
8
11
[ ` fireEvent ` ] ( dom-testing-library/api-events.mdx#fireevent ) method.
9
12
10
- [ ` user-event ` on GitHub ] [ gh ]
13
+ ## Installation
11
14
12
- ## Install
15
+ <Tabs defaultValue = " npm" values = { [{ label: ' npm' , value: ' npm' }, { label: ' Yarn' , value: ' yarn' }]} >
16
+ <TabItem value = " npm" >
13
17
14
- ```
18
+ ``` sh
15
19
npm install --save-dev @testing-library/user-event @testing-library/dom
16
20
```
17
21
18
- ``` jsx
19
- import { screen } from ' @testing-library/dom'
22
+ </TabItem >
23
+ <TabItem value = " yarn" >
24
+
25
+ ``` sh
26
+ yarn add --dev @testing-library/user-event @testing-library/dom
27
+ ```
28
+
29
+ </TabItem >
30
+ </Tabs >
31
+
32
+ Now simply import it in your tests:
33
+
34
+ ``` js
20
35
import userEvent from ' @testing-library/user-event'
21
36
22
- test (' types inside textarea' , () => {
23
- document .body .innerHTML = ` <textarea />`
37
+ // or
24
38
25
- userEvent .type (screen .getByRole (' textbox' ), ' Hello, World!' )
26
- expect (screen .getByRole (' textbox' )).toHaveValue (' Hello, World!' )
27
- })
39
+ const { default: userEvent } = require (' @testing-library/user-event' )
28
40
```
29
41
30
42
## API
@@ -69,7 +81,9 @@ See the
69
81
constructor documentation for more options.
70
82
71
83
Note that ` click ` will trigger hover events before clicking. To disable this,
72
- set the ` skipHover ` option to ` true ` .
84
+ set the ` skipHover ` option to ` true ` . Also note that trying to click an element
85
+ with ` pointer-events ` being set to ` "none" ` (i.e. unclickable) will throw an
86
+ error.
73
87
74
88
### ` dblClick(element, eventInit, options) `
75
89
@@ -113,10 +127,6 @@ are typed. By default it's 0. You can use this option if your component has a
113
127
different behavior for fast or slow users. If you do this, you need to make sure
114
128
to ` await ` !
115
129
116
- > To be clear, ` userEvent.type ` _ always_ returns a promise, but you _ only_ need
117
- > to ` await ` the promise it returns if you're using the ` delay ` option.
118
- > Otherwise everything runs synchronously and you can ignore the promise.
119
-
120
130
` type ` will click the element before typing. To disable this, set the
121
131
` skipClick ` option to ` true ` .
122
132
@@ -136,6 +146,8 @@ The following special character strings are supported:
136
146
| ` {arrowright} ` | ArrowRight | N/A | |
137
147
| ` {arrowup} ` | ArrowUp | N/A | |
138
148
| ` {arrowdown} ` | ArrowDown | N/A | |
149
+ | ` {home} ` | Home | N/A | |
150
+ | ` {end} ` | End | N/A | |
139
151
| ` {shift} ` | Shift | ` shiftKey ` | Does ** not** capitalize following characters. |
140
152
| ` {ctrl} ` | Control | ` ctrlKey ` | |
141
153
| ` {alt} ` | Alt | ` altKey ` | |
@@ -184,18 +196,15 @@ The following is an example of usage of this library with
184
196
` <input type="time" /> `
185
197
186
198
``` jsx
187
- import React from ' react
188
- import {render, screen} from ' @testing-library/react'
199
+ import React from ' react'
200
+ import { render , screen } from ' @testing-library/react'
189
201
import userEvent from ' @testing-library/user-event'
190
202
191
203
test (' types into the input' , () => {
192
204
render (
193
205
<>
194
206
< label for = " time" > Enter a time< / label>
195
- <input
196
- type="time"
197
- id="time"
198
- />
207
+ < input type= " time" id= " time" / >
199
208
< / >
200
209
)
201
210
const input = screen .getByLabelText (/ enter a time/ i )
@@ -261,7 +270,7 @@ userEvent.keyboard('a', { keyboardState }) // press [KeyA] with active ctrlKey m
261
270
```
262
271
263
272
The mapping of ` key ` to ` code ` is performed by a
264
- [default key map](https://github.com/testing-library/user-event/blob/master /src/keyboard/keyMap.ts)
273
+ [ default key map] ( https://github.com/testing-library/user-event/blob/main /src/keyboard/keyMap.ts )
265
274
portraying a "default" US-keyboard. You can provide your own local keyboard
266
275
mapping per option.
267
276
@@ -274,12 +283,15 @@ userEvent.keyboard('?', { keyboardMap: myOwnLocaleKeyboardMap })
274
283
> CapsLock is not active and ` A ` is referenced. If you don't wish this behavior,
275
284
> you can pass ` autoModify: false ` when using ` userEvent.keyboard ` in your code.
276
285
277
- ### ` upload(element, file, [{ clickInit, changeInit }])`
286
+ ### ` upload(element, file, [{ clickInit, changeInit }], [options] ) `
278
287
279
288
Uploads file to an ` <input> ` . For uploading multiple files use ` <input> ` with
280
289
the ` multiple ` attribute and the second ` upload ` argument as an array. It's also
281
290
possible to initialize a click or change event using a third argument.
282
291
292
+ If ` options.applyAccept ` is set to ` true ` and there is an ` accept ` attribute on
293
+ the element, files that don't match will be discarded.
294
+
283
295
``` jsx
284
296
import React from ' react'
285
297
import { render , screen } from ' @testing-library/react'
@@ -527,15 +539,17 @@ method.
527
539
528
540
| Key | Character |
529
541
| ---------- | -------------- |
530
- | arrowLeft | `{arrowLeft}` |
531
- | arrowRight | `{arrowRight}` |
532
- | arrowDown | `{arrowDown}` |
533
- | arrowUp | `{arrowUp}` |
542
+ | arrowLeft | ` {arrowleft} ` |
543
+ | arrowRight | ` {arrowright} ` |
544
+ | arrowDown | ` {arrowdown} ` |
545
+ | arrowUp | ` {arrowup} ` |
546
+ | home | ` {home} ` |
547
+ | end | ` {end} ` |
534
548
| enter | ` {enter} ` |
535
549
| escape | ` {esc} ` |
536
550
| delete | ` {del} ` |
537
551
| backspace | ` {backspace} ` |
538
- | selectAll | `{selectAll }` |
552
+ | selectAll | ` {selectall } ` |
539
553
| space | ` {space} ` |
540
554
| whitespace | ` ' ' ` |
541
555
@@ -561,9 +575,4 @@ test('delete characters within the selectedRange', () => {
561
575
})
562
576
```
563
577
564
- ## Known limitations
565
-
566
- - No `<input type="color" />` support.
567
- [#423](https://github.com/testing-library/user-event/issues/423#issuecomment-669368863)
568
-
569
578
[ gh ] : https://github.com/testing-library/user-event
0 commit comments