-
Notifications
You must be signed in to change notification settings - Fork 3
feat(dom): toHaveAccessibleDescription #174
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
base: main
Are you sure you want to change the base?
Changes from all commits
90ddec4
96c7dea
3b9d68b
729d5d1
09fdc64
cc258a9
5571b3b
3656bb0
11f4cf3
5498a6c
ee49db0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,24 @@ | ||
| function normalizeText(text: string): string { | ||
| return text.replace(/\s+/g, " ").trim(); | ||
| export function isValidAriaPressed(element: Element): boolean { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch on cleanning up this helper 🚀 |
||
| const pressedAttribute = element.getAttribute("aria-pressed"); | ||
| return pressedAttribute !== null && ["true", "false", "mixed"].includes(pressedAttribute); | ||
| } | ||
|
|
||
| export function getAccessibleDescription(actual: Element): string { | ||
| const ariaDescribedBy = actual.getAttribute("aria-describedby"); | ||
|
|
||
| if (!ariaDescribedBy) { | ||
| return ""; | ||
| export function matchesAccessibleExpectation(actual: string, expected?: RegExp | string): boolean { | ||
| if (expected === undefined) { | ||
| return Boolean(actual); | ||
| } | ||
|
|
||
| const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean); | ||
|
|
||
| const getElementText = (id: string): null | string => { | ||
| const element = actual.ownerDocument.getElementById(id); | ||
|
|
||
| if (!element || !element.textContent) { | ||
| return null; | ||
| } | ||
|
|
||
| return element.textContent; | ||
| }; | ||
|
|
||
| const combinedText = descriptionIds | ||
| .map(getElementText) | ||
| .filter((text): text is string => text !== null) | ||
| .join(" "); | ||
|
|
||
| return normalizeText(combinedText); | ||
| return expected instanceof RegExp | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we returning this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because expected is RegExp | string — the check picks the right comparison at runtime and narrows the type so .test() type-checks. |
||
| ? expected.test(actual) | ||
| : actual === expected; | ||
| } | ||
|
|
||
| export function isValidAriaPressed(element: Element): boolean { | ||
| const pressedAttribute = element.getAttribute("aria-pressed"); | ||
| return pressedAttribute !== null && ["true", "false", "mixed"].includes(pressedAttribute); | ||
| export function describeAccessibleExpectation(expected?: RegExp | string): string { | ||
| if (expected === undefined) { | ||
| return ""; | ||
| } | ||
|
|
||
| return expected instanceof RegExp | ||
| ? `matching ${expected}` | ||
| : `"${expected}"`; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about adding an alias to
toHaveDescriptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave it as is. toHaveDescription is ambiguous: it reads like it's checking a description attribute or raw text content rather than the ARIA-computed description. This ticket is also moving toward covering the ARIA-related assertions, so the explicit name fits better.