import * as React from "react";
import {act, render} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {CheckboxGroup, Checkbox} from "../src";
describe("Checkbox.Group", () => {
it("should render correctly", () => {
const wrapper = render(
Sydney
Buenos Aires
,
);
expect(() => wrapper.unmount()).not.toThrow();
});
it("ref should be forwarded", () => {
const ref = React.createRef();
render(
Sydney
Buenos Aires
,
);
expect(ref.current).not.toBeNull();
});
it("should work correctly with initial value", () => {
const {container} = render(
Sydney
Buenos Aires
,
);
// check if the first checkbox is checked
expect(container.querySelector("[data-testid=first-checkbox] input")).toBeChecked();
// second checkbox should not be checked
expect(container.querySelector("[data-testid=second-checkbox] input")).not.toBeChecked();
});
it("should change value after click", () => {
let value = ["sydney"];
const wrapper = render(
act(() => (value = val as string[]))}
>
Sydney
Buenos Aires
,
);
const secondCheckbox = wrapper.getByTestId("second-checkbox");
act(() => {
secondCheckbox.click();
});
expect(value).toEqual(["sydney", "buenos-aires"]);
});
it("should ignore events when disabled", () => {
const {container} = render(
Sydney
Buenos Aires
,
);
const firstCheckbox = container.querySelector("[data-testid=first-checkbox] input");
const secondCheckbox = container.querySelector("[data-testid=second-checkbox] input");
expect(firstCheckbox).toBeChecked();
expect(secondCheckbox).not.toBeChecked();
secondCheckbox && userEvent.click(secondCheckbox);
expect(firstCheckbox).toBeChecked();
expect(secondCheckbox).not.toBeChecked();
});
it("should work correctly with controlled value", () => {
let checked = ["sydney"];
const onChange = jest.fn((value) => {
checked = value;
});
const wrapper = render(
{
act(() => {
onChange(checked);
});
}}
>
Sydney
Buenos Aires
,
);
const secondCheckbox = wrapper.getByTestId("second-checkbox");
act(() => {
secondCheckbox.click();
});
expect(onChange).toHaveBeenCalledTimes(1);
expect(checked).toEqual(["sydney", "buenos-aires"]);
});
describe("validation", () => {
let user = userEvent.setup();
beforeAll(() => {
user = userEvent.setup();
});
describe("validationBehavior=native", () => {
it("supports group level isRequired", async () => {
let {getAllByRole, getByRole, getByTestId} = render(
,
);
let group = getByRole("group");
expect(group).not.toHaveAttribute("aria-describedby");
let checkboxes = getAllByRole("checkbox") as HTMLInputElement[];
for (let input of checkboxes) {
expect(input).toHaveAttribute("required");
expect(input).not.toHaveAttribute("aria-required");
expect(input.validity.valid).toBe(false);
}
act(() => {
(getByTestId("form") as HTMLFormElement).checkValidity();
});
expect(group).toHaveAttribute("aria-describedby");
expect(document.getElementById(group.getAttribute("aria-describedby")!)).toHaveTextContent(
"Constraints not satisfied",
);
await user.click(checkboxes[0]);
for (let input of checkboxes) {
expect(input).not.toHaveAttribute("required");
expect(input).not.toHaveAttribute("aria-required");
expect(input.validity.valid).toBe(true);
}
expect(group).not.toHaveAttribute("aria-describedby");
});
it("supports checkbox level isRequired", async () => {
let {getAllByRole, getByRole, getByTestId} = render(
,
);
let group = getByRole("group");
expect(group).not.toHaveAttribute("aria-describedby");
let checkboxes = getAllByRole("checkbox") as HTMLInputElement[];
for (let input of checkboxes.slice(0, 2)) {
expect(input).toHaveAttribute("required");
expect(input).not.toHaveAttribute("aria-required");
expect(input.validity.valid).toBe(false);
}
expect(checkboxes[2]).not.toHaveAttribute("required");
expect(checkboxes[2]).not.toHaveAttribute("aria-required");
expect(checkboxes[2].validity.valid).toBe(true);
act(() => {
(getByTestId("form") as HTMLFormElement).checkValidity();
});
expect(group).toHaveAttribute("aria-describedby");
expect(document.getElementById(group.getAttribute("aria-describedby")!)).toHaveTextContent(
"Constraints not satisfied",
);
expect(document.activeElement).toBe(checkboxes[0]);
await user.click(checkboxes[0]);
await user.click(checkboxes[1]);
expect(checkboxes[0].validity.valid).toBe(true);
expect(checkboxes[1].validity.valid).toBe(true);
expect(group).not.toHaveAttribute("aria-describedby");
});
it("supports group level validate function", async () => {
let {getAllByRole, getByRole, getByTestId} = render(
,
);
let group = getByRole("group");
expect(group).not.toHaveAttribute("aria-describedby");
let checkboxes = getAllByRole("checkbox") as HTMLInputElement[];
for (let input of checkboxes) {
expect(input).not.toHaveAttribute("required");
expect(input).not.toHaveAttribute("aria-required");
expect(input.validity.valid).toBe(false);
}
act(() => {
(getByTestId("form") as HTMLFormElement).checkValidity();
});
expect(group).toHaveAttribute("aria-describedby");
expect(document.getElementById(group.getAttribute("aria-describedby")!)).toHaveTextContent(
"You must accept all terms",
);
expect(document.activeElement).toBe(checkboxes[0]);
await user.click(checkboxes[0]);
expect(group).toHaveAttribute("aria-describedby");
for (let input of checkboxes) {
expect(input.validity.valid).toBe(false);
}
await user.click(checkboxes[1]);
expect(group).toHaveAttribute("aria-describedby");
for (let input of checkboxes) {
expect(input.validity.valid).toBe(false);
}
await user.click(checkboxes[2]);
expect(group).not.toHaveAttribute("aria-describedby");
for (let input of checkboxes) {
expect(input.validity.valid).toBe(true);
}
});
});
describe("validationBehavior=aria", () => {
it("supports group level validate function", async () => {
let {getAllByRole, getByRole} = render(
(v.length < 3 ? "You must accept all terms" : null)}
validationBehavior="aria"
>
Terms and conditions
Cookies
Privacy policy
,
);
let group = getByRole("group");
expect(group).toHaveAttribute("aria-describedby");
expect(document.getElementById(group.getAttribute("aria-describedby")!)).toHaveTextContent(
"You must accept all terms",
);
let checkboxes = getAllByRole("checkbox") as HTMLInputElement[];
for (let input of checkboxes) {
expect(input).toHaveAttribute("aria-invalid", "true");
expect(input.validity.valid).toBe(true);
}
await user.click(checkboxes[0]);
expect(group).toHaveAttribute("aria-describedby");
await user.click(checkboxes[1]);
expect(group).toHaveAttribute("aria-describedby");
await user.click(checkboxes[2]);
expect(group).toHaveAttribute("aria-describedby");
});
});
});
});