@@ -6,26 +6,24 @@ sidebar_label: Example
6
6
7
7
## Quickstart
8
8
9
- This is a minimal setup to get you started.
9
+ This is a very simple setup to get you started.
10
10
If you want to see a description of what each line does,
11
11
scroll down to the [ annotated version] ( #quickstart-annotated ) .
12
12
Scroll down to [ Full Example] ( #full-example ) to see a more advanced test setup.
13
13
14
14
``` jsx
15
- import {render , waitFor , screen } from ' @testing-library/react'
15
+ import { render , screen } from ' @testing-library/react'
16
16
import userEvent from ' @testing-library/user-event'
17
17
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'
21
19
22
20
test (' loads and displays greeting' , async () => {
23
21
24
22
// ARRANGE
25
- const { user } = setup (< Fetch url= " /greeting" / > )
23
+ render (< Fetch url= " /greeting" / > )
26
24
27
25
// ACT
28
- await user .click (screen .getByText (' Load Greeting' ))
26
+ await userEvent .click (screen .getByText (' Load Greeting' ))
29
27
await screen .findByRole (' heading' )
30
28
31
29
// ASSERT
@@ -40,23 +38,20 @@ test('loads and displays greeting', async () => {
40
38
41
39
``` jsx
42
40
// 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.
44
43
import userEvent from ' @testing-library/user-event'
45
44
// add custom jest matchers from jest-dom
46
45
import ' @testing-library/jest-dom'
47
46
// 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'
53
48
54
49
test (' loads and displays greeting' , async () => {
55
50
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" / > )
58
53
59
- await user .click (screen .getByText (' Load Greeting' ))
54
+ await userEvent .click (screen .getByText (' Load Greeting' ))
60
55
// wait before throwing an error if it cannot find an element
61
56
await screen .findByRole (' heading' )
62
57
0 commit comments