Skip to content

Commit

Permalink
feat: Add strictVisibility check option to ByRole queries
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 1, 2024
1 parent a86c54c commit 6ecbf77
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
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', {strictVisibility: true})
expect(strictResults).toHaveLength(1)

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

test('getAll* matchers return an array', () => {
const {
getAllByAltText,
Expand Down
16 changes: 11 additions & 5 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 @@ -296,11 +298,15 @@ const queryAllByRole: AllByRole = (
)
})
.filter(element => {
return hidden === false
? isInaccessible(element, {
isSubtreeInaccessible: cachedIsSubtreeInaccessible,
}) === false
: true
if (strictVisibilityCheck) {
return hidden === false
? isInaccessible(element, {
isSubtreeInaccessible: cachedIsSubtreeInaccessible,
}) === false
: true
} else {
return hidden === false ? isInaccessibleLoose(element) === false : true
}
})
}

Expand Down
21 changes: 21 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ 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) {
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
}

return false
}

function getImplicitAriaRoles(currentNode) {
// eslint bug here:
// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -382,6 +402,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

0 comments on commit 6ecbf77

Please sign in to comment.