Skip to content

Commit e6ff3b8

Browse files
sakit0Kent C. Dodds
authored and
Kent C. Dodds
committed
docs: use screen (#553)
1 parent e35c213 commit e6ff3b8

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Diff for: README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,23 @@ import '@testing-library/jest-dom/extend-expect'
175175
// NOTE: jest-dom adds handy assertions to Jest and is recommended, but not required
176176

177177
import React from 'react'
178-
import {render, fireEvent} from '@testing-library/react'
178+
import {render, fireEvent, screen} from '@testing-library/react'
179179
import HiddenMessage from '../hidden-message'
180180

181181
test('shows the children when the checkbox is checked', () => {
182182
const testMessage = 'Test Message'
183-
const {queryByText, getByLabelText, getByText} = render(
184-
<HiddenMessage>{testMessage}</HiddenMessage>,
185-
)
183+
render(<HiddenMessage>{testMessage}</HiddenMessage>)
186184

187185
// query* functions will return the element or null if it cannot be found
188186
// 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()
190188

191189
// 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))
193191

194192
// .toBeInTheDocument() is an assertion that comes from jest-dom
195193
// otherwise you could use .toBeDefined()
196-
expect(getByText(testMessage)).toBeInTheDocument()
194+
expect(screen.getByText(testMessage)).toBeInTheDocument()
197195
})
198196
```
199197

@@ -265,7 +263,7 @@ export default Login
265263
// your testing framework configuration rather than importing them in every file.
266264
import '@testing-library/jest-dom/extend-expect'
267265
import React from 'react'
268-
import {render, fireEvent} from '@testing-library/react'
266+
import {render, fireEvent, screen} from '@testing-library/react'
269267
import Login from '../login'
270268

271269
test('allows the user to login successfully', async () => {
@@ -277,17 +275,21 @@ test('allows the user to login successfully', async () => {
277275
})
278276
})
279277

280-
const {getByLabelText, getByText, findByRole} = render(<Login />)
278+
render(<Login />)
281279

282280
// 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+
})
285287

286-
fireEvent.click(getByText(/submit/i))
288+
fireEvent.click(screen.getByText(/submit/i))
287289

288290
// just like a manual tester, we'll instruct our test to wait for the alert
289291
// to show up before continuing with our assertions.
290-
const alert = await findByRole('alert')
292+
const alert = await screen.findByRole('alert')
291293

292294
// .toHaveTextContent() comes from jest-dom's assertions
293295
// otherwise you could use expect(alert.textContent).toMatch(/congrats/i)

0 commit comments

Comments
 (0)