-
Notifications
You must be signed in to change notification settings - Fork 723
docs: update user-event references #1030
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
Changes from 6 commits
c3a01d8
c9e37cf
3467846
9f694d6
d41b75d
b6c9149
de52ec8
64a0316
2c22fcf
bcfca98
2c58bf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,10 @@ import TabItem from '@theme/TabItem' | |
advanced simulation of browser interactions than the built-in | ||
[`fireEvent`](dom-testing-library/api-events.mdx#fireevent) method. | ||
|
||
> This page describes `[email protected]`. | ||
If you are starting or actively working on a project, | ||
we recommend to use [`[email protected]`](user-event/intro.mdx) instead, as it includes important bug fixes and new features. | ||
> This page describes `[email protected]`. | ||
> This version is no longer maintained. Please use | ||
> [`user-event@14`](user-event/intro.mdx) instead, as it includes important bug | ||
> fixes and new features. | ||
|
||
## Installation | ||
|
||
|
@@ -222,7 +223,7 @@ import {render, screen} from '@testing-library/react' | |
import userEvent from '@testing-library/user-event' | ||
|
||
test('prepend text', () => { | ||
render(<input defaultValue="World!"/>) | ||
render(<input defaultValue="World!" />) | ||
const element = screen.getByRole('textbox') | ||
|
||
// Prepend text | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,12 +63,13 @@ import {MyForm} from './myForm.js' | |
test('rendering and submitting a basic Formik form', async () => { | ||
const handleSubmit = jest.fn() | ||
render(<MyForm onSubmit={handleSubmit} />) | ||
const user = userEvent.setup() | ||
|
||
userEvent.type(screen.getByLabelText(/first name/i), 'John') | ||
userEvent.type(screen.getByLabelText(/last name/i), 'Dee') | ||
userEvent.type(screen.getByLabelText(/email/i), '[email protected]') | ||
await user.type(screen.getByLabelText(/first name/i), 'John') | ||
await user.type(screen.getByLabelText(/last name/i), 'Dee') | ||
await user.type(screen.getByLabelText(/email/i), '[email protected]') | ||
|
||
userEvent.click(screen.getByRole('button', {name: /submit/i})) | ||
await user.click(screen.getByRole('button', {name: /submit/i})) | ||
|
||
await waitFor(() => | ||
expect(handleSubmit).toHaveBeenCalledWith({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ some of the project's other libraries that can help you along the way: | |
|
||
- **[@testing-library/jest-dom](https://github.com/testing-library/jest-dom)**: | ||
`jest-dom` provides a set of custom Jest matchers that you can use to extend | ||
Jest. These make your tests more declarative, clearer to read, and easier | ||
to maintain. | ||
Jest. These make your tests more declarative, clearer to read, and easier to | ||
maintain. | ||
|
||
- **[@testing-library/user-event](https://github.com/testing-library/user-event):** | ||
`user-event` tries to simulate the real events that happen in the browser as | ||
|
@@ -120,8 +120,8 @@ test this component: | |
|
||
The following component gets a `name` from `props` and shows a welcome message | ||
in an `h1` element. It also has a text input which users can change to a | ||
different name, and the template updates accordingly. Check the live version | ||
on [CodeSandbox](https://codesandbox.io/s/ecstatic-hellman-fh7in). | ||
different name, and the template updates accordingly. Check the live version on | ||
[CodeSandbox](https://codesandbox.io/s/ecstatic-hellman-fh7in). | ||
|
||
```jsx | ||
const Welcome = props => { | ||
|
@@ -247,15 +247,15 @@ of the query documentation might help you understand the concepts better. | |
> A `<form>` element must have a `name` attribute in order to have an implicit | ||
> `role` of `'form'` (as required by the specification). | ||
|
||
React Testing Library aims to test the components how users use them. Users | ||
see buttons, headings, forms and other elements by their role, not by their | ||
`id`, `class`, or element tag name. Therefore, when you use React Testing | ||
Library you should avoid accessing the DOM with the `document.querySelector` | ||
API. (You _can_ use it in your tests, but it's not recommended for the reasons | ||
stated in this paragraph.) | ||
React Testing Library aims to test the components how users use them. Users see | ||
buttons, headings, forms and other elements by their role, not by their `id`, | ||
`class`, or element tag name. Therefore, when you use React Testing Library you | ||
should avoid accessing the DOM with the `document.querySelector` API. (You _can_ | ||
use it in your tests, but it's not recommended for the reasons stated in this | ||
paragraph.) | ||
|
||
React Testing Library exposes some handy query APIs which help you access | ||
the component elements efficiently. You can see the | ||
React Testing Library exposes some handy query APIs which help you access the | ||
component elements efficiently. You can see the | ||
[list of available queries here](queries/about.mdx#types-of-queries). If you're | ||
not sure which query you should use in a given situation, we have a great page | ||
which explains [which query to use](queries/about.mdx#priority), so check it | ||
|
@@ -330,10 +330,14 @@ input's "checked" property is properly set. Let's see how we might write a test | |
for that case: | ||
|
||
```jsx | ||
test('handles click correctly', () => { | ||
test('handles click correctly', async () => { | ||
render(<Checkbox />) | ||
const user = userEvent.setup() | ||
|
||
// You can also call this method directly on userEvent, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that's even something we want to "share". If we believe that the recommended approach is to use the methods returned from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it eases the transition for people who are used to the previous APIs. That's why the Direct APIs still exist. If there is only a single API call, it makes no difference, as the Direct APIs call If it helps or hurts to mention it in the example - I don't know. |
||
// but using the methods from `.setup()` is recommended. | ||
await user.click(screen.getByText('Check')) | ||
|
||
userEvent.click(screen.getByText('Check')) | ||
expect(screen.getByLabelText('Check')).toBeChecked() | ||
}) | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of making this a warning https://docusaurus.io/docs/markdown-features/admonitions?
To me, this doesn't stand out and could be overlooked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I converted those blockquotes on the intro into admonitions. :)