-
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 6 commits
90ddec4
96c7dea
3b9d68b
729d5d1
09fdc64
cc258a9
5571b3b
3656bb0
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,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}"`; | ||
|
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.
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. Good catch, thanks! |
||
|
|
||
| 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. | ||
| * | ||
|
|
||
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 !
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.
Thanks, sounds good!