Skip to content

Commit 5723d92

Browse files
authored
Merge pull request #518 from testing-library/nickmccurdy-patch-1
Add documentation for queryFallback
2 parents 0a91037 + 7023713 commit 5723d92

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ getByRole(
605605
name?: TextMatch,
606606
normalizer?: NormalizerFn,
607607
selected?: boolean,
608+
queryFallbacks?: boolean,
608609
}): HTMLElement
609610
```
610611

@@ -710,6 +711,12 @@ cy.findByRole('dialog').should('exist')
710711

711712
<!--END_DOCUSAURUS_CODE_TABS-->
712713

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).
719+
713720
### `ByTestId`
714721

715722
> getByTestId, queryByTestId, getAllByTestId, queryAllByTestId, findByTestId,

0 commit comments

Comments
 (0)