Skip to content

Commit 108c292

Browse files
authored
Merge pull request #1961 from ishaanb5/source
Updated material and added fixes for user-event 14
2 parents 10dbc4f + 6f604e0 commit 108c292

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/content/5/en/part5c.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Now the HTML of the wanted element gets printed:
244244

245245
In addition to displaying content, the <i>Note</i> component also makes sure that when the button associated with the note is pressed, the _toggleImportance_ event handler function gets called.
246246

247-
Let us install a library [user-event](https://testing-library.com/docs/ecosystem-user-event/) that makes simulating user input a bit easier:
247+
Let us install a library [user-event](https://testing-library.com/docs/user-event/intro) that makes simulating user input a bit easier:
248248

249249
```
250250
npm install --save-dev @testing-library/user-event
@@ -279,8 +279,9 @@ test('clicking the button calls event handler once', async () => {
279279
<Note note={note} toggleImportance={mockHandler} />
280280
)
281281

282+
const user = userEvent.setup()
282283
const button = screen.getByText('make not important')
283-
userEvent.click(button)
284+
await user.click(button)
284285

285286
expect(mockHandler.mock.calls).toHaveLength(1)
286287
})
@@ -293,14 +294,19 @@ There are a few interesting things related to this test. The event handler is a
293294
const mockHandler = jest.fn()
294295
```
295296

297+
A [session](https://testing-library.com/docs/user-event/setup/) is started to interact with the rendered component:
298+
```js
299+
const user = userEvent.setup()
300+
```
301+
296302
The test finds the button <i>based on the text</i> from the rendered component and clicks the element:
297303

298304
```js
305+
await user.click(button)
299306
const button = screen.getByText('make not important')
300-
userEvent.click(button)
301307
```
302308

303-
Clicking happens with the method [click](https://testing-library.com/docs/ecosystem-user-event/#clickelement-eventinit-options) of the userEvent-library.
309+
Clicking happens with the method [click](https://testing-library.com/docs/user-event/convenience/#click) of the userEvent-library.
304310

305311

306312
The expectation of the test verifies that the <i>mock function</i> has been called exactly once.
@@ -368,9 +374,10 @@ describe('<Togglable />', () => {
368374
expect(div).toHaveStyle('display: none')
369375
})
370376

371-
test('after clicking the button, children are displayed', () => {
377+
test('after clicking the button, children are displayed', async () => {
378+
const user = userEvent.setup()
372379
const button = screen.getByText('show...')
373-
userEvent.click(button)
380+
await user.click(button)
374381

375382
const div = container.querySelector('.togglableContent')
376383
expect(div).not.toHaveStyle('display: none')
@@ -397,12 +404,13 @@ describe('<Togglable />', () => {
397404

398405
// ...
399406

400-
test('toggled content can be closed', () => {
407+
test('toggled content can be closed', async () => {
408+
const user = userEvent.setup()
401409
const button = screen.getByText('show...')
402-
userEvent.click(button)
410+
await user.click(button)
403411

404412
const closeButton = screen.getByText('cancel')
405-
userEvent.click(closeButton)
413+
await user.click(closeButton)
406414

407415
const div = container.querySelector('.togglableContent')
408416
expect(div).toHaveStyle('display: none')
@@ -412,11 +420,12 @@ describe('<Togglable />', () => {
412420

413421
### Testing the forms
414422

415-
We already used the _click_ function of the [user-event](https://testing-library.com/docs/ecosystem-user-event/) in our previous tests to click buttons.
423+
We already used the _click_ function of the [user-event](https://testing-library.com/docs/user-event/intro) in our previous tests to click buttons.
416424

417425
```js
426+
const user = userEvent.setup()
418427
const button = screen.getByText('show...')
419-
userEvent.click(button)
428+
await user.click(button)
420429
```
421430

422431
We can also simulate text input with <i>userEvent</i>.
@@ -472,16 +481,17 @@ import '@testing-library/jest-dom/extend-expect'
472481
import NoteForm from './NoteForm'
473482
import userEvent from '@testing-library/user-event'
474483

475-
test('<NoteForm /> updates parent state and calls onSubmit', () => {
484+
test('<NoteForm /> updates parent state and calls onSubmit', async () => {
476485
const createNote = jest.fn()
486+
const user = userEvent.setup()
477487

478488
render(<NoteForm createNote={createNote} />)
479489

480490
const input = screen.getByRole('textbox')
481491
const sendButton = screen.getByText('save')
482492

483-
userEvent.type(input, 'testing a form...' )
484-
userEvent.click(sendButton)
493+
await user.type(input, 'testing a form...' )
494+
await user.click(sendButton)
485495

486496
expect(createNote.mock.calls).toHaveLength(1)
487497
expect(createNote.mock.calls[0][0].content).toBe('testing a form...' )
@@ -490,7 +500,7 @@ test('<NoteForm /> updates parent state and calls onSubmit', () => {
490500

491501
Tests gets the access to the the input field using the function [getByRole](https://testing-library.com/docs/queries/byrole).
492502

493-
Method [type](https://testing-library.com/docs/ecosystem-user-event/#typeelement-text-options) of the userEvent is used to write text to the input field.
503+
Method [type](https://testing-library.com/docs/user-event/utility#type) of the userEvent is used to write text to the input field.
494504

495505
The first test expectation ensures, that submitting the form calls the _createNote_ method.
496506
The second expectation checks, that the event handler is called with the right parameters - that a note with the correct content is created when the form is filled.
@@ -540,7 +550,7 @@ The error message suggests to use <i>getAllByRole</i>. Test could be fixed as fo
540550
```js
541551
const inputs = screen.getAllByRole('textbox')
542552

543-
userEvent.type(input[0], 'testing a form...' )
553+
await user.type(inputs[0], 'testing a form...' )
544554
```
545555

546556
Method <i>getAllByRole</i> now returns an array and the right input field is the first element of the array. However, this approach is a bit suspicious since it relies on the order of the input fields.

0 commit comments

Comments
 (0)