Skip to content

Commit 662f56c

Browse files
committed
Update quickstart example
1 parent 0a54b10 commit 662f56c

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

docs/react-testing-library/example-intro.mdx

+11-16
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@ sidebar_label: Example
66

77
## Quickstart
88

9-
This is a minimal setup to get you started.
9+
This is a very simple setup to get you started.
1010
If you want to see a description of what each line does,
1111
scroll down to the [annotated version](#quickstart-annotated).
1212
Scroll down to [Full Example](#full-example) to see a more advanced test setup.
1313

1414
```jsx
15-
import {render, waitFor, screen} from '@testing-library/react'
15+
import { render, screen } from '@testing-library/react'
1616
import userEvent from '@testing-library/user-event'
1717
import '@testing-library/jest-dom'
18-
import Fetch from '../fetch'
19-
20-
const setup = (jsx)=> ({ user: userEvent.setup(), ...render(jsx) })
18+
import Fetch from './fetch'
2119

2220
test('loads and displays greeting', async () => {
2321

2422
// ARRANGE
25-
const { user } = setup(<Fetch url="/greeting" />)
23+
render(<Fetch url="/greeting" />)
2624

2725
// ACT
28-
await user.click(screen.getByText('Load Greeting'))
26+
await userEvent.click(screen.getByText('Load Greeting'))
2927
await screen.findByRole('heading')
3028

3129
// ASSERT
@@ -40,23 +38,20 @@ test('loads and displays greeting', async () => {
4038

4139
```jsx
4240
// import react-testing methods
43-
import {render, waitFor, screen} from '@testing-library/react'
41+
import { render, screen } from '@testing-library/react'
42+
// userEvent library simulates user interactions by dispatching the events that would happen if the interaction took place in a browser.
4443
import userEvent from '@testing-library/user-event'
4544
// add custom jest matchers from jest-dom
4645
import '@testing-library/jest-dom'
4746
// the component to test
48-
import Fetch from '../fetch'
49-
50-
// Apply workarounds and mock the UI layer to simulate user interactions
51-
// like they would happen in the browser
52-
const setup = (jsx)=> ({ user: userEvent.setup(), ...render(jsx) })
47+
import Fetch from './fetch'
5348

5449
test('loads and displays greeting', async () => {
5550

56-
// Run the UI setup and render a React element into the DOM
57-
const { user } = setup(<Fetch url="/greeting" />)
51+
// Render a React element into the DOM
52+
render(<Fetch url="/greeting" />)
5853

59-
await user.click(screen.getByText('Load Greeting'))
54+
await userEvent.click(screen.getByText('Load Greeting'))
6055
// wait before throwing an error if it cannot find an element
6156
await screen.findByRole('heading')
6257

0 commit comments

Comments
 (0)