-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test code for <CheckBoxWithLabelForm/>
- Loading branch information
1 parent
b61a0fb
commit cc5f5d7
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/components/forms/__test__/CheckBoxWithLabelForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters