Skip to content

Commit 32067ef

Browse files
author
Artemis330
committed
Added docs for getRoles and logRoles (#155)
* Added docs for getRoles and logRoles These functions are proposed in dom-testing-library PR#285 testing-library/dom-testing-library#285 * Updated logRoles output example to match new format * Changed import to @testing-library/dom Co-Authored-By: Adrià Fontcuberta <[email protected]> * Changed 'aria' to 'ARIA' in api-helpers * Gave example HTML ul/li elements valid attributes name is not a valid attribute for ul/li, changed to type/value respectively * Updated another instance wrong 'dom-testing-library' import * Added link to ARIA in HTML under getRoles * Updated getRoles and logRoles examples After discussion we decided to pull attributes off the example elements. testing-library/testing-library-docs#155
1 parent 1b8f2ce commit 32067ef

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ module.exports = {
5858
5959
## `buildQueries`
6060

61-
The `buildQueries` helper allows you to create custom queres with all standard [variants](api-queries.md) of queries in testing-library.
61+
The `buildQueries` helper allows you to create custom queres with all standard
62+
[variants](api-queries.md) of queries in testing-library.
6263

63-
See the [Add custom queries](/docs/react-testing-library/setup#add-custom-queries) section of the custom render guide for example usage.
64+
See the
65+
[Add custom queries](/docs/react-testing-library/setup#add-custom-queries)
66+
section of the custom render guide for example usage.
6467

6568
### Using other assertion libraries
6669

@@ -148,6 +151,36 @@ cy.get('form').within(() => {
148151

149152
<!--END_DOCUSAURUS_CODE_TABS-->
150153

154+
## `getRoles`
155+
156+
This function allows iteration over the implicit ARIA roles represented in a
157+
given tree of DOM nodes.
158+
159+
It returns an object, indexed by role name, with each value being an array of
160+
elements which have that implicit ARIA role.
161+
162+
See
163+
[ARIA in HTML](https://www.w3.org/TR/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html)
164+
for more information about implicit ARIA roles.
165+
166+
```javascript
167+
import { getRoles } from '@testing-library/dom'
168+
169+
const nav = document.createElement('nav')
170+
nav.innerHTML = `
171+
<ul>
172+
<li>Item 1</li>
173+
<li>Item 2</li>
174+
</ul>`
175+
console.log(getRoles(nav))
176+
177+
// Object {
178+
// navigation: [<nav />],
179+
// list: [<ul />],
180+
// listitem: [<li />, <li />]
181+
// }
182+
```
183+
151184
## Debugging
152185

153186
When you use any `get` calls in your test cases, the current state of the
@@ -213,3 +246,44 @@ console.log(prettyDOM(div))
213246

214247
This function is what also powers
215248
[the automatic debugging output described above](#debugging).
249+
250+
### `logRoles`
251+
252+
This helper function can be used to print out a list of all the implicit ARIA
253+
roles within a tree of DOM nodes, each role containing a list of all of the
254+
nodes which match that role. This can be helpful for finding ways to query the
255+
DOM under test with [getByRole](api-queries.md#byrole)
256+
257+
```javascript
258+
import { logRoles } from '@testing-library/dom'
259+
260+
const nav = document.createElement('nav')
261+
nav.innerHTML = `
262+
<ul>
263+
<li>Item 1</li>
264+
<li>Item 2</li>
265+
</ul>`
266+
267+
console.log(logRoles(nav))
268+
269+
// navigation:
270+
//
271+
// <nav />
272+
//
273+
//
274+
// --------------------------------------------------
275+
// list:
276+
//
277+
// <ul />
278+
//
279+
//
280+
// --------------------------------------------------
281+
// listitem:
282+
//
283+
// <li />
284+
//
285+
// <li />
286+
//
287+
//
288+
// --------------------------------------------------
289+
```

0 commit comments

Comments
 (0)