Skip to content

Commit e5c907f

Browse files
authored
Add docs for ByRole 'level' option (#582)
1 parent 6e1705e commit e5c907f

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ getByRole(
625625
checked?: boolean,
626626
pressed?: boolean,
627627
queryFallbacks?: boolean,
628+
level?: number,
628629
}): HTMLElement
629630
```
630631

@@ -635,7 +636,7 @@ attribute. Here you can see
635636
[a table of HTML elements with their default and desired roles](https://www.w3.org/TR/html-aria/#docconformance).
636637

637638
Please note that setting a `role` and/or `aria-*` attribute that matches the
638-
implicit ARIA semantics is unnecessary and is **not recomended** as these
639+
implicit ARIA semantics is unnecessary and is **not recommended** as these
639640
properties are already set by the browser, and we must not use the `role` and
640641
`aria-*` attributes in a manner that conflicts with the semantics described. For
641642
example, a `button` element can't have the `role` attribute of `heading`,
@@ -787,6 +788,8 @@ cy.findByRole('dialog').should('exist')
787788

788789
<!--END_DOCUSAURUS_CODE_TABS-->
789790

791+
#### `queryFallbacks`
792+
790793
By default, it's assumed that the first role of each element is supported, so
791794
only the first role can be queried. If you need to query an element by any of
792795
its fallback roles instead, you can use `queryFallbacks: true`.
@@ -803,6 +806,53 @@ roles and therefore match the same element.
803806
> roles get introduced and you want to start supporting those as well as older
804807
> environments that don't understand that role (yet).
805808
809+
#### `level`
810+
811+
An element with the `heading` role can be queried by any heading level
812+
`getByRole('heading')` or by a specific heading level using the `level` option
813+
`getByRole('heading', { level: 2 })`.
814+
815+
The `level` option queries the element(s) with the `heading` role matching the
816+
indicated level determined by the semantic HTML heading elements `<h1>-<h6>` or
817+
matching the `aria-level` attribute.
818+
819+
Given the example below,
820+
821+
```html
822+
<body>
823+
<section>
824+
<h1>Heading Level One</h1>
825+
<h2>First Heading Level Two</h2>
826+
<h3>Heading Level Three</h3>
827+
<div role="heading" aria-level="2">Second Heading Level Two</div>
828+
</section>
829+
</body>
830+
```
831+
832+
you can query the `Heading Level Three` heading using
833+
`getByRole('heading', { level: 3 })`.
834+
835+
```js
836+
getByRole('heading', { level: 1 })
837+
// <h1>Heading Level One</h1>
838+
839+
getAllByRole('heading', { level: 2 })
840+
// [
841+
// <h2>First Heading Level Two</h2>,
842+
// <div role="heading" aria-level="2">Second Heading Level Two</div>
843+
// ]
844+
```
845+
846+
While it is possible to explicitly set `role="heading"` and `aria-level`
847+
attribute on an element, it is **strongly encouraged** to use the semantic HTML
848+
headings `<h1>-<h6>`.
849+
850+
To learn more about the `aria-level` property, see
851+
[ARIA `aria-level`](https://www.w3.org/TR/wai-aria-1.2/#aria-level).
852+
853+
> The `level` option is _only_ applicable to the `heading` role. An error will
854+
> be thrown when used with any other role.
855+
806856
### `ByTestId`
807857

808858
> getByTestId, queryByTestId, getAllByTestId, queryAllByTestId, findByTestId,

0 commit comments

Comments
 (0)