-
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
Open
JDOM10
wants to merge
6
commits into
main
Choose a base branch
from
feat/dom-to-have-accessible-description
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
90ddec4
feat(dom): add toContainHTML assertion method and corresponding tests
JDOM10 96c7dea
fix(dom): correct import statement and ensure string literals are wra…
JDOM10 3b9d68b
feat(dom): add HTML normalization in toContainHTML assertion and corr…
JDOM10 729d5d1
fix(deps): update dom-accessibility-api to version 0.5.16 in package.…
JDOM10 09fdc64
refactor(dom): rename toHaveDescription to toHaveAccessibleDescriptio…
JDOM10 cc258a9
refactor(accessibility): remove unused getAccessibleDescription funct…
JDOM10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Assertion, AssertionError } from "@assertive-ts/core"; | ||
| import { computeAccessibleDescription } from "dom-accessibility-api"; | ||
| import equal from "fast-deep-equal"; | ||
|
|
||
| import { getAccessibleDescription, isValidAriaPressed } from "./helpers/accessibility"; | ||
| import { isButtonElement, isElementEmpty } from "./helpers/dom"; | ||
| import { isValidAriaPressed } from "./helpers/accessibility"; | ||
| import { isButtonElement, isElementEmpty, normalizeHtml } from "./helpers/dom"; | ||
| import { getExpectedAndReceivedStyles } from "./helpers/styles"; | ||
|
|
||
| export class ElementAssertion<T extends Element> extends Assertion<T> { | ||
|
|
@@ -293,29 +294,29 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | |
| /** | ||
| * Asserts that the element has an accessible description. | ||
| * | ||
| * The accessible description is computed from the `aria-describedby` | ||
| * attribute, which references one or more elements by ID. The text | ||
| * content of those elements is combined to form the description. | ||
| * The accessible description is computed following the | ||
| * [accname](https://www.w3.org/TR/accname/) specification, taking | ||
| * into account `aria-describedby`, `aria-description`, and `title`, | ||
| * among others. | ||
| * | ||
| * @example | ||
| * ``` | ||
| * // Check if element has any description | ||
| * expect(element).toHaveDescription(); | ||
| * expect(element).toHaveAccessibleDescription(); | ||
| * | ||
| * // Check if element has specific description text | ||
| * expect(element).toHaveDescription('Expected description text'); | ||
| * expect(element).toHaveAccessibleDescription("Expected description text"); | ||
| * | ||
| * // Check if element description matches a regex pattern | ||
| * expect(element).toHaveDescription(/description pattern/i); | ||
| * expect(element).toHaveAccessibleDescription(/description pattern/i); | ||
| * ``` | ||
| * | ||
| * @param expectedDescription | ||
| * - Optional expected description (string or RegExp). | ||
| * @returns the assertion instance. | ||
| */ | ||
|
|
||
| public toHaveDescription(expectedDescription?: RegExp | string): this { | ||
| const description = getAccessibleDescription(this.actual); | ||
| public toHaveAccessibleDescription(expectedDescription?: RegExp | string): this { | ||
| const description = computeAccessibleDescription(this.actual); | ||
| const hasExpectedValue = expectedDescription !== undefined; | ||
|
|
||
| const matchesExpectation = (desc: string): boolean => { | ||
|
|
@@ -327,25 +328,24 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | |
| : desc === expectedDescription; | ||
| }; | ||
|
|
||
| const formatExpectation = (isRegExp: boolean): string => | ||
| isRegExp ? `matching ${expectedDescription}` : `"${expectedDescription}"`; | ||
| const expectation = expectedDescription instanceof RegExp | ||
| ? `matching ${expectedDescription}` | ||
| : `"${expectedDescription}"`; | ||
|
Comment on lines
+331
to
+333
Collaborator
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. A minor suggestion here, to consider only constructing expectation when hasExpectedValue is true. Otherwise, it evaluates to the string "undefined" when no argument is passed, which isn't currently exposed but could be misleading or cause issues if this code changes later. |
||
|
|
||
| const error = new AssertionError({ | ||
| actual: description, | ||
| expected: expectedDescription, | ||
| message: hasExpectedValue | ||
| ? `Expected the element to have description ${formatExpectation(expectedDescription instanceof RegExp)}, ` | ||
| + `but received "${description}"` | ||
| : "Expected the element to have a description", | ||
| ? `Expected the element to have accessible description ${expectation}, but received "${description}"` | ||
| : "Expected the element to have an accessible description", | ||
| }); | ||
|
|
||
| const invertedError = new AssertionError({ | ||
| actual: description, | ||
| expected: expectedDescription, | ||
| message: hasExpectedValue | ||
| ? `Expected the element NOT to have description ${formatExpectation(expectedDescription instanceof RegExp)}, ` | ||
| + `but received "${description}"` | ||
| : `Expected the element NOT to have a description, but received "${description}"`, | ||
| ? `Expected the element NOT to have accessible description ${expectation}, but received "${description}"` | ||
| : `Expected the element NOT to have an accessible description, but received "${description}"`, | ||
| }); | ||
|
|
||
| return this.execute({ | ||
|
|
@@ -434,6 +434,51 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that the element contains the specified HTML. | ||
| * | ||
| * The expected HTML is normalized through a detached element before | ||
| * comparison, so differences in attribute quoting (single vs double quotes), | ||
| * tag case, and whitespace between attributes are ignored. Attribute ordering | ||
| * and whitespace within text content are still significant. | ||
| * | ||
| * @example | ||
| * ``` | ||
| * expect(container).toContainHTML('<span>Hello</span>'); | ||
| * expect(container).toContainHTML("<div class='foo'>Bar</div>"); | ||
| * ``` | ||
| * | ||
| * @param htmlText The HTML text that should be contained in the element | ||
| * @returns the assertion instance. | ||
| */ | ||
| public toContainHTML(htmlText: string): this { | ||
| if (typeof htmlText !== "string") { | ||
| throw new Error(`.toContainHTML() expects a string value, got ${typeof htmlText}`); | ||
| } | ||
|
|
||
| if (htmlText === "") { | ||
| throw new Error(".toContainHTML() expects a non-empty string"); | ||
| } | ||
|
|
||
| const error = new AssertionError({ | ||
| actual: this.actual, | ||
| expected: htmlText, | ||
| message: `Expected the element to contain HTML: ${htmlText}`, | ||
| }); | ||
|
|
||
| const invertedError = new AssertionError({ | ||
| actual: this.actual, | ||
| expected: htmlText, | ||
| message: `Expected the element NOT to contain HTML: ${htmlText}`, | ||
| }); | ||
|
|
||
| return this.execute({ | ||
| assertWhen: this.actual.outerHTML.includes(normalizeHtml(htmlText, this.actual.ownerDocument)), | ||
| error, | ||
| invertedError, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to assert the presence or absence of class names. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Non-blocking suggestion: this helper is clear as is, but it might be worth extracting it into helpers/accessibility.ts as we would end up adding other matchers in the future (e.g. toHaveAccessibleName) that could reuse the same matching logic 😸 let me know what you think !