import * as React from "react";
import { render, click } from "reakit-test-utils";
import { Checkbox, useCheckbox, useCheckboxState } from "..";
test("single checkbox", () => {
const Test = () => {
const checkbox = useCheckboxState();
return (
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
click(checkbox);
expect(checkbox.checked).toBe(true);
});
test("group checkbox", () => {
const Test = () => {
const checkbox = useCheckboxState();
return (
);
};
const { getByLabelText } = render();
const apple = getByLabelText("apple") as HTMLInputElement;
const orange = getByLabelText("orange") as HTMLInputElement;
const watermelon = getByLabelText("watermelon") as HTMLInputElement;
expect(apple.checked).toBe(false);
expect(orange.checked).toBe(false);
expect(watermelon.checked).toBe(false);
click(orange);
click(apple);
expect(apple.checked).toBe(true);
expect(orange.checked).toBe(true);
expect(watermelon.checked).toBe(false);
});
test("group checkbox with initial state", () => {
const Test = () => {
const checkbox = useCheckboxState({ state: ["orange"] });
return (
);
};
const { getByLabelText } = render();
const apple = getByLabelText("apple") as HTMLInputElement;
const orange = getByLabelText("orange") as HTMLInputElement;
const watermelon = getByLabelText("watermelon") as HTMLInputElement;
expect(apple.checked).toBe(false);
expect(orange.checked).toBe(true);
expect(watermelon.checked).toBe(false);
click(apple);
expect(apple.checked).toBe(true);
expect(orange.checked).toBe(true);
expect(watermelon.checked).toBe(false);
});
test("checkbox onChange checked value", () => {
const onChange = jest.fn();
const Test = () => {
const checkbox = useCheckboxState();
return (
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(onChange).not.toBeCalled();
click(checkbox);
expect(checkbox.checked).toBe(true);
expect(onChange).toBeCalledWith(true);
click(checkbox);
expect(checkbox.checked).toBe(false);
expect(onChange).toBeCalledWith(false);
});
test("non-native checkbox onChange checked value", () => {
const onChange = jest.fn();
const Test = () => {
const checkbox = useCheckboxState();
return (
onChange(event.target.checked)}
/>
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(onChange).not.toBeCalled();
click(checkbox);
expect(checkbox.checked).toBe(true);
expect(onChange).toBeCalledWith(true);
click(checkbox);
expect(checkbox.checked).toBe(false);
expect(onChange).toBeCalledWith(false);
});
test("checkbox onChange checked value without useCheckboxState", () => {
const onChange = jest.fn();
const Test = () => {
const [checked, setChecked] = React.useState(false);
return (
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(onChange).not.toBeCalled();
click(checkbox);
expect(checkbox.checked).toBe(true);
expect(onChange).toBeCalledWith(true);
click(checkbox);
expect(checkbox.checked).toBe(false);
expect(onChange).toBeCalledWith(false);
});
test("non-native checkbox onChange checked value without useCheckboxState", () => {
const onChange = jest.fn();
const Test = () => {
const [checked, setChecked] = React.useState(false);
return (
{
setChecked(event.target.checked);
onChange(event.target.checked);
}}
/>
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(onChange).not.toBeCalled();
click(checkbox);
expect(checkbox.checked).toBe(true);
expect(onChange).toBeCalledWith(true);
click(checkbox);
expect(checkbox.checked).toBe(false);
expect(onChange).toBeCalledWith(false);
});
test("non-native checkbox onClick preventDefault", () => {
const Test = () => {
const checkbox = useCheckboxState();
return (
event.preventDefault()}
/>
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
click(checkbox);
expect(checkbox.checked).toBe(false);
});
test("useCheckbox", () => {
const Test = () => {
const [checked, setChecked] = React.useState(false);
const props = useCheckbox(
{},
{
checked,
onChange: (event: React.ChangeEvent) =>
setChecked(event.target.checked),
}
);
return (
);
};
const { getByLabelText } = render();
const checkbox = getByLabelText("checkbox") as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(checkbox).toMatchInlineSnapshot(`
`);
click(checkbox);
expect(checkbox.checked).toBe(true);
expect(checkbox).toMatchInlineSnapshot(`
`);
click(checkbox);
expect(checkbox.checked).toBe(false);
expect(checkbox).toMatchInlineSnapshot(`
`);
});