Skip to content

Commit

Permalink
chore: add operation information form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellmueller committed Sep 23, 2024
1 parent a1f21d1 commit 35e2059
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const OperationInformationForm = ({
},
);

if (response.error) {
if (response?.error) {
setError(response.error);
return { error: response.error };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen } from "@testing-library/react";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { RJSFSchema } from "@rjsf/utils";
import OperationInformationForm from "apps/administration/app/components/operations/OperationInformationForm";
import { useSession } from "@bciers/testConfig/mocks";
import { actionHandler, useSession } from "@bciers/testConfig/mocks";

useSession.mockReturnValue({
data: {
Expand Down Expand Up @@ -76,23 +76,68 @@ describe("the OperationInformationForm component", () => {
expect(screen.getByText(/Operation Type/i)).toBeVisible();
});

// it("should have unchecked task list sections when no formData is provided", async () => {
// render(<OperationInformationForm formData={{}} schema={testSchema} />);

// expect(screen.getByTestId("section1-tasklist-check")).not.toContainHTML(
// "svg",
// );
// expect(screen.getByTestId("section2-tasklist-check")).not.toContainHTML(
// "svg",
// );
//});

// it("should have checked task list sections when formData is provided", async () => {
// render(
// <OperationInformationForm formData={formData} schema={testSchema} />,
// );

// expect(screen.getByTestId("section1-tasklist-check")).toContainHTML("svg");
// expect(screen.getByTestId("section2-tasklist-check")).toContainHTML("svg");
// });
it("should enable editing when the Edit button is clicked", async () => {
render(
<OperationInformationForm
formData={formData}
schema={testSchema}
operationId={operationId}
/>,
);

expect(screen.getByRole("button", { name: "Edit" })).toBeVisible();

await act(async () => {
// Click the Edit button
screen.getByRole("button", { name: "Edit" }).click();
});

// Expect the Edit button to be disabled
expect(screen.queryByRole("button", { name: "Edit" })).toBeNull();
});

it("should edit and submit the form", async () => {
render(
<OperationInformationForm
formData={formData}
schema={testSchema}
operationId={operationId}
/>,
);

await act(async () => {
// Click the Edit button
screen.getByRole("button", { name: "Edit" }).click();
});

// Fill out the form
const nameInput = screen.getByLabelText(/Operation Name/i);

expect(nameInput).toHaveValue("Operation 3");

await act(async () => {
fireEvent.change(nameInput, { target: { value: "Operation 4" } });
});

// Click the Submit button
await act(async () => {
screen.getByRole("button", { name: "Submit" }).click();
});

expect(actionHandler).toHaveBeenCalledTimes(1);
expect(actionHandler).toHaveBeenCalledWith(
`registration/v2/operations/${operationId}`,
"PUT",
"",
{
body: JSON.stringify({
name: "Operation 4",
type: "Single Facility Operation",
}),
},
);

// Expect the form to be submitted
expect(screen.getByText(/Operation 4/i)).toBeVisible();
});
});

0 comments on commit 35e2059

Please sign in to comment.