Skip to content

Commit b96ab69

Browse files
authored
Update based on PR recommendations & comments
1 parent b20b50a commit b96ab69

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
],
66
"files.associations": {
77
"*.md": "mdx"
8-
}
8+
},
9+
"githubPullRequests.ignoredPullRequestBranches": [
10+
"main"
11+
]
912
}

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

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

77
## Quickstart
88

9-
This is a very simple setup to get you started.
9+
This is a minimal 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, fireEvent, waitFor, screen} from '@testing-library/react'
15+
import {render, waitFor, screen} from '@testing-library/react'
16+
import userEvent from '@testing-library/user-event'
1617
import '@testing-library/jest-dom'
1718
import Fetch from '../fetch'
1819

20+
const setup = (jsx)=> ({ user: userEvent.setup(), ...render(jsx) })
21+
1922
test('loads and displays greeting', async () => {
2023

2124
// ARRANGE
22-
render(<Fetch url="/greeting" />)
25+
const { user } = setup(<Fetch url="/greeting" />)
2326

2427
// ACT
25-
fireEvent.click(screen.getByText('Load Greeting'))
26-
await waitFor(() => screen.getByRole('heading'))
28+
await user.click(screen.getByText('Load Greeting'))
29+
await screen.findByRole('heading')
2730

2831
// ASSERT
2932
expect(screen.getByRole('heading')).toHaveTextContent('hello there')
@@ -37,20 +40,25 @@ test('loads and displays greeting', async () => {
3740

3841
```jsx
3942
// import react-testing methods
40-
import {render, fireEvent, waitFor, screen} from '@testing-library/react'
43+
import {render, waitFor, screen} from '@testing-library/react'
44+
import userEvent from '@testing-library/user-event'
4145
// add custom jest matchers from jest-dom
4246
import '@testing-library/jest-dom'
4347
// the component to test
4448
import Fetch from '../fetch'
4549

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) })
53+
4654
test('loads and displays greeting', async () => {
4755

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

51-
fireEvent.click(screen.getByText('Load Greeting'))
52-
// getByRole throws an error if it cannot find an element
53-
await waitFor(() => screen.getByRole('heading'))
59+
await user.click(screen.getByText('Load Greeting'))
60+
// wait before throwing an error if it cannot find an element
61+
await screen.findByRole('heading')
5462

5563
// assert that the alert message is correct using
5664
// toHaveTextContent, a custom matcher from jest-dom.

0 commit comments

Comments
 (0)