-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ui): add test for checkbox input component (#1370)
* test: add test for checlbox input component * fix: radio input warning * refactor: update radio input test
- Loading branch information
1 parent
a88bc3e
commit 8cfdddc
Showing
3 changed files
with
144 additions
and
22 deletions.
There are no files selected for viewing
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
119 changes: 119 additions & 0 deletions
119
packages/ui/src/FormWidgets/CheckboxInput/__test__/CheckboxInput.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,119 @@ | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { CheckboxInput } from "../index"; | ||
|
||
describe("CheckboxInput", () => { | ||
const options = [ | ||
{ value: "option1", label: "Option 1" }, | ||
{ value: "option2", label: "Option 2" }, | ||
]; | ||
|
||
const onChangeMock = vi.fn(); | ||
|
||
const renderCheckboxInput = (properties = {}) => { | ||
return render( | ||
<CheckboxInput | ||
name="test-checkbox" | ||
options={options} | ||
onChange={onChangeMock} | ||
{...properties} | ||
/>, | ||
); | ||
}; | ||
|
||
afterEach(() => { | ||
onChangeMock.mockClear(); | ||
}); | ||
|
||
it("renders a single checkbox when no options are provided", () => { | ||
renderCheckboxInput({ options: [] }); | ||
|
||
const checkbox = screen.getByRole("checkbox"); | ||
|
||
expect(checkbox).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders multiple checkboxes when options are provided", () => { | ||
renderCheckboxInput(); | ||
|
||
const checkboxes = screen.getAllByRole("checkbox"); | ||
|
||
expect(checkboxes).toHaveLength(options.length); | ||
}); | ||
|
||
it("toggles the single checkbox and calls onChange with the correct value", () => { | ||
renderCheckboxInput({ options: [] }); | ||
|
||
const checkbox = screen.getByRole("checkbox"); | ||
|
||
fireEvent.click(checkbox); | ||
|
||
expect(onChangeMock).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it("toggles multiple checkboxes and calls onChange with the selected values", () => { | ||
renderCheckboxInput(); | ||
|
||
const [checkbox1, checkbox2] = screen.getAllByRole("checkbox"); | ||
|
||
// Select the first option | ||
fireEvent.click(checkbox1); | ||
expect(onChangeMock).toHaveBeenCalledWith(["option1"]); | ||
|
||
// Select the second option | ||
fireEvent.click(checkbox2); | ||
expect(onChangeMock).toHaveBeenCalledWith(["option1", "option2"]); | ||
|
||
// Deselect the first option | ||
fireEvent.click(checkbox1); | ||
expect(onChangeMock).toHaveBeenCalledWith(["option2"]); | ||
}); | ||
|
||
it("disables checkboxes when the disabled prop is true", () => { | ||
renderCheckboxInput({ disabled: true }); | ||
|
||
const checkboxes = screen.getAllByRole("checkbox"); | ||
|
||
checkboxes.forEach((checkbox) => { | ||
expect(checkbox).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
it("renders helper text and error message when provided", () => { | ||
renderCheckboxInput({ | ||
helperText: "Helper text", | ||
errorMessage: "Error message", | ||
}); | ||
|
||
expect(screen.getByText("Helper text")).toBeInTheDocument(); | ||
expect(screen.getByText("Error message")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders custom labels for options when renderOptionsLabel is provided", () => { | ||
const renderOptionsLabel = (option: { value: string; label: string }) => ( | ||
<span>Custom {option.label}</span> | ||
); | ||
|
||
renderCheckboxInput({ renderOptionsLabel }); | ||
|
||
expect(screen.getByText("Custom Option 1")).toBeInTheDocument(); | ||
expect(screen.getByText("Custom Option 2")).toBeInTheDocument(); | ||
}); | ||
|
||
it("initializes with default selected values", () => { | ||
renderCheckboxInput({ value: ["option1"] }); | ||
|
||
const [checkbox1, checkbox2] = screen.getAllByRole("checkbox"); | ||
expect(checkbox1).toBeChecked(); | ||
expect(checkbox2).not.toBeChecked(); | ||
}); | ||
|
||
it("initializes with default checked state for single checkbox", () => { | ||
renderCheckboxInput({ options: [], checked: true }); | ||
|
||
const checkbox = screen.getByRole("checkbox"); | ||
expect(checkbox).toBeChecked(); | ||
}); | ||
}); |
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