Skip to content

Commit 59fdc89

Browse files
committed
Add description how to find an input element by it's label
1 parent 6d2a928 commit 59fdc89

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/content/5/en/part5c.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,39 @@ await user.type(inputs[0], 'testing a form...')
632632

633633
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.
634634

635+
If an <i>label</i> were defined for the input field, the input field could be located using it with the getByLabelText method. For example, if we added a label to the input field:
636+
637+
```js
638+
// ...
639+
<label> // highlight-line
640+
content // highlight-line
641+
<input
642+
value={newNote}
643+
onChange={event => setNewNote(event.target.value)}
644+
/>
645+
</label> // highlight-line
646+
// ...
647+
```
648+
649+
The test could locate the input field as follows:
650+
651+
```js
652+
test('<NoteForm /> updates parent state and calls onSubmit', () => {
653+
const createNote = vi.fn()
654+
655+
render(<NoteForm createNote={createNote} />)
656+
657+
const input = screen.getByLabelText('content') // highlight-line
658+
const sendButton = screen.getByText('save')
659+
660+
userEvent.type(input, 'testing a form...' )
661+
userEvent.click(sendButton)
662+
663+
expect(createNote.mock.calls).toHaveLength(1)
664+
expect(createNote.mock.calls[0][0].content).toBe('testing a form...' )
665+
})
666+
```
667+
635668
Quite often input fields have a <i>placeholder</i> text that hints user what kind of input is expected. Let us add a placeholder to our form:
636669

637670
```js

src/content/5/fi/osa5c.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,39 @@ await user.type(inputs[0], 'testing a form...')
631631

632632
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.
633633

634+
Jos syötekentälle olisi määritelty <i>label</i>, voisi kyseisen syötekentän etsiä sen avulla käyttäen metodia _getByLabelText_. Jos siis lisäisimme syötekentälle labelin:
635+
636+
```js
637+
// ...
638+
<label> // highlight-line
639+
content // highlight-line
640+
<input
641+
value={newNote}
642+
onChange={event => setNewNote(event.target.value)}
643+
/>
644+
</label> // highlight-line
645+
// ...
646+
```
647+
648+
Testi löytäisi syötekentän seuraavasti:
649+
650+
```js
651+
test('<NoteForm /> updates parent state and calls onSubmit', () => {
652+
const createNote = vi.fn()
653+
654+
render(<NoteForm createNote={createNote} />)
655+
656+
const input = screen.getByLabelText('content') // highlight-line
657+
const sendButton = screen.getByText('save')
658+
659+
userEvent.type(input, 'testing a form...' )
660+
userEvent.click(sendButton)
661+
662+
expect(createNote.mock.calls).toHaveLength(1)
663+
expect(createNote.mock.calls[0][0].content).toBe('testing a form...' )
664+
})
665+
```
666+
634667
Syötekentille määritellään usein placeholder-teksti, joka ohjaa käyttäjää kirjoittamaan syötekenttään oikean arvon. Lisätään placeholder lomakkeellemme:
635668

636669
```js

0 commit comments

Comments
 (0)