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): toBePressed and toBePartiallyPressed#172
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d4d40c5582a7828234a4856226c6f691b49ba8a1ed1c8407e6259f5a32ea7bc55820fdFile 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,8 +1,8 @@ | ||
| import { Assertion, AssertionError } from "@assertive-ts/core"; | ||
| import equal from "fast-deep-equal"; | ||
| import { getAccessibleDescription } from "./helpers/accessibility"; | ||
| import { isElementEmpty } from "./helpers/dom"; | ||
| import { getAccessibleDescription, isValidAriaPressed } from "./helpers/accessibility"; | ||
| import { isButtonElement, isElementEmpty } from "./helpers/dom"; | ||
| import { getExpectedAndReceivedStyles } from "./helpers/styles"; | ||
| export class ElementAssertion<T extends Element> extends Assertion<T> { | ||
| @@ -355,6 +355,85 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | ||
| }); | ||
| } | ||
| /** | ||
| * Asserts that the element is a pressed button. | ||
| * | ||
| * @example | ||
| * // It takes into account aria-pressed attribute | ||
| * expect(element).toBePressed(); | ||
| * expect(element).not.toBePressed(); | ||
| * | ||
| * @returns the assertion instance. | ||
| */ | ||
| public toBePressed(): this { | ||
| if (!isButtonElement(this.actual) || !isValidAriaPressed(this.actual)) { | ||
| throw new Error( | ||
| 'Expected a button or button-like control with a valid pressed state: "true", "false", or "mixed".', | ||
| ); | ||
| } | ||
| const pressedAttributeValue = this.actual.getAttribute("aria-pressed"); | ||
| const isPressed = pressedAttributeValue === "true"; | ||
| const error = new AssertionError({ | ||
| actual: pressedAttributeValue, | ||
| expected: "true", | ||
| message: `Expected the element to be pressed, but received aria-pressed="${pressedAttributeValue}"`, | ||
| }); | ||
| const invertedError = new AssertionError({ | ||
| actual: pressedAttributeValue, | ||
| expected: "false", | ||
| message: `Expected the element to NOT be pressed, but received aria-pressed="${pressedAttributeValue}"`, | ||
| }); | ||
| return this.execute({ | ||
| assertWhen: isPressed, | ||
| error, | ||
| invertedError, | ||
| }); | ||
| } | ||
| /** | ||
| * Asserts that the element is a partially pressed button. | ||
| * | ||
KeylaMunnoz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * @example | ||
| * // It takes into account aria-pressed attribute | ||
| * expect(element).toBePartiallyPressed(); | ||
| * expect(element).not.toBePartiallyPressed(); | ||
| * | ||
KeylaMunnoz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * @returns the assertion instance. | ||
| */ | ||
| public toBePartiallyPressed(): this { | ||
| if (!isButtonElement(this.actual) || !isValidAriaPressed(this.actual)) { | ||
| throw new Error( | ||
| 'Expected a button or button-like control with a valid pressed state: "true", "false", or "mixed".', | ||
| ); | ||
| } | ||
| const pressedAttributeValue = this.actual.getAttribute("aria-pressed"); | ||
| const isPartiallyPressed = pressedAttributeValue === "mixed"; | ||
| const error = new AssertionError({ | ||
| actual: pressedAttributeValue, | ||
| expected: "mixed", | ||
| message: `Expected the element to be partially pressed, but received aria-pressed="${pressedAttributeValue}"`, | ||
| }); | ||
| const invertedError = new AssertionError({ | ||
| actual: pressedAttributeValue, | ||
| message: `Expected the element to NOT be partially pressed, but received aria-pressed="${pressedAttributeValue}"`, | ||
| }); | ||
| return this.execute({ | ||
| assertWhen: isPartiallyPressed, | ||
| error, | ||
| invertedError, | ||
| }); | ||
| } | ||
| /** | ||
| * Helper method to assert the presence or absence of class names. | ||
| * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,3 +4,17 @@ export function isElementEmpty(element: Element): boolean { | ||
| const nonCommentChildNodes = [...element.childNodes].filter(child => child.nodeType !== COMMENT_NODE_TYPE); | ||
| return nonCommentChildNodes.length === 0; | ||
| } | ||
KeylaMunnoz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| export function isButtonElement(element: Element): boolean { | ||
| const roles = (element.getAttribute("role") || "") | ||
| .split(" ") | ||
| .map(role => role.trim()); | ||
| const tagName = element.tagName.toLowerCase(); | ||
| const type = element.getAttribute("type"); | ||
| const isNativeButton = tagName === "button" || (tagName === "input" && type === "button"); | ||
| const hasButtonRole = roles.includes("button"); | ||
| return isNativeButton || hasButtonRole; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { ReactElement } from "react"; | ||
| export function PressedTestComponent(): ReactElement { | ||
| return ( | ||
| <div> | ||
| {/* <button> variants */} | ||
| <button data-testid="button-pressed" aria-pressed="true">{"Pressed"}</button> | ||
| <button data-testid="button-not-pressed" aria-pressed="false">{"Not pressed"}</button> | ||
| <button data-testid="button-mixed" aria-pressed="mixed">{"Mixed"}</button> | ||
| <button data-testid="button-no-aria-pressed">{"No aria-pressed"}</button> | ||
| {/* <input type="button"> variants */} | ||
| <input data-testid="input-button-pressed" type="button" aria-pressed="true" /> | ||
| <input data-testid="input-button-not-pressed" type="button" aria-pressed="false" /> | ||
| <input data-testid="input-button-mixed" type="button" aria-pressed="mixed" /> | ||
| {/* role="button" variants */} | ||
| <div data-testid="role-button-pressed" role="button" aria-pressed="true">{"Pressed"}</div> | ||
| <div data-testid="role-button-not-pressed" role="button" aria-pressed="false">{"Not pressed"}</div> | ||
| <div data-testid="role-button-mixed" role="button" aria-pressed="mixed">{"Mixed"}</div> | ||
| {/* invalid element – no button role/tag */} | ||
| <div data-testid="non-button-element" aria-pressed="true">{"Not a button"}</div> | ||
| </div> | ||
| ); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.