Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
90ddec496c7dea3b9d68b729d5d109fdc64cc258a95571b3b3656bb011f4cf35498a6cee49db0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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? CollaboratorAuthor 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}"`; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.