Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for boolean control #19

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/antd/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test, expect} from "vitest";
import { screen } from "@testing-library/react";
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();
});
30 changes: 21 additions & 9 deletions src/antd/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import { CellProps, WithClassname, Helpers } from "@jsonforms/core"
import { Checkbox as AntDCheckbox } from "antd"
import { CheckboxChangeEvent } from "antd/es/checkbox"
import { CellProps, WithClassname, Helpers } from "@jsonforms/core";
import { Checkbox as AntDCheckbox } from "antd";
import { CheckboxChangeEvent } from "antd/es/checkbox";

interface CheckboxProps extends CellProps, WithClassname {
label?: string
label?: string;
}

export function Checkbox({ data, className, id, enabled, uischema, schema, path, handleChange, label } : CheckboxProps) {
const checked = !!data // convert undefined to false
export function Checkbox({
data,
className,
id,
enabled,
uischema,
schema,
path,
handleChange,
label,
}: CheckboxProps) {
const checked = !!data; // convert undefined to false

const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema);
const defaultLabel = labelDescription.show ? labelDescription.text : '';
const defaultLabel = labelDescription.show ? labelDescription.text : "";

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}
>
{label ?? defaultLabel}
</AntDCheckbox>
)
);
}
57 changes: 57 additions & 0 deletions src/controls/BooleanControl.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, expect, vi } from "vitest";
import { screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { render } from "../common/test-render";

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");
});

test("handles onChange event correctly", async () => {
const updateData = vi.fn();
render({
schema: {
type: "object",
properties: { name: { type: "boolean", title: "Name" } },
},
data: { name: false },
onChange: (result) => {
console.log(result);
updateData(result);
},
});

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

await userEvent.click(checkbox);
expect(checkbox).toBeChecked();
// FYI the calls to updateData lag behind the actual checkbox state. Not sure why.
// It could be the difference between json-forms handleChange(path, value) and the onChange event.
expect(updateData).toHaveBeenLastCalledWith({
data: { name: false },
errors: [],
});

await userEvent.click(checkbox);
expect(checkbox).not.toBeChecked();
expect(updateData).toHaveBeenLastCalledWith({
data: { name: true },
errors: [],
});

expect(updateData).toBeCalledTimes(2);
});
Loading