Skip to content

Commit 1dc5f28

Browse files
[findByLabelText] Adds a bad example using for w/ a non-form element (#534)
This came up at my work today. An engineer thought that a `for` attr on a `<label>` along with a related `<section>` `id` can be queried by `findByLabelText`. But really, we end up with this error: ``` Timed out retrying: Found a label with the text of: Photos, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. ``` I think making that explicit in the docs would avoid future confusion.
1 parent bbdf4b4 commit 1dc5f28

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

docs/dom-testing-library/api-queries.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ If it is important that you query an actual `<label>` element you can provide a
220220
const inputNode = screen.getByLabelText('Username', { selector: 'input' })
221221
```
222222

223+
Note that it will not work in the case where a `for` attr on a `label` matches and `id` field on a non-form element.
224+
225+
```js
226+
// This case is not valid
227+
// for/htmlFor between label and an element that is not a form element
228+
<section id="photos-section">
229+
<label for="photos-section">Photos</label>
230+
</section>
231+
```
232+
223233
### `ByPlaceholderText`
224234

225235
> getByPlaceholderText, queryByPlaceholderText, getAllByPlaceholderText,

0 commit comments

Comments
 (0)