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
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -632,6 +632,39 @@ await user.type(inputs[0], 'testing a form...')
632
632
633
633
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.
634
634
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', () => {
Copy file name to clipboardExpand all lines: src/content/5/fi/osa5c.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -631,6 +631,39 @@ await user.type(inputs[0], 'testing a form...')
631
631
632
632
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.
633
633
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', () => {
expect(createNote.mock.calls[0][0].content).toBe('testing a form...' )
664
+
})
665
+
```
666
+
634
667
Syötekentille määritellään usein placeholder-teksti, joka ohjaa käyttäjää kirjoittamaan syötekenttään oikean arvon. Lisätään placeholder lomakkeellemme:
0 commit comments