Skip to content

Commit af35066

Browse files
authored
docs: update docs for user-event from readme (#892)
* docs: update API docs from user-event * docs: move install section to docs * docs: remove content already in readme * docs: add Tabs for user-event's installation
1 parent 9ae3bf1 commit af35066

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

docs/ecosystem-user-event.mdx

+42-33
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ id: ecosystem-user-event
33
title: user-event
44
---
55

6+
import Tabs from '@theme/Tabs'
7+
import TabItem from '@theme/TabItem'
8+
69
[`user-event`][gh] is a companion library for Testing Library that provides more
710
advanced simulation of browser interactions than the built-in
811
[`fireEvent`](dom-testing-library/api-events.mdx#fireevent) method.
912

10-
[`user-event` on GitHub][gh]
13+
## Installation
1114

12-
## Install
15+
<Tabs defaultValue="npm" values={[{ label: 'npm', value: 'npm' }, { label: 'Yarn', value: 'yarn' }]}>
16+
<TabItem value="npm">
1317

14-
```
18+
```sh
1519
npm install --save-dev @testing-library/user-event @testing-library/dom
1620
```
1721

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
2035
import userEvent from '@testing-library/user-event'
2136

22-
test('types inside textarea', () => {
23-
document.body.innerHTML = `<textarea />`
37+
// or
2438

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')
2840
```
2941

3042
## API
@@ -69,7 +81,9 @@ See the
6981
constructor documentation for more options.
7082

7183
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.
7387

7488
### `dblClick(element, eventInit, options)`
7589

@@ -113,10 +127,6 @@ are typed. By default it's 0. You can use this option if your component has a
113127
different behavior for fast or slow users. If you do this, you need to make sure
114128
to `await`!
115129

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-
120130
`type` will click the element before typing. To disable this, set the
121131
`skipClick` option to `true`.
122132

@@ -136,6 +146,8 @@ The following special character strings are supported:
136146
| `{arrowright}` | ArrowRight | N/A | |
137147
| `{arrowup}` | ArrowUp | N/A | |
138148
| `{arrowdown}` | ArrowDown | N/A | |
149+
| `{home}` | Home | N/A | |
150+
| `{end}` | End | N/A | |
139151
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
140152
| `{ctrl}` | Control | `ctrlKey` | |
141153
| `{alt}` | Alt | `altKey` | |
@@ -184,18 +196,15 @@ The following is an example of usage of this library with
184196
`<input type="time" />`
185197

186198
```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'
189201
import userEvent from '@testing-library/user-event'
190202

191203
test('types into the input', () => {
192204
render(
193205
<>
194206
<label for="time">Enter a time</label>
195-
<input
196-
type="time"
197-
id="time"
198-
/>
207+
<input type="time" id="time" />
199208
</>
200209
)
201210
const input = screen.getByLabelText(/enter a time/i)
@@ -261,7 +270,7 @@ userEvent.keyboard('a', { keyboardState }) // press [KeyA] with active ctrlKey m
261270
```
262271

263272
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)
265274
portraying a "default" US-keyboard. You can provide your own local keyboard
266275
mapping per option.
267276

@@ -274,12 +283,15 @@ userEvent.keyboard('?', { keyboardMap: myOwnLocaleKeyboardMap })
274283
> CapsLock is not active and `A` is referenced. If you don't wish this behavior,
275284
> you can pass `autoModify: false` when using `userEvent.keyboard` in your code.
276285
277-
### `upload(element, file, [{ clickInit, changeInit }])`
286+
### `upload(element, file, [{ clickInit, changeInit }], [options])`
278287

279288
Uploads file to an `<input>`. For uploading multiple files use `<input>` with
280289
the `multiple` attribute and the second `upload` argument as an array. It's also
281290
possible to initialize a click or change event using a third argument.
282291

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+
283295
```jsx
284296
import React from 'react'
285297
import { render, screen } from '@testing-library/react'
@@ -527,15 +539,17 @@ method.
527539

528540
| Key | Character |
529541
| ---------- | -------------- |
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}` |
534548
| enter | `{enter}` |
535549
| escape | `{esc}` |
536550
| delete | `{del}` |
537551
| backspace | `{backspace}` |
538-
| selectAll | `{selectAll}` |
552+
| selectAll | `{selectall}` |
539553
| space | `{space}` |
540554
| whitespace | `' '` |
541555

@@ -561,9 +575,4 @@ test('delete characters within the selectedRange', () => {
561575
})
562576
```
563577

564-
## Known limitations
565-
566-
- No `<input type="color" />` support.
567-
[#423](https://github.com/testing-library/user-event/issues/423#issuecomment-669368863)
568-
569578
[gh]: https://github.com/testing-library/user-event

0 commit comments

Comments
 (0)