Skip to content

Commit

Permalink
test: add test code for <CheckBoxWithLabelForm/>
Browse files Browse the repository at this point in the history
  • Loading branch information
toothlessdev committed Nov 21, 2024
1 parent b61a0fb commit cc5f5d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/components/forms/__test__/CheckBoxWithLabelForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { CheckBoxWithLabelForm } from "../CheckBoxWithLabelForm";
import { screen, render, fireEvent } from "@testing-library/react";

describe("<CheckBoxWithLabelForm/>", () => {
const formState = {
"test-question-text": [],
};
const setFormState = jest.fn();

beforeEach(() => {
render(
<CheckBoxWithLabelForm
questionText={"test-question-text"}
options={["options1", "options2", "options3"]}
formState={formState}
setFormState={setFormState}
/>,
);
});

afterEach(() => {
jest.clearAllMocks();
});

test("should render Container properly", () => {
expect(screen.getByTestId("checkbox-with-label-form")).toBeInTheDocument();
});

test("should render each options properly", () => {
expect(screen.getByText("options1")).toBeInTheDocument();
expect(screen.getByText("options2")).toBeInTheDocument();
expect(screen.getByText("options3")).toBeInTheDocument();
});

test("should call setFormState once when checkbox is clicked", async () => {
const checkbox = await screen.findByTestId("checkbox_options1");
expect(checkbox.getAttribute("data-state")).toBe("unchecked");

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(1);
});

test("should call setFormState twice when checkbox is clicked twice", async () => {
const checkbox = await screen.findByTestId("checkbox_options1");

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(1);

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(2);
});
});
14 changes: 14 additions & 0 deletions src/components/forms/__test__/form-element.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe("FormElements", () => {
);
});

afterEach(() => {
jest.clearAllMocks();
});

test("should render Container properly", () => {
expect(screen.getByTestId("checkbox-with-label-form")).toBeInTheDocument();
});
Expand All @@ -34,6 +38,16 @@ describe("FormElements", () => {
const checkbox = await screen.findByTestId("checkbox_options1");
expect(checkbox.getAttribute("data-state")).toBe("unchecked");

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(1);
});

test("should call setFormState twice when checkbox is clicked twice", async () => {
const checkbox = await screen.findByTestId("checkbox_options1");

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(1);

fireEvent.click(checkbox);
expect(setFormState).toHaveBeenCalledTimes(2);
});
Expand Down

0 comments on commit cc5f5d7

Please sign in to comment.