Skip to content

feat(ByRole): Add support for fallback roles #418

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 2 commits into from
Jan 28, 2020
Merged

feat(ByRole): Add support for fallback roles #418

merged 2 commits into from
Jan 28, 2020

Conversation

iAziz786
Copy link
Contributor

What: Adding support for *ByRole query to query multiple roles.

Why:

The *ByRole query should support querying elements that have multiple role values defined. the ARIA spec defines the role attribute as a whitespace delimited list of values, of which the engine will choose the first supported value. See w3.org/TR/wai-aria-1.2/#introroles. Querying by role when testing should take this into account and find roles matching any of the items in the list rather than only the entire role attribute.

Closes: #411

How: With the help of a flag named queryFallbacks, *ByRole queries can query multiple roles. If the flag is missing and you're trying to query a role that is located on the right side, then this will throw an error. The working is same as mentioned in (#411 (comment))

Checklist:

  • Documentation added to the
    docs site
  • I've prepared a PR for types targeting
    DefinitelyTyped (I'm not sure whether it's needed or not)
  • Tests
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 13, 2019

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 97141f7:

Sandbox Source
happy-kilby-lmtt6 Configuration

@codecov
Copy link

codecov bot commented Dec 13, 2019

Codecov Report

Merging #418 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #418   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          22     22           
  Lines         374    381    +7     
  Branches       87     89    +2     
=====================================
+ Hits          374    381    +7
Impacted Files Coverage Δ
src/queries/role.js 100% <100%> (ø) ⬆️
src/events.js 100% <0%> (ø) ⬆️

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 a2c3472...97141f7. Read the comment docs.

@iAziz786
Copy link
Contributor Author

iAziz786 commented Dec 13, 2019

@kentcdodds Since this is my first PR I need some guidance to merge it ASAP. I am aware of that I have to update the docs. I'm not sure whether to create the PR for it before the merge of the changes or after it

Please let me know if I missed something.

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! Great job! Just a few comments. Let me know if you have any questions.

@@ -433,6 +434,7 @@ test('queryAllByRole returns semantic html elements', () => {
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
expect(queryAllByRole(/img/i)).toHaveLength(1)
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
expect(queryAllByRole('meter')).toHaveLength(1)
expect(queryAllByRole('progressbar')).toHaveLength(0)
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)

@@ -482,6 +485,7 @@ test('getAll* matchers return an array', () => {
expect(getAllByDisplayValue(/cars$/)).toHaveLength(2)
expect(getAllByText(/^where/i)).toHaveLength(1)
expect(getAllByRole(/container/i)).toHaveLength(1)
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
expect(getAllByRole('meter')).toHaveLength(1)
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)

Comment on lines +60 to +67
await expect(
findByRole('progressbar', {queryFallbacks: true}),
).resolves.toBeTruthy()
await expect(
findAllByRole('progressbar', {queryFallbacks: true}),
).resolves.toHaveLength(1)
Copy link
Member

Choose a reason for hiding this comment

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

I can't make a suggestion in GitHub for this, but I would suggest this change to:

  await expect(findByRole('meter')).resolves.toBeTruthy()
  await expect(findAllByRole('meter')).resolves.toHaveLength(1)
  await expect(findByRole('progressbar', {queryFallbacks: true})).resolves.toBeTruthy()
  await expect(findAllByRole('progressbar', {queryFallbacks: true})).resolves.toHaveLength(1)

Thanks!

@kentcdodds
Copy link
Member

Also, sorry that it took me so long to get to this. I took a little break 😅

Thanks for your work!

@iAziz786
Copy link
Contributor Author

Welcome back from holidays, Kent. I hope it went well.

Thanks for the review. I have lost some track on this, but I'll push the suggested changes ASAP.

@iAziz786
Copy link
Contributor Author

@kentcdodds I have made the review changes.

.some(text => matcher(text, node, role, matchNormalizer))
}
// if a custom normalizer is passed then let normalizer handle the role value
if (normalizer) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test specifically covering this case? We should.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it is covered in text-matcher.js.

If this is not the test case you're talking about then can you please guide me where to add the test case for it? That will be very helpful :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kentcdodds ping you to remind again.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. That's fine. Thank you!

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.

Looking good! Could you add one test case?

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.

This is great! Thank you for your help on this @iAziz786!

@kentcdodds kentcdodds merged commit 91d05c1 into testing-library:master Jan 28, 2020
@kentcdodds
Copy link
Member

Could you make a pull request to add this to the docs please? https://github.com/testing-library/testing-library-docs

@kentcdodds
Copy link
Member

@all-contributors please add @iAziz786 code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @iAziz786! 🎉

@kentcdodds
Copy link
Member

🎉 This PR is included in version 6.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iAziz786
Copy link
Contributor Author

Yes, @kentcdodds, I'll surely add this to the docs. I need a little time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getByRole should support fallback roles
2 participants