-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
d4d40c5
582a782
8234a48
56226c6
f691b49
ba8a1ed
1c8407e
6259f5a
32ea7bc
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,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,87 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that the element is a pressed button. | ||
| * | ||
| * @example | ||
| * const toggleButton = document.querySelector('#toggle'); | ||
|
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. we can remove this const |
||
| * expect(toggleButton).toBePressed(); // passes if aria-pressed="true" | ||
| * expect(toggleButton).not.toBePressed(); // fails if aria-pressed="true" | ||
|
Comment on lines
+363
to
+364
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. |
||
| * | ||
| * @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.
|
||
| * @example | ||
| * const toggleButton = document.querySelector('#toggle'); | ||
| * expect(toggleButton).toBePartiallyPressed(); | ||
| * // passes if aria-pressed="mixed" | ||
| * expect(toggleButton).not.toBePartiallyPressed(); | ||
| * // fails if aria-pressed="mixed" | ||
| * | ||
|
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 suggestion as above |
||
| * @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. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.