Skip to content

feat(ByRole): Add 'level' option for *ByRole('heading') #757

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

Conversation

winterlamon
Copy link
Contributor

@winterlamon winterlamon commented Aug 27, 2020

What:

Adds the level option for the *ByRole('heading') queries as proposed in #743

getByRole('heading', { level: number })

Why:

There may be situations in which headings should be queried by a specific level rather than all heading levels.

How:

Using the level option in the *ByRole('heading') queries includes the elements with the "heading" role matching the indicated level, either by the semantic HTML heading elements <h1>-<h6> or matching the aria-level attribute.

// Given this dom
<div>
  <h1>H1</h1>
  <h2>First H2</h2>
  <h3>H3</h3>
  <div role="heading" aria-level="2">Second H2</div>
  <button>Click Me</button>
</div>

getByRole('heading', { level: 1 })
// output: <h1>H1</h1>

getAllByRole('heading', { level: 2 })
// output: [<h2>First H2</h2>, <div role="heading" aria-level="2">Second H2</div>]

Any role other than "heading" using the level option, will throw an error.

getAllByRole('button', { level: 2 })
// `Role "button" cannot have "level" option.`

Checklist:

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 27, 2020

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 0b1402e:

Sandbox Source
kentcdodds/react-testing-library-examples Configuration

@codecov
Copy link

codecov bot commented Aug 27, 2020

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #757   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          667       675    +8     
  Branches       176       181    +5     
=========================================
+ Hits           667       675    +8     
Impacted Files Coverage Δ
src/queries/role.js 100.00% <100.00%> (ø)
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 a5f8657...0b1402e. Read the comment docs.

@eps1lon eps1lon added the enhancement New feature or request label Aug 27, 2020
Copy link
Member

@eps1lon eps1lon 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. Just some editorial changes.

@winterlamon winterlamon requested a review from eps1lon August 27, 2020 15:46
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.

Solid 👍

@kentcdodds kentcdodds merged commit ea52c91 into testing-library:master Aug 27, 2020
@kentcdodds
Copy link
Member

@all-contributors please add @winterlamon for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

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

@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.23.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants