Skip to content

Commit 9dd8b32

Browse files
authored
feat(byRole): Add name option (#368)
* feat(byRole): Add `name` option * Recommend byRole more * resolve Kent's review * Try another query recommendation * Fix more by*
1 parent e49f159 commit 9dd8b32

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ cy.findByLabelText('username').should('exist')
187187

188188
<!--END_DOCUSAURUS_CODE_TABS-->
189189

190-
It will NOT find the input node for label text broken up by elements. For this
191-
case, you can provide a `selector` in the options:
190+
It will NOT find the input node for label text broken up by elements. You can
191+
use `getByRole('textbox', { name: 'Username' })` instead which is robust against
192+
switching to `aria-label` or `aria-labelledby`.
193+
194+
If it is important that you query an actual `<label>` element you can provide a
195+
`selector` in the options:
192196

193197
```html
194198
<label> <span>Username</span> <input /> </label>
@@ -590,6 +594,7 @@ getByRole(
590594
options?: {
591595
exact?: boolean = true,
592596
hidden?: boolean = true,
597+
name?: TextMatch,
593598
normalizer?: NormalizerFn,
594599
}): HTMLElement
595600
```
@@ -604,6 +609,24 @@ Library uses `aria-query` under the hood to find those elements by their default
604609
ARIA roles, you can find in their docs
605610
[which HTML Elements with inherent roles are mapped to each role](https://github.com/A11yance/aria-query#elements-to-roles).
606611

612+
You can query the returned element(s) by their
613+
[accessible name](https://www.w3.org/TR/accname-1.1/). The accessible name is
614+
for simple cases equal to e.g. the label of a form element, or the text content
615+
of a button, or the value of the `aria-label` attribute. It can be used to query
616+
a specific element if multiple elements with the same role are present on the
617+
rendered content. For an in-depth guide check out
618+
["What is an accessible name?" from ThePacielloGroup](https://developer.paciellogroup.com/blog/2017/04/what-is-an-accessible-name/).
619+
If you only query for a single element with `getByText('The name')` it's
620+
oftentimes better to use `getByRole(expectedRole, { name: 'The name' })`. The
621+
accessible name query does not replace other queries such as `*ByAlt` or
622+
`*ByTitle`. While the accessible name can be equal to these attributes, it does
623+
not replace the functionality of these attributes. For example
624+
`<img aria-label="fancy image" src="fancy.jpg" />` will be returned for both
625+
`getByAlt('fancy image')` and `getByRole('image', { name: 'fancy image' })`.
626+
However, the image will not display its description if `fancy.jpg` could not be
627+
loaded. Whether you want assert this functionality in your test or not is up to
628+
you.
629+
607630
If you set `hidden` to `true` elements that are normally excluded from the
608631
accessibility tree are considered for the query as well. The default behavior
609632
follows https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion with the exception of

docs/guide-which-query.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ possible. With this in mind, we recommend this order of priority:
1717
1. `getByPlaceholderText`:
1818
[A placeholder is not a substitute for a label](https://www.nngroup.com/articles/form-design-placeholders/).
1919
But if that's all you have, then it's better than alternatives.
20+
1. `getByRole`: This can be used to query every element that is exposed in
21+
the
22+
[accessibility tree](https://developer.mozilla.org/en-US/docs/Glossary/AOM).
23+
With the `name` option you can filter the returned elements by their
24+
[accessible name](https://www.w3.org/TR/accname-1.1/). This should be your
25+
top preference for interactive elements such as buttons.
2026
1. `getByText`: Not useful for forms, but this is the number 1 method a user
21-
finds other elements (like buttons to click), so it should be your top
22-
preference for non-form elements.
27+
finds most non-interactive elements (listitems or divs).
2328
1. `getByDisplayValue`: The current value of a form element can be useful
2429
when navigating a page with filled-in values.
2530
1. **Semantic Queries** HTML5 and ARIA compliant selectors. Note that the user
@@ -29,8 +34,6 @@ possible. With this in mind, we recommend this order of priority:
2934
`area`, and `input`), then you can use this to find that element.
3035
1. `getByTitle`: The title attribute is not consistently read by
3136
screenreaders, and is not visible by default for sighted users
32-
1. `getByRole`: This can be used to select dialog boxes and other
33-
difficult-to-capture elements in a more semantic way
3437
1. **Test IDs**
3538
1. `getByTestId`: The user cannot see (or hear) these, so this is only
3639
recommended for cases where you can't match by role or text or it doesn't

0 commit comments

Comments
 (0)