-
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
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,30 @@ 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") || ""; | ||
|
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. What about adding a conditional check to verify if ariaDescribedBy is not an empty string and then you can extract the ids 👀. In this scenario it may not be critical but for some other cases you may face errors or functions that do not accept empty strings which is why we need to verify that the string has a valid value. |
||
| const descriptionIds = ariaDescribedBy | ||
| .split(/\s+/) | ||
| .filter(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. what is this line doing?
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. These lines parse the IDs into a clean array by splitting the string and filtering out extra whitespace, so we can get the descriptions from differents elements at the same time. |
||
|
|
||
| if (descriptionIds.length === 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| const getElementText = (id: string): string | null => { | ||
| const element = actual.ownerDocument.getElementById(id); | ||
| return element?.textContent || null; | ||
| }; | ||
|
|
||
|
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 |
||
| return normalizeText( | ||
| descriptionIds | ||
| .map(getElementText) | ||
| .filter((text): text is string => text !== null) | ||
| .join(" "), | ||
| ); | ||
| } | ||
| 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!