You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/5/en/part5c.md
+26-16Lines changed: 26 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -244,7 +244,7 @@ Now the HTML of the wanted element gets printed:
244
244
245
245
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.
246
246
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:
constbutton=screen.getByText('make not important')
283
-
userEvent.click(button)
284
+
awaituser.click(button)
284
285
285
286
expect(mockHandler.mock.calls).toHaveLength(1)
286
287
})
@@ -293,14 +294,19 @@ There are a few interesting things related to this test. The event handler is a
293
294
constmockHandler=jest.fn()
294
295
```
295
296
297
+
A [session](https://testing-library.com/docs/user-event/setup/) is started to interact with the rendered component:
298
+
```js
299
+
constuser=userEvent.setup()
300
+
```
301
+
296
302
The test finds the button <i>based on the text</i> from the rendered component and clicks the element:
297
303
298
304
```js
305
+
awaituser.click(button)
299
306
constbutton=screen.getByText('make not important')
300
-
userEvent.click(button)
301
307
```
302
308
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.
304
310
305
311
306
312
The expectation of the test verifies that the <i>mock function</i> has been called exactly once.
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.
416
424
417
425
```js
426
+
constuser=userEvent.setup()
418
427
constbutton=screen.getByText('show...')
419
-
userEvent.click(button)
428
+
awaituser.click(button)
420
429
```
421
430
422
431
We can also simulate text input with <i>userEvent</i>.
test('<NoteForm /> updates parent state and calls onSubmit', () => {
484
+
test('<NoteForm /> updates parent state and calls onSubmit', async() => {
476
485
constcreateNote=jest.fn()
486
+
constuser=userEvent.setup()
477
487
478
488
render(<NoteForm createNote={createNote} />)
479
489
480
490
constinput=screen.getByRole('textbox')
481
491
constsendButton=screen.getByText('save')
482
492
483
-
userEvent.type(input, 'testing a form...' )
484
-
userEvent.click(sendButton)
493
+
awaituser.type(input, 'testing a form...' )
494
+
awaituser.click(sendButton)
485
495
486
496
expect(createNote.mock.calls).toHaveLength(1)
487
497
expect(createNote.mock.calls[0][0].content).toBe('testing a form...' )
@@ -490,7 +500,7 @@ test('<NoteForm /> updates parent state and calls onSubmit', () => {
490
500
491
501
Tests gets the access to the the input field using the function [getByRole](https://testing-library.com/docs/queries/byrole).
492
502
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.
494
504
495
505
The first test expectation ensures, that submitting the form calls the _createNote_ method.
496
506
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
540
550
```js
541
551
constinputs=screen.getAllByRole('textbox')
542
552
543
-
userEvent.type(input[0], 'testing a form...' )
553
+
awaituser.type(inputs[0], 'testing a form...' )
544
554
```
545
555
546
556
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