Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/dom/src/lib/ElementAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import equal from "fast-deep-equal";

import {
describeAccessibleExpectation,
isValidAriaCheckedStrict,
isValidAriaPressed,
matchesAccessibleExpectation,
} from "./helpers/accessibility";
import {
isButtonElement,
isCheckableInput,
isCheckboxInput,
isElementEmpty,
isElementOrAncestorDisabled,
normalizeHtml,
Expand Down Expand Up @@ -432,6 +435,88 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
});
}

/**
* Asserts that the element is checked.
*
* Valid for `<input type="checkbox">`, `<input type="radio">`, or elements
* with a valid `aria-checked` attribute ("true" or "false") and
* a checkable role (`checkbox`, `radio`, or `switch`).
*
* @example
* // Native checkbox
* expect(element).toBeChecked();
* expect(element).not.toBeChecked();
*
* // ARIA checkbox
* expect(element).toBeChecked(); // when aria-checked="true"
*
* @returns the assertion instance.
*/
public toBeChecked(): this {
const isNativeCheckable = isCheckableInput(this.actual);
const isAriaCheckable = isValidAriaCheckedStrict(this.actual);
if (!isNativeCheckable && !isAriaCheckable) {
throw new Error(
"Only checkbox/radio inputs or valid aria-checked elements can be used with .toBeChecked()",
);
}
const isChecked = isNativeCheckable
? (this.actual as unknown as HTMLInputElement).checked
: this.actual.getAttribute("aria-checked") === "true";
const error = new AssertionError({
actual: this.actual,
message: "Expected the element to be checked",
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the element to NOT be checked",
});
return this.execute({
assertWhen: isChecked,
error,
invertedError,
});
}

/**
* Asserts that the element is partially checked (indeterminate).
*
* Valid for `<input type="checkbox">` or elements with `role="checkbox"`
* and `aria-checked="mixed"`.
*
* @example
* expect(element).toBePartiallyChecked();
* expect(element).not.toBePartiallyChecked();
*
* @returns the assertion instance.
*/
public toBePartiallyChecked(): this {
const isNativeCheckbox = isCheckboxInput(this.actual);
const isAriaCheckbox = this.actual.getAttribute("role") === "checkbox";
if (!isNativeCheckbox && !isAriaCheckbox) {
throw new Error(
"Only checkbox inputs or checkbox-role elements can be used with .toBePartiallyChecked()",
);
}
const isPartiallyChecked = isNativeCheckbox
? (this.actual as unknown as HTMLInputElement).indeterminate
|| this.actual.getAttribute("aria-checked") === "mixed"
: this.actual.getAttribute("aria-checked") === "mixed";
const error = new AssertionError({
actual: this.actual,
message: "Expected the element to be partially checked",
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the element to NOT be partially checked",
});
return this.execute({
assertWhen: isPartiallyChecked,
error,
invertedError,
});
}

/**
* Asserts that the element contains the specified HTML.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/dom/src/lib/helpers/accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ export function isValidAriaPressed(element: Element): boolean {
return pressedAttribute !== null && ["true", "false", "mixed"].includes(pressedAttribute);
}

export function isValidAriaCheckedStrict(element: Element): boolean {
const checkedAttribute = element.getAttribute("aria-checked");
const role = element.getAttribute("role") ?? "";
return ["checkbox", "radio", "switch"].includes(role)
&& checkedAttribute !== null
&& ["true", "false"].includes(checkedAttribute);
}

export function matchesAccessibleExpectation(actual: string, expected?: RegExp | string): boolean {
if (expected === undefined) {
return Boolean(actual);
Expand Down
12 changes: 12 additions & 0 deletions packages/dom/src/lib/helpers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export function isButtonElement(element: Element): boolean {
return isNativeButton || hasButtonRole;
}

export function isCheckableInput(element: Element): boolean {
const tagName = element.tagName.toLowerCase();
const type = element.getAttribute("type");
return tagName === "input" && (type === "checkbox" || type === "radio");
}

export function isCheckboxInput(element: Element): boolean {
const tagName = element.tagName.toLowerCase();
const type = element.getAttribute("type");
return tagName === "input" && type === "checkbox";
}

const FORM_TAGS = [
"fieldset",
"input",
Expand Down
Loading
Loading