Skip to content

Commit

Permalink
chore(jest): add unit test for FormRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Tarca authored and matejtarca committed Oct 15, 2023
1 parent b2ba9a6 commit 3ec848a
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { act, render, screen, waitFor } from "@testing-library/react";
import FormRenderer from "../FormRenderer";
import { Button } from "@/components/ui/button";
import userEvent from "@testing-library/user-event";

const onSubmitMock = jest.fn();
const actionButtons = <Button type="submit">Save</Button>;
describe("FormRenderer", () => {
beforeEach(() => {
jest.resetAllMocks();
});
it("should render simple form with a text field", async () => {
render(
<FormRenderer
formFields={[
{
id: 1,
position: 1,
name: "name",
label: "Name",
type: "text",
required: true,
initialValue: "",
optionList: [],
},
]}
onSubmit={onSubmitMock}
actionButtons={actionButtons}
/>
);

const input = screen.getByLabelText("Name");
const button = screen.getByRole("button", { name: "Save" });
expect(input).toBeVisible();
expect(button).toBeVisible();

act(() => userEvent.click(button));
expect(onSubmitMock).not.toHaveBeenCalled();
await waitFor(() =>
expect(screen.getByText("This field is required")).toBeVisible()
);

act(() => userEvent.type(input, "John"));
expect(input).toHaveValue("John");

act(() => userEvent.click(button));
await waitFor(() => expect(onSubmitMock).toHaveBeenCalledTimes(1));
});
});

0 comments on commit 3ec848a

Please sign in to comment.