Skip to content

Commit a102852

Browse files
authored
Update osa5c.md
1 parent 108c292 commit a102852

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/content/5/fi/osa5c.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ test('clicking the button calls event handler once', async () => {
275275
<Note note={note} toggleImportance={mockHandler} />
276276
)
277277

278+
const user = userEvent.setup()
278279
const button = screen.getByText('make not important')
279-
userEvent.click(button)
280+
await user.click(button)
280281

281282
expect(mockHandler.mock.calls).toHaveLength(1)
282283
})
@@ -287,15 +288,21 @@ Testissä on muutama mielenkiintoinen seikka. Tapahtumankäsittelijäksi annetaa
287288
```js
288289
const mockHandler = jest.fn()
289290
```
291+
292+
Jotta renderöidyn komponentin kanssa voi vuorovaikuttaa tapahtumien avulla, tulee ensin aloittaa uusi sessio. Tämä onnistuu userEvent-olion [setup](https://testing-library.com/docs/user-event/setup/)-metodin avulla:
293+
294+
```js
295+
const user = userEvent.setup()
296+
```
290297

291298
Testi hakee renderöidystä komponentista napin <i>tekstin perusteella</i> ja klikkaa sitä:
292299

293300
```js
294301
const button = screen.getByText('make not important')
295-
userEvent.click(button)
302+
await user.click(button)
296303
```
297304

298-
Klikkaaminen tapahtuu userEvent-olion metodin [click](https://testing-library.com/docs/ecosystem-user-event/#clickelement-eventinit-options) avulla.
305+
Klikkaaminen tapahtuu userEvent-olion metodin [click](https://testing-library.com/docs/user-event/convenience/#click) avulla.
299306

300307
Testin ekspektaatio varmistaa, että <i>mock-funktiota</i> on kutsuttu täsmälleen kerran:
301308

@@ -362,9 +369,10 @@ describe('<Togglable />', () => {
362369
expect(div).toHaveStyle('display: none')
363370
})
364371

365-
test('after clicking the button, children are displayed', () => {
372+
test('after clicking the button, children are displayed', async () => {
373+
const user = userEvent.setup()
366374
const button = screen.getByText('show...')
367-
userEvent.click(button)
375+
await user.click(button)
368376

369377
const div = container.querySelector('.togglableContent')
370378
expect(div).not.toHaveStyle('display: none')
@@ -391,12 +399,14 @@ describe('<Togglable />', () => {
391399

392400
// ...
393401

394-
test('toggled content can be closed', () => {
402+
test('toggled content can be closed', async () => {
403+
const user = userEvent.setup()
404+
395405
const button = screen.getByText('show...')
396-
userEvent.click(button)
406+
await user.click(button)
397407

398408
const closeButton = screen.getByText('cancel')
399-
userEvent.click(closeButton)
409+
await user.click(closeButton)
400410

401411
const div = container.querySelector('.togglableContent')
402412
expect(div).toHaveStyle('display: none')
@@ -406,14 +416,14 @@ describe('<Togglable />', () => {
406416

407417
### Lomakkeiden testaus
408418

409-
Käytimme jo edellisissä testeissä [userEvent]([user-event](https://testing-library.com/docs/ecosystem-user-event/))-kirjaston _fireEvent_-funktiota nappien klikkaamiseen:
419+
Käytimme jo edellisissä testeissä [user-event](https://testing-library.com/docs/user-event/intro)-kirjaston _click_-metodia nappien klikkaamiseen:
410420

411421
```js
412422
const button = screen.getByText('show...')
413-
userEvent.click(button)
423+
await user.click(button)
414424
```
415425

416-
Käytännössä siis loimme <i>userEventin</i> avulla tapahtuman <i>click</i> -nappia vastaavalle komponentille. Voimme simuloida myös lomakkeelle kirjoittamista <i>userEventin</i> avulla.
426+
Käytännössä siis loimme metodin avulla <i>click</i>-tapahtuman metodin argumenttina annatulle komponentille. Voimme simuloida myös lomakkeelle kirjoittamista <i>userEventin</i>-olion avulla.
417427

418428
Tehdään testi komponentille <i>NoteForm</i>. Lomakkeen koodi näyttää seuraavalta:
419429

@@ -466,16 +476,17 @@ import '@testing-library/jest-dom/extend-expect'
466476
import NoteForm from './NoteForm'
467477
import userEvent from '@testing-library/user-event'
468478

469-
test('<NoteForm /> updates parent state and calls onSubmit', () => {
479+
test('<NoteForm /> updates parent state and calls onSubmit', async () => {
480+
const user = userEvent.setup()
470481
const createNote = jest.fn()
471482

472483
render(<NoteForm createNote={createNote} />)
473484

474485
const input = screen.getByRole('textbox')
475486
const sendButton = screen.getByText('save')
476487

477-
userEvent.type(input, 'testing a form...')
478-
userEvent.click(sendButton)
488+
await user.type(input, 'testing a form...')
489+
await user.click(sendButton)
479490

480491
expect(createNote.mock.calls).toHaveLength(1)
481492
expect(createNote.mock.calls[0][0].content).toBe('testing a form...')
@@ -484,7 +495,7 @@ test('<NoteForm /> updates parent state and calls onSubmit', () => {
484495

485496
Syötekenttä etsitään metodin [getByRole](https://testing-library.com/docs/queries/byrole) avulla.
486497

487-
Syötekenttään kirjoitetaan userEvent:in tarjoaman metodin [type](https://testing-library.com/docs/ecosystem-user-event/#typeelement-text-options) avulla.
498+
Syötekenttään kirjoitetaan metodin [type](https://testing-library.com/docs/user-event/utility#type) avulla.
488499

489500
Testin ensimmäinen ekspektaatio varmistaa, että lomakkeen lähetys on aikaansaanut tapahtumankäsittelijän _createNote_ kutsumisen. Toinen ekspektaatio tarkistaa, että tapahtumankäsittelijää kutsutaan oikealla parametrilla, eli että luoduksi tulee samansisältöinen muistiinpano kuin lomakkeelle kirjoitetaan.
490501

@@ -533,7 +544,7 @@ Virheilmoitus ehdottaa käytettäväksi metodia <i>getAllByRole</i> (jos tilanne
533544
```js
534545
const inputs = screen.getAllByRole('textbox')
535546

536-
userEvent.type(input[0], 'testing a form...')
547+
await user.type(inputs[0], 'testing a form...')
537548
```
538549

539550
Metodi <i>getAllByRole</i> palauttaa taulukon, ja oikea tekstikenttä on taulukossa ensimmäisenä. Testi on kuitenkin hieman epäilyttävä, sillä se luottaa tekstikenttien järjestykseen.

0 commit comments

Comments
 (0)