Skip to content

Commit

Permalink
feat(ByRole): Allow filter by busy state (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Mar 24, 2023
1 parent fb069c9 commit 8c40a21
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/ariaAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ test('`expanded` throws on unsupported roles', () => {
)
})

test('`busy` throws on unsupported roles', () => {
const {getByRole} = render(
`<div aria-busy="true" role="none">Hello, Dave!</div>`,
)
expect(() =>
getByRole('none', {busy: true}),
).toThrowErrorMatchingInlineSnapshot(
`"aria-busy" is not supported on role "none".`,
)
})

test('`busy: true|false` matches `busy` regions', () => {
const {getByRole} = renderIntoDocument(
`<div>
<div role="log" aria-busy="true" />
<div role="log" aria-busy="false" />
</div>`,
)
expect(getByRole('log', {busy: true})).toBeInTheDocument()
expect(getByRole('log', {busy: false})).toBeInTheDocument()
})

test('`checked: true|false` matches `checked` checkboxes', () => {
const {getByRole} = renderIntoDocument(
`<div>
Expand Down
15 changes: 15 additions & 0 deletions src/queries/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'aria-query'
import {
computeAriaSelected,
computeAriaBusy,
computeAriaChecked,
computeAriaPressed,
computeAriaCurrent,
Expand Down Expand Up @@ -42,6 +43,7 @@ const queryAllByRole: AllByRole = (
description,
queryFallbacks = false,
selected,
busy,
checked,
pressed,
current,
Expand All @@ -61,6 +63,16 @@ const queryAllByRole: AllByRole = (
}
}

if (busy !== undefined) {
// guard against unknown roles
if (
allRoles.get(role as ARIARoleDefinitionKey)?.props['aria-busy'] ===
undefined
) {
throw new Error(`"aria-busy" is not supported on role "${role}".`)
}
}

if (checked !== undefined) {
// guard against unknown roles
if (
Expand Down Expand Up @@ -152,6 +164,9 @@ const queryAllByRole: AllByRole = (
if (selected !== undefined) {
return selected === computeAriaSelected(element)
}
if (busy !== undefined) {
return busy === computeAriaBusy(element)
}
if (checked !== undefined) {
return checked === computeAriaChecked(element)
}
Expand Down
10 changes: 10 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ function computeAriaSelected(element) {
return checkBooleanAttribute(element, 'aria-selected')
}

/**
* @param {Element} element -
* @returns {boolean} -
*/
function computeAriaBusy(element) {
// https://www.w3.org/TR/wai-aria-1.1/#aria-busy
return element.getAttribute('aria-busy') === 'true'
}

/**
* @param {Element} element -
* @returns {boolean | undefined} - false/true if (not)checked, undefined if not checked-able
Expand Down Expand Up @@ -333,6 +342,7 @@ export {
prettyRoles,
isInaccessible,
computeAriaSelected,
computeAriaBusy,
computeAriaChecked,
computeAriaPressed,
computeAriaCurrent,
Expand Down
5 changes: 5 additions & 0 deletions types/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export interface ByRoleOptions {
* selected in the accessibility tree, i.e., `aria-selected="true"`
*/
selected?: boolean
/**
* If true only includes elements in the query set that are marked as
* busy in the accessibility tree, i.e., `aria-busy="true"`
*/
busy?: boolean
/**
* If true only includes elements in the query set that are marked as
* checked in the accessibility tree, i.e., `aria-checked="true"`
Expand Down

0 comments on commit 8c40a21

Please sign in to comment.