Skip to content

Added docs for getRoles and logRoles #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions docs/dom-testing-library/api-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ module.exports = {

## `buildQueries`

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

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

### Using other assertion libraries

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

<!--END_DOCUSAURUS_CODE_TABS-->

## `getRoles`

This function allows iteration over the implicit ARIA roles represented in a
given tree of DOM nodes.

It returns an object, indexed by role name, with each value being an array of
elements which have that implicit ARIA role.

See
[ARIA in HTML](https://www.w3.org/TR/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html)
for more information about implicit ARIA roles.

```javascript
import { getRoles } from '@testing-library/dom'

const nav = document.createElement('nav')
nav.innerHTML = `
<ul name="menu">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since name isn't really a valid attribute for ul/li elements, I'd suggest removing it, so there's also less code to read in the example.

What do you think about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I didn't realize name isn't valid for ul/li.

I think we should have some attribute in there to illustrate how it prints out the attributes. Thoughts?

Assuming the above, I'll probably change name="menu" to type="circle" on the ul and give the li elements a 'value' attribute in place of 'name'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed here: da985145

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know that until I checked right now, but type attr on <ul> elements is discouraged: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul#attr-type

Also, looks like value on <ul> list items doesn't make much sense: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li#attr-value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. swing and a miss, haha

nav->ul->li was nice because it is three distinct roles in a real world kind of situation.

Maybe it doesn't matter so much if the example illustrates how the output prints attributes, so there's always the option of getting rid of the attributes in the example (as your initial suggestion).

Though I guess we could always add class and/or id attributes to illustrate a more real-world output.

I'm gonna push one more commit this evening.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;) no prob.

Maybe it doesn't matter so much if the example illustrates how the output prints attributes, so there's always the option of getting rid of the attributes in the example.

This still makes sense to me. The main goal of the example is to illustrate what a grouping-by-role looks like, not the attribute themselves.

Thanks for your work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've pulled the attributes off the elements completely. I agree that it's the correct call. Sorry for the delay, distracted by work/holiday.

<li name="item-1">Item 1</li>
<li name="item-2">Item 2</li>
</ul>`
console.log(getRoles(nav))

// Object {
// navigation: [<nav />],
// list: [<ul name="menu" />],
// listitem: [<li name="item-1" />, <li name="item-2" />]
// }
```

## Debugging

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

This function is what also powers
[the automatic debugging output described above](#debugging).

### `logRoles`

This helper function can be used to print out a list of all the implicit ARIA
roles within a tree of DOM nodes, each role containing a list of all of the
nodes which match that role. This can be helpful for finding ways to query the
DOM under test with [getByRole](api-queries.md#byrole)

```javascript
import { logRoles } from '@testing-library/dom'

const nav = document.createElement('nav')
nav.innerHTML = `
<ul type="circle">
<li value="1">Item 1</li>
<li value="2">Item 2</li>
</ul>`

console.log(logRoles(nav))

// navigation:
//
// <nav />
//
//
// --------------------------------------------------
// list:
//
// <ul
// type="circle"
// />
//
//
// --------------------------------------------------
// listitem:
//
// <li
// value="1"
// />
//
// <li
// value="2"
// />
//
// --------------------------------------------------
```