import React, { useState } from "react";
import CheckBox from "./checkbox";
import { fireEvent, render } from "@testing-library/react-native";
import { ThemeProvider } from "../../../theme/src/theme-context";
import CheckBoxGroup from "./checkbox-group";
import Stack from "../../atoms/stack/stack";
jest.useFakeTimers();
describe("Molecules/CheckBox", () => {
it("passes the snapshot test for basic setup", async () => {
const tree = await render(
Check me!
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test for different sizes", async () => {
const tree = await render(
Small Checkbox
Regular Checkbox
Large Checkbox
Extra Large Checkbox
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test for different variants", async () => {
const tree = await render(
Filled Checkbox
Outline Checkbox
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test when in a checked/indeterminate state", async () => {
const tree = await render(
Indeterminate Checkbox
Checked Checkbox
Indeterminate Checkbox
Checked Checkbox
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test when in an error state", async () => {
const tree = await render(
Error Checkbox
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test for different color schemes", async () => {
const tree = await render(
Primary CheckBox
Secondary CheckBox
).toJSON();
expect(tree).toMatchSnapshot();
});
it("passes the snapshot test when using style props", async () => {
const onPress = jest.fn();
const tree = await render(
Button press
).toJSON();
expect(tree).toMatchSnapshot();
});
it("captures the onPress event", async () => {
const onPress = jest.fn();
const component = await render(
Check me
);
const tree = component.toJSON();
if (
(tree as any).children[0].children[0].children[0].type !==
"RNGestureHandlerButton"
) {
const pressableInstance = component.getByTestId("checkBox");
fireEvent.press(pressableInstance);
expect(onPress).toHaveBeenCalledTimes(1);
}
});
it("changes the active value successfully when used in a group", async () => {
const onChange = jest.fn();
const Wrapper: React.FC = () => {
const [checkedGroup, setCheckedGroup] = useState([]);
const checkedHandler = (value: any) => {
setCheckedGroup(value);
onChange(value);
};
return (
Value 1
Value 2
Value 3
Value 4
);
};
const component = await render(
);
const tree = component.toJSON();
if (
(tree as any).children[0].children[0].children[0].children[0].children[0]
.children[0].type !== "RNGestureHandlerButton"
) {
const checkbox1 = component.getByTestId("checkbox1");
fireEvent.press(checkbox1);
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toBeCalledWith(["1"]);
const checkbox2 = component.getByTestId("checkbox2");
fireEvent.press(checkbox2);
expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange).toBeCalledWith(["1", "2"]);
// Remove the value
fireEvent.press(checkbox2);
expect(onChange).toHaveBeenCalledTimes(3);
expect(onChange).toBeCalledWith(["1"]);
const checkbox3 = component.getByTestId("checkbox3");
fireEvent.press(checkbox3);
expect(onChange).toHaveBeenCalledTimes(4);
expect(onChange).toBeCalledWith(["1", 3]);
const checkbox4 = component.getByTestId("checkbox4");
fireEvent.press(checkbox4);
expect(onChange).toHaveBeenCalledTimes(5);
expect(onChange).toBeCalledWith(["1", 3, 4]);
}
});
it("doesn't capture the onPress event when the checkbox is disabled ", async () => {
const onPress = jest.fn();
const component = await render(
Check me
);
const tree = component.toJSON();
if (
(tree as any).children[0].children[0].children[0].type !==
"RNGestureHandlerButton"
) {
const pressableInstance = component.getByTestId("checkBox");
fireEvent.press(pressableInstance);
expect(onPress).toHaveBeenCalledTimes(0);
}
});
});