|
28 | 28 | as the user interacts with it. For example `userEvent.click(checkbox)` would
|
29 | 29 | change the state of the checkbox.
|
30 | 30 |
|
31 |
| -The library is still a work in progress and any help is appreciated. |
| 31 | +**The library is still a work in progress and any help is appreciated.** |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +With NPM: |
| 36 | + |
| 37 | +`npm install user-event --dev` |
| 38 | + |
| 39 | +With Yarn: |
| 40 | + |
| 41 | +`yarn add user-event --dev` |
| 42 | + |
| 43 | +Now simply import it in your tests: |
| 44 | + |
| 45 | +```js |
| 46 | +import userEvent from "user-event"; |
| 47 | +// or |
| 48 | +var userEvent = require("user-event"); |
| 49 | +``` |
| 50 | + |
| 51 | +## API |
| 52 | + |
| 53 | +### `click(element)` |
| 54 | + |
| 55 | +Clicks `element`, depending on what `element` is it can have different side |
| 56 | +effects. |
| 57 | + |
| 58 | +```jsx |
| 59 | +import React from "react"; |
| 60 | +import { render } from "react-testing-library"; |
| 61 | +import userEvent from "user-event"; |
| 62 | + |
| 63 | +const { getByText, getByTestId } = test("click", () => { |
| 64 | + render( |
| 65 | + <div> |
| 66 | + <label htmlFor="checkbox">Check</label> |
| 67 | + <input id="checkbox" data-testid="checkbox" type="checkbox" /> |
| 68 | + </div> |
| 69 | + ); |
| 70 | +}); |
| 71 | + |
| 72 | +userEvent.click(getByText("Check")); |
| 73 | +expect(getByTestId("checkbox")).toHaveAttribute("checked", true); |
| 74 | +``` |
| 75 | + |
| 76 | +### `type(element, text, [options])` |
| 77 | + |
| 78 | +Writes `text` inside an `<input>` or a `<textarea>`. `options` accepts one |
| 79 | +argument `allAtOnce` which is `false` by default. If it's set to `true` it will |
| 80 | +write `text` at once rather than one character at the time. |
| 81 | + |
| 82 | +```jsx |
| 83 | +import React from "react"; |
| 84 | +import { render } from "react-testing-library"; |
| 85 | +import userEvent from "user-event"; |
| 86 | + |
| 87 | +const { getByText } = test("click", () => { |
| 88 | + render(<textarea data-testid="email" />); |
| 89 | +}); |
| 90 | + |
| 91 | +userEvent.type(getByTestId("email"), "Hello, World!"); |
| 92 | +expect(getByTestId("email")).toHaveAttribute("value", "Hello, World!"); |
| 93 | +``` |
0 commit comments