-
Notifications
You must be signed in to change notification settings - Fork 2
feat(dom): DOM - toHaveDescription #163
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
ffde214
81c16cb
cccddab
ade1476
382f08d
e9b598c
982727a
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 |
|---|---|---|
|
|
@@ -73,3 +73,34 @@ export const getExpectedAndReceivedStyles = | |
| elementProcessedStyle, | ||
| ]; | ||
| }; | ||
|
|
||
| function normalizeText(text: string): string { | ||
| return text.replace(/\s+/g, " ").trim(); | ||
| } | ||
|
|
||
| export function getAccessibleDescription(actual: Element): string { | ||
| const ariaDescribedBy = actual.getAttribute("aria-describedby"); | ||
|
|
||
| if (!ariaDescribedBy) { | ||
| return ""; | ||
| } | ||
|
|
||
| const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean); | ||
|
|
||
| const getElementText = (id: string): string | null => { | ||
| const element = actual.ownerDocument.getElementById(id); | ||
|
|
||
| if (!element || !element.textContent) { | ||
| return null; | ||
| } | ||
|
|
||
| return element.textContent; | ||
| }; | ||
|
|
||
|
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. Same as above, what if getElementText is null? We need to add a conditional check to process only data that is not null
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. Thanks for noticing this. I have added a conditional check to verify if the description is null or an empty string. |
||
| const combinedText = descriptionIds | ||
| .map(getElementText) | ||
| .filter((text): text is string => text !== null) | ||
| .join(" "); | ||
|
|
||
| return normalizeText(combinedText); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { ReactElement } from "react"; | ||
|
|
||
| export function DescriptionTestComponent(): ReactElement { | ||
| return ( | ||
| <div> | ||
| <div id="description-1">{"This is a description"}</div> | ||
| <div id="description-2">{"Additional info"}</div> | ||
| <div id="description-3">{"More details here"}</div> | ||
|
|
||
| <button aria-describedby="description-1" data-testid="button-single"> | ||
| {"Button with single description"} | ||
| </button> | ||
|
|
||
| <button aria-describedby="description-1 description-2" data-testid="button-multiple"> | ||
| {"Button with multiple descriptions"} | ||
| </button> | ||
|
|
||
| <button data-testid="button-no-description"> | ||
| {"Button without description"} | ||
| </button> | ||
|
|
||
| <input | ||
| type="text" | ||
| aria-describedby="description-3" | ||
| data-testid="input-with-description" | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
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.
could you put this in one line please so it has same structure as the other matchers please
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.
I have to put like that because of the lenght of the line due to linter configuration!