Skip to content
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
22 changes: 22 additions & 0 deletions src/__tests__/ariaAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,25 @@ test('`expanded: true|false` matches `expanded` elements with proper role', () =
expect(getByRole('button', {expanded: true})).toBeInTheDocument()
expect(getByRole('button', {expanded: false})).toBeInTheDocument()
})

test('`expected: true|false` matches `disabled` buttons', () => {
const {getByRole} = renderIntoDocument(
`<div>
<button disabled />
<button />
</div>`,
)
expect(getByRole('button', {disabled: true})).toBeInTheDocument()
expect(getByRole('button', {disabled: false})).toBeInTheDocument()
})

test('`expected: true|false` matches `aria-disabled` buttons', () => {
const {getByRole} = renderIntoDocument(
`<div>
<button aria-disabled="true" />
<button aria-disabled="false" />
</div>`,
)
expect(getByRole('button', {disabled: true})).toBeInTheDocument()
expect(getByRole('button', {disabled: false})).toBeInTheDocument()
})
5 changes: 5 additions & 0 deletions src/queries/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
computeAriaValueMin,
computeAriaValueText,
computeHeadingLevel,
computeDisabled,
getImplicitAriaRoles,
prettyRoles,
isInaccessible,
Expand Down Expand Up @@ -53,6 +54,7 @@ const queryAllByRole: AllByRole = (
pressed,
current,
level,
disabled,
expanded,
value: {
now: valueNow,
Expand Down Expand Up @@ -233,6 +235,9 @@ const queryAllByRole: AllByRole = (
if (level !== undefined) {
return level === computeHeadingLevel(element)
}
if (disabled !== undefined) {
return disabled === computeDisabled(element)
}
if (
valueNow !== undefined ||
valueMax !== undefined ||
Expand Down
9 changes: 9 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ function computeAriaValueText(element) {
return valueText === null ? undefined : valueText
}

/**
* @param {Element} element
* @returns {boolean | undefined}
*/
function computeDisabled(element) {
return element.disabled || element.getAttribute('aria-disabled') === 'true'
}

export {
getRoles,
logRoles,
Expand All @@ -387,5 +395,6 @@ export {
computeAriaValueMax,
computeAriaValueMin,
computeAriaValueText,
computeDisabled,
computeHeadingLevel,
}
1 change: 1 addition & 0 deletions types/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface ByRoleOptions {
| RegExp
| string
| ((accessibleDescription: string, element: Element) => boolean)
disabled?: boolean
}

export type AllByRole<T extends HTMLElement = HTMLElement> = (
Expand Down