Skip to content

Commit 5560e6e

Browse files
authored
Warn about literal role matching (#520)
1 parent 5723d92 commit 5560e6e

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ more than one element is found after a default timeout of `1000`ms. If you need
3838
to find more than one element, then use `findAllBy`.
3939

4040
> Note, this is a simple combination of `getBy*` queries and
41-
> [`waitFor`](/docs/api-async#waitfor). The `findBy*` queries
42-
> accept the `waitFor` options as the last argument. (i.e.
41+
> [`waitFor`](/docs/api-async#waitfor). The `findBy*` queries accept the
42+
> `waitFor` options as the last argument. (i.e.
4343
> `screen.findByText('text', queryOptions, waitForOptions)`)
4444
4545
### findAllBy
@@ -88,7 +88,7 @@ This method is essentially a shortcut for `console.log(prettyDOM())`. It
8888
supports debugging the document, a single element, or an array of elements.
8989

9090
```javascript
91-
import {screen} from '@testing-library/dom'
91+
import { screen } from '@testing-library/dom'
9292

9393
document.body.innerHTML = `
9494
<button>test</button>
@@ -217,7 +217,7 @@ If it is important that you query an actual `<label>` element you can provide a
217217
```
218218

219219
```js
220-
const inputNode = screen.getByLabelText('Username', {selector: 'input'})
220+
const inputNode = screen.getByLabelText('Username', { selector: 'input' })
221221
```
222222

223223
### `ByPlaceholderText`
@@ -619,6 +619,10 @@ Library uses `aria-query` under the hood to find those elements by their default
619619
ARIA roles, you can find in their docs
620620
[which HTML Elements with inherent roles are mapped to each role](https://github.com/A11yance/aria-query#elements-to-roles).
621621

622+
> Roles are matched literally by string equality, without inheriting from the
623+
> ARIA role hierarchy. As a result, querying a superclass role like `checkbox`
624+
> will not include elements with a subclass role like `switch`.
625+
622626
You can query the returned element(s) by their
623627
[accessible name](https://www.w3.org/TR/accname-1.1/). The accessible name is
624628
for simple cases equal to e.g. the label of a form element, or the text content
@@ -658,11 +662,11 @@ case. For example in
658662
assertions about the `Open dialog`-button you would need to use
659663
`getAllByRole('button', { hidden: true })`.
660664

661-
The default value for `hidden` can [be configured](api-configuration#configuration).
665+
The default value for `hidden` can
666+
[be configured](api-configuration#configuration).
662667

663-
Certain roles can have a selected state. You can filter the
664-
returned elements by their selected state
665-
by setting `selected: true` or `selected: false`.
668+
Certain roles can have a selected state. You can filter the returned elements by
669+
their selected state by setting `selected: true` or `selected: false`.
666670

667671
For example in
668672

@@ -677,8 +681,8 @@ For example in
677681
```
678682

679683
you can get the "Native"-tab by calling `getByRole('tab', { selected: true })`.
680-
To learn more about the selected state and which elements can
681-
have this state see [ARIA `aria-selected`](https://www.w3.org/TR/wai-aria-1.2/#aria-selected).
684+
To learn more about the selected state and which elements can have this state
685+
see [ARIA `aria-selected`](https://www.w3.org/TR/wai-aria-1.2/#aria-selected).
682686

683687
```html
684688
<div role="dialog">...</div>
@@ -711,11 +715,21 @@ cy.findByRole('dialog').should('exist')
711715

712716
<!--END_DOCUSAURUS_CODE_TABS-->
713717

714-
By default, it's assumed that the first role of each element is supported, so only the first role can be queried. If you need to query an element by any of its fallback roles instead, you can use `queryFallbacks: true`.
715-
716-
For example, `getByRole('switch')` would always match `<div role="switch checkbox" />` because it's the first role, while `getByRole('checkbox')` would not. However, `getByRole('checkbox', { queryFallbacks: true })` would enable all fallback roles and therefore match the same element.
717-
718-
> An element doesn't have multiple roles in a given environment. It has a single one. Multiple roles in the attribute are evaluated from left to right until the environment finds the first role it understands. This is useful when new roles get introduced and you want to start supporting those as well as older environments that don't understand that role (yet).
718+
By default, it's assumed that the first role of each element is supported, so
719+
only the first role can be queried. If you need to query an element by any of
720+
its fallback roles instead, you can use `queryFallbacks: true`.
721+
722+
For example, `getByRole('switch')` would always match
723+
`<div role="switch checkbox" />` because it's the first role, while
724+
`getByRole('checkbox')` would not. However,
725+
`getByRole('checkbox', { queryFallbacks: true })` would enable all fallback
726+
roles and therefore match the same element.
727+
728+
> An element doesn't have multiple roles in a given environment. It has a single
729+
> one. Multiple roles in the attribute are evaluated from left to right until
730+
> the environment finds the first role it understands. This is useful when new
731+
> roles get introduced and you want to start supporting those as well as older
732+
> environments that don't understand that role (yet).
719733
720734
### `ByTestId`
721735

0 commit comments

Comments
 (0)