Skip to content

feat: Add strictVisibility check option to ByRole queries #1331

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,37 @@ test('queryAllByRole returns semantic html elements', () => {
expect(queryAllByRole('listbox')).toHaveLength(1)
})

test('getAllByRole matchers with strictVisibility enabled and disabled', () => {
const {getAllByRole} = render(`
<div aria-hidden="true">
<button>Aria Hidden Button</button>
</div>
<div style="display: none;">
<button>Display None Button</button>
</div>
<div style="visibility: hidden;">
<button>Visibility Hidden Button</button>
</div>
<div style="visibility: hidden;">
<div>
<button>Deep Visibility Hidden Button</button>
</div>
</div>
<div>
<button>Visible Button</button>
</div>
`)

const defaultResults = getAllByRole('button')
expect(defaultResults).toHaveLength(1)

const strictResults = getAllByRole('button', {strictVisibilityCheck: true})
expect(strictResults).toHaveLength(1)

const looseResults = getAllByRole('button', {strictVisibilityCheck: false})
expect(looseResults).toHaveLength(1)
})

test('getAll* matchers return an array', () => {
const {
getAllByAltText,
Expand Down
10 changes: 7 additions & 3 deletions src/queries/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
prettyRoles,
isInaccessible,
isSubtreeInaccessible,
isInaccessibleLoose,
} from '../role-helpers'
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {checkContainerType} from '../helpers'
Expand Down Expand Up @@ -54,6 +55,7 @@ const queryAllByRole: AllByRole = (
current,
level,
expanded,
strictVisibilityCheck = true,
value: {
now: valueNow,
min: valueMin,
Expand Down Expand Up @@ -297,9 +299,11 @@ const queryAllByRole: AllByRole = (
})
.filter(element => {
return hidden === false
? isInaccessible(element, {
isSubtreeInaccessible: cachedIsSubtreeInaccessible,
}) === false
? strictVisibilityCheck
? isInaccessible(element, {
isSubtreeInaccessible: cachedIsSubtreeInaccessible,
}) === false
: isInaccessibleLoose(element) === false
: true
})
}
Expand Down
23 changes: 23 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ function isInaccessible(element, options = {}) {
return false
}

/**
* A fast check to see if an element is inaccessible.
* @param {Element} element
* @returns {boolean} true if exluded, otherwise false
*/
function isInaccessibleLoose(element) {
do {
const explictlyHidden =
element.style.visibility === 'hidden' || element.style.display === 'none'
if (explictlyHidden) {
return true
}

const ariaHidden = element.getAttribute('aria-hidden') === 'true'
if (ariaHidden) {
return true
}
} while ((element = element.parentElement))

return false
}

function getImplicitAriaRoles(currentNode) {
// eslint bug here:
// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -382,6 +404,7 @@ export {
isSubtreeInaccessible,
prettyRoles,
isInaccessible,
isInaccessibleLoose,
computeAriaSelected,
computeAriaBusy,
computeAriaChecked,
Expand Down
11 changes: 11 additions & 0 deletions types/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ export interface ByRoleOptions {
* the `aria-level` attribute.
*/
level?: number

/**
* Whether or not to strictly check the visibliity of the element. Doing so
* can cause issues with the performance of tests, because it requires the DOM tree
* is traversed, and the `getComputedStyle` function is called on each element.
*
* If you're finding that your tests are slow, you may want to disable this option.
* @default true
*/
strictVisibilityCheck?: boolean

value?: {
now?: number
min?: number
Expand Down
Loading