diff --git a/src/components/forms/__test__/CheckBoxWithLabelForm.test.tsx b/src/components/forms/__test__/CheckBoxWithLabelForm.test.tsx new file mode 100644 index 0000000..d68e2b2 --- /dev/null +++ b/src/components/forms/__test__/CheckBoxWithLabelForm.test.tsx @@ -0,0 +1,52 @@ +import { CheckBoxWithLabelForm } from "../CheckBoxWithLabelForm"; +import { screen, render, fireEvent } from "@testing-library/react"; + +describe("", () => { + const formState = { + "test-question-text": [], + }; + const setFormState = jest.fn(); + + beforeEach(() => { + render( + , + ); + }); + + 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); + }); +}); diff --git a/src/components/forms/__test__/form-element.test.tsx b/src/components/forms/__test__/form-element.test.tsx index 3fe72cc..dd703a2 100644 --- a/src/components/forms/__test__/form-element.test.tsx +++ b/src/components/forms/__test__/form-element.test.tsx @@ -20,6 +20,10 @@ describe("FormElements", () => { ); }); + afterEach(() => { + jest.clearAllMocks(); + }); + test("should render Container properly", () => { expect(screen.getByTestId("checkbox-with-label-form")).toBeInTheDocument(); }); @@ -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); });