Skip to content

docs: update docs for user-event from readme #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 21, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions docs/ecosystem-user-event.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ title: user-event
advanced simulation of browser interactions than the built-in
[`fireEvent`](dom-testing-library/api-events.mdx#fireevent) method.

[`user-event` on GitHub][gh]
## Installation

## Install
With NPM:

```
```sh
npm install --save-dev @testing-library/user-event @testing-library/dom
```

```jsx
import { screen } from '@testing-library/dom'
With Yarn:

```sh
yarn add --dev @testing-library/user-event @testing-library/dom
```

Now simply import it in your tests:

```js
import userEvent from '@testing-library/user-event'

test('types inside textarea', () => {
document.body.innerHTML = `<textarea />`
// or

userEvent.type(screen.getByRole('textbox'), 'Hello, World!')
expect(screen.getByRole('textbox')).toHaveValue('Hello, World!')
})
const { default: userEvent } = require('@testing-library/user-event')
```

## API
Expand Down Expand Up @@ -69,7 +73,9 @@ See the
constructor documentation for more options.

Note that `click` will trigger hover events before clicking. To disable this,
set the `skipHover` option to `true`.
set the `skipHover` option to `true`. Also note that trying to click an element
with `pointer-events` being set to `"none"` (i.e. unclickable) will throw an
error.

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

Expand Down Expand Up @@ -113,10 +119,6 @@ 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.type` _always_ 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`.

Expand All @@ -136,6 +138,8 @@ The following special character strings are supported:
| `{arrowright}` | ArrowRight | N/A | |
| `{arrowup}` | ArrowUp | N/A | |
| `{arrowdown}` | ArrowDown | N/A | |
| `{home}` | Home | N/A | |
| `{end}` | End | N/A | |
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
| `{ctrl}` | Control | `ctrlKey` | |
| `{alt}` | Alt | `altKey` | |
Expand Down Expand Up @@ -184,18 +188,15 @@ The following is an example of usage of this library with
`<input type="time" />`

```jsx
import React from 'react
import {render, screen} from '@testing-library/react'
import React from 'react'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'

test('types into the input', () => {
render(
<>
<label for="time">Enter a time</label>
<input
type="time"
id="time"
/>
<input type="time" id="time" />
</>
)
const input = screen.getByLabelText(/enter a time/i)
Expand Down Expand Up @@ -261,7 +262,7 @@ userEvent.keyboard('a', { keyboardState }) // press [KeyA] with active ctrlKey m
```

The mapping of `key` to `code` is performed by a
[default key map](https://github.com/testing-library/user-event/blob/master/src/keyboard/keyMap.ts)
[default key map](https://github.com/testing-library/user-event/blob/main/src/keyboard/keyMap.ts)
portraying a "default" US-keyboard. You can provide your own local keyboard
mapping per option.

Expand All @@ -274,12 +275,15 @@ userEvent.keyboard('?', { keyboardMap: myOwnLocaleKeyboardMap })
> CapsLock is not active and `A` is referenced. If you don't wish this behavior,
> you can pass `autoModify: false` when using `userEvent.keyboard` in your code.

### `upload(element, file, [{ clickInit, changeInit }])`
### `upload(element, file, [{ clickInit, changeInit }], [options])`

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

If `options.applyAccept` is set to `true` and there is an `accept` attribute on
the element, files that don't match will be discarded.

```jsx
import React from 'react'
import { render, screen } from '@testing-library/react'
Expand Down Expand Up @@ -527,15 +531,17 @@ method.

| Key | Character |
| ---------- | -------------- |
| arrowLeft | `{arrowLeft}` |
| arrowRight | `{arrowRight}` |
| arrowDown | `{arrowDown}` |
| arrowUp | `{arrowUp}` |
| arrowLeft | `{arrowleft}` |
| arrowRight | `{arrowright}` |
| arrowDown | `{arrowdown}` |
| arrowUp | `{arrowup}` |
| home | `{home}` |
| end | `{end}` |
| enter | `{enter}` |
| escape | `{esc}` |
| delete | `{del}` |
| backspace | `{backspace}` |
| selectAll | `{selectAll}` |
| selectAll | `{selectall}` |
| space | `{space}` |
| whitespace | `' '` |

Expand All @@ -561,9 +567,4 @@ test('delete characters within the selectedRange', () => {
})
```

## Known limitations

- No `<input type="color" />` support.
[#423](https://github.com/testing-library/user-event/issues/423#issuecomment-669368863)

[gh]: https://github.com/testing-library/user-event