Skip to content
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

fix: Consider explicit role when pretty printing roles #568

Merged
merged 2 commits into from May 13, 2020

Conversation

eps1lon
Copy link
Member

@eps1lon eps1lon commented May 12, 2020

What:

Consider explicit roles first when pretty printing roles.

Why:

getRoles printed a different role than what queryAllByRole could query. Closes #553

How:

Check for existence of role attribute first and use the first role from that list. I haven't put much thought into fallback roles but we can discuss it when it becomes an issue.

Technically this isn't correct. html-aria disallows (some) roles for certain HTML elements. But we didn't consider this for queryAllByRole either and it isn't clear if we should restrict it (e.g. chrome's a11y pane does not).

Checklist:

  • [ ] Documentation added to the
    docs site
  • [ ] I've prepared a PR for types targeting
    DefinitelyTyped
  • Tests
  • Ready to be merged

@eps1lon eps1lon added the bug Something isn't working label May 12, 2020
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 17bb655:

Sandbox Source
upbeat-moon-n0lyn Configuration
react-testing-library demo Issue #553

@codecov
Copy link

codecov bot commented May 12, 2020

Codecov Report

Merging #568 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #568   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        23           
  Lines          459       462    +3     
  Branches       113       114    +1     
=========================================
+ Hits           459       462    +3     
Impacted Files Coverage Δ
src/role-helpers.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 217b2a8...17bb655. Read the comment docs.

@eps1lon eps1lon changed the title Fix/role specificity fix: Consider explicit role when pretty printing roles May 12, 2020
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Thanks for this! Just a few comments.

let roles = []
// TODO: This violates html-aria which does not allow any role on every element
if (node.hasAttribute('role')) {
roles = node.getAttribute('role').split(' ').slice(0, 1)
Copy link
Member

Choose a reason for hiding this comment

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

Can an element have multiple roles? If so, is there a reason we're only getting the first one here?

Copy link
Member Author

Choose a reason for hiding this comment

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

An element can only have a single role. We only consider arrays right now for historic reasons. At some point aria-query will return a single role but this requires more work that isn't actually blocking this PR.

If you pass in multiple roles then those are considered fallback roles. We allow querying them but haven't decided yet if we want to include it in the error message. It gets quite complicate since you have to branch one more time.

I don't think this is a concern of this PR. aria-query returning multiple roles or queryFallback not being considered can be solved independently.

@@ -134,7 +134,13 @@ function getRoles(container, {hidden = false} = {}) {
return hidden === false ? isInaccessible(element) === false : true
})
.reduce((acc, node) => {
const roles = getImplicitAriaRoles(node)
let roles = []
// TODO: This violates html-aria which does not allow any role on every element
Copy link
Member

Choose a reason for hiding this comment

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

I'd love to deal with this TODO before merging if it's not a huge effort.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, just noticed what you said about this. Maybe rather than TODO this should link to some explanation for why we're doing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a TODO though. The way we currently handle it is wrong. But this can be fixed in a separate PR so that we're able to revert if multiple roles are required still (also better for changelog).

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks great 👍

@kentcdodds kentcdodds merged commit 04b027c into testing-library:master May 13, 2020
@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.5.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@eps1lon eps1lon deleted the fix/role-specificity branch September 7, 2021 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getByRole's error message is incorrect on button with role "tab"
2 participants