@@ -8,13 +8,14 @@ title: Using findByText
8
8
// This is an example of how to use findByText to query for text that
9
9
// is not visible right away
10
10
11
- import {getByRole , findByText , getByPlaceholderText } from ' @testing-library/dom'
11
+ import {screen } from ' @testing-library/dom'
12
12
import userEvent from ' @testing-library/user-event'
13
13
// provides a set of custom jest matchers that you can use to extend jest
14
14
// i.e. `.toBeVisible`
15
15
import ' @testing-library/jest-dom'
16
16
17
- const renderContent = el => {
17
+ function renderApp () {
18
+ const el = document .body .appendChild (document .createElement (' div' ))
18
19
el .innerHTML = `
19
20
<form id =' login_form' method =' post' name =' login' >
20
21
<label for =' username' >User Name:</label >
@@ -64,8 +65,8 @@ const renderContent = el => {
64
65
const userInput = el .querySelector (' #username_input' )
65
66
const passwordInput = el .querySelector (' #password_input' )
66
67
67
- var userName = userInput .value
68
- var password = passwordInput .value
68
+ const userName = userInput .value
69
+ const password = passwordInput .value
69
70
if (! userName) {
70
71
el .querySelector (' #username_required_error' ).style .display = ' inline'
71
72
}
@@ -88,51 +89,47 @@ const renderContent = el => {
88
89
window .history .back ()
89
90
})
90
91
91
- return el
92
+ return {user : userEvent . setup ()}
92
93
}
93
94
94
- describe (' findByText Examples' , () => {
95
- let div
96
- let container
97
-
98
- beforeEach (() => {
99
- div = document .createElement (' div' )
100
- container = renderContent (div)
101
- })
95
+ afterEach (() => (document .body .innerHTML = ` ` ))
102
96
97
+ describe (' findByText Examples' , () => {
103
98
it (' should show a required field warning for each empty input field' , async () => {
104
- userEvent .click (
105
- getByRole (container, ' button' , {
99
+ const {user } = renderApp ()
100
+ await user .click (
101
+ screen .getByRole (' button' , {
106
102
name: ' Login' ,
107
103
}),
108
104
)
109
105
110
- expect (await findByText (container, ' User Name Required' )).toBeVisible ()
106
+ expect (await screen . findByText (' User Name Required' )).toBeVisible ()
111
107
112
- expect (await findByText (container, ' Password Required' )).toBeVisible ()
108
+ expect (await screen . findByText (' Password Required' )).toBeVisible ()
113
109
})
114
110
115
111
it (' should show invalid field errors for each invalid input field' , async () => {
116
- const userNameField = getByPlaceholderText (container, ' Enter user name' )
117
- const passwordField = getByPlaceholderText (container, ' Enter password' )
112
+ const {user } = renderApp ()
113
+ const userNameField = screen .getByPlaceholderText (' Enter user name' )
114
+ const passwordField = screen .getByPlaceholderText (' Enter password' )
118
115
119
- expect (await findByText (container, ' Invalid Password' )).not .toBeVisible ()
120
- expect (await findByText (container, ' Invalid User Name' )).not .toBeVisible ()
116
+ expect (await screen . findByText (' Invalid Password' )).not .toBeVisible ()
117
+ expect (await screen . findByText (' Invalid User Name' )).not .toBeVisible ()
121
118
122
- userEvent .type (userNameField, ' Philchard' )
123
- userEvent .type (passwordField, ' theCat' )
119
+ await user .type (userNameField, ' Philchard' )
120
+ await user .type (passwordField, ' theCat' )
124
121
125
122
expect (userNameField).toHaveValue (' Philchard' )
126
123
expect (passwordField).toHaveValue (' theCat' )
127
124
128
- userEvent .click (
129
- getByRole (container, ' button' , {
125
+ await user .click (
126
+ screen . getByRole (' button' , {
130
127
name: ' Login' ,
131
128
}),
132
129
)
133
130
134
- expect (await findByText (container, ' Invalid User Name' )).toBeVisible ()
135
- expect (await findByText (container, ' Invalid Password' )).toBeVisible ()
131
+ expect (await screen . findByText (' Invalid User Name' )).toBeVisible ()
132
+ expect (await screen . findByText (' Invalid Password' )).toBeVisible ()
136
133
})
137
134
})
138
135
```
0 commit comments