@@ -175,25 +175,23 @@ import '@testing-library/jest-dom/extend-expect'
175
175
// NOTE: jest-dom adds handy assertions to Jest and is recommended, but not required
176
176
177
177
import React from ' react'
178
- import {render , fireEvent } from ' @testing-library/react'
178
+ import {render , fireEvent , screen } from ' @testing-library/react'
179
179
import HiddenMessage from ' ../hidden-message'
180
180
181
181
test (' shows the children when the checkbox is checked' , () => {
182
182
const testMessage = ' Test Message'
183
- const {queryByText , getByLabelText , getByText } = render (
184
- < HiddenMessage> {testMessage}< / HiddenMessage> ,
185
- )
183
+ render (< HiddenMessage> {testMessage}< / HiddenMessage> )
186
184
187
185
// query* functions will return the element or null if it cannot be found
188
186
// get* functions will return the element or throw an error if it cannot be found
189
- expect (queryByText (testMessage)).toBeNull ()
187
+ expect (screen . queryByText (testMessage)).toBeNull ()
190
188
191
189
// the queries can accept a regex to make your selectors more resilient to content tweaks and changes.
192
- fireEvent .click (getByLabelText (/ show/ i ))
190
+ fireEvent .click (screen . getByLabelText (/ show/ i ))
193
191
194
192
// .toBeInTheDocument() is an assertion that comes from jest-dom
195
193
// otherwise you could use .toBeDefined()
196
- expect (getByText (testMessage)).toBeInTheDocument ()
194
+ expect (screen . getByText (testMessage)).toBeInTheDocument ()
197
195
})
198
196
```
199
197
@@ -265,7 +263,7 @@ export default Login
265
263
// your testing framework configuration rather than importing them in every file.
266
264
import ' @testing-library/jest-dom/extend-expect'
267
265
import React from ' react'
268
- import {render , fireEvent } from ' @testing-library/react'
266
+ import {render , fireEvent , screen } from ' @testing-library/react'
269
267
import Login from ' ../login'
270
268
271
269
test (' allows the user to login successfully' , async () => {
@@ -277,17 +275,21 @@ test('allows the user to login successfully', async () => {
277
275
})
278
276
})
279
277
280
- const { getByLabelText , getByText , findByRole } = render (< Login / > )
278
+ render (< Login / > )
281
279
282
280
// fill out the form
283
- fireEvent .change (getByLabelText (/ username/ i ), {target: {value: ' chuck' }})
284
- fireEvent .change (getByLabelText (/ password/ i ), {target: {value: ' norris' }})
281
+ fireEvent .change (screen .getByLabelText (/ username/ i ), {
282
+ target: {value: ' chuck' },
283
+ })
284
+ fireEvent .change (screen .getByLabelText (/ password/ i ), {
285
+ target: {value: ' norris' },
286
+ })
285
287
286
- fireEvent .click (getByText (/ submit/ i ))
288
+ fireEvent .click (screen . getByText (/ submit/ i ))
287
289
288
290
// just like a manual tester, we'll instruct our test to wait for the alert
289
291
// to show up before continuing with our assertions.
290
- const alert = await findByRole (' alert' )
292
+ const alert = await screen . findByRole (' alert' )
291
293
292
294
// .toHaveTextContent() comes from jest-dom's assertions
293
295
// otherwise you could use expect(alert.textContent).toMatch(/congrats/i)
0 commit comments