Skip to content

Commit

Permalink
Adding tests for boolean control
Browse files Browse the repository at this point in the history
  • Loading branch information
TrangPham committed Mar 6, 2024
1 parent 1186933 commit b8454a5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/antd/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { test, expect } from "vitest";
import { screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { render } from "../common/test-render";

test("renders Checkbox component with default label", async () => {
render({
schema: {
type: "object",
properties: { name: { type: "boolean", title: "Name" } },
},
data: { name: true },
});

const checkbox = await screen.findByLabelText("Name");
expect(checkbox).toBeChecked();
});

test("handles onChange event correctly", async () => {
let formData = { name: false };

render({
schema: {
type: "object",
properties: { name: { type: "boolean", title: "Name" } },
},
data: formData,
onChange: (result) => {console.log(result); formData = result.data},
});

const checkbox = await screen.findByLabelText("Name");
expect(checkbox).not.toBeChecked();

await userEvent.click(checkbox);
expect(checkbox).toBeChecked();
console.log(formData);
expect(formData.name).toBe(true);

await userEvent.click(checkbox);
expect(checkbox).not.toBeChecked();
expect(formData.name).toBe(false);
});
2 changes: 1 addition & 1 deletion src/antd/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function Checkbox({ data, className, id, enabled, uischema, schema, path,
return (
<AntDCheckbox
checked={checked}
onChange={(e: CheckboxChangeEvent) => handleChange(path, e.target.checked)}
onChange={(e: CheckboxChangeEvent) => {handleChange(path, e.target.checked)}}
className={className}
id={id}
disabled={!enabled}
Expand Down
23 changes: 23 additions & 0 deletions src/controls/BooleanControl.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test, expect} from "vitest";
import { render } from "../common/test-render";
import {screen} from "@testing-library/react";

test("renders the Checkbox component", async () => {
render(
{
schema: {
type: "object",
properties: { adult: { type: "boolean", title: "Adult"} },
},
}
);

const checkbox = await screen.findByLabelText("Adult");
expect(checkbox).toBeInTheDocument();
expect(checkbox).not.toBeChecked();
expect(checkbox).toBeEnabled();
// check that there is an checkbox
expect(checkbox.tagName).toBe("INPUT");
expect(checkbox.getAttribute("type")).toBe("checkbox");

});

0 comments on commit b8454a5

Please sign in to comment.