Skip to content

Commit

Permalink
test: add regression tests
Browse files Browse the repository at this point in the history
Signed-off-by: SeSo <[email protected]>
  • Loading branch information
Sepehr-Sobhani committed Jan 3, 2025
1 parent c71cc1f commit 1a1f585
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export const checkEmptyContactForm = () => {
expect(screen.getByLabelText(/Select the user/i)).toHaveValue("");
expect(screen.getByLabelText(/First Name/i)).toHaveValue("");
expect(screen.getByLabelText(/Last Name/i)).toHaveValue("");
expect(screen.getByText(/Places Assigned/i)).toBeVisible();

expect(
screen.getByRole("heading", { name: /Work Information/i }),
).toBeVisible();
Expand Down Expand Up @@ -108,7 +106,7 @@ describe("ContactForm component", () => {
it("renders the empty contact form when creating a new contact", async () => {
render(
<ContactForm
schema={contactsSchema}
schema={createContactSchema(contactsSchema, [], true)}
uiSchema={contactsUiSchema}
formData={{}}
isCreating
Expand Down Expand Up @@ -194,7 +192,7 @@ describe("ContactForm component", () => {
it("does not allow new contact form submission if there are validation errors (empty form data)", async () => {
render(
<ContactForm
schema={contactsSchema}
schema={createContactSchema(contactsSchema, [], true)}
uiSchema={contactsUiSchema}
formData={{}}
isCreating
Expand All @@ -214,7 +212,7 @@ describe("ContactForm component", () => {
async () => {
render(
<ContactForm
schema={contactsSchema}
schema={createContactSchema(contactsSchema, [], true)}
uiSchema={contactsUiSchema}
formData={{}}
isCreating
Expand Down Expand Up @@ -244,7 +242,6 @@ describe("ContactForm component", () => {
existing_bciers_user: false,
first_name: "John",
last_name: "Doe",
places_assigned: ["None"],
position_title: "Senior Officer",
email: "[email protected]",
phone_number: "+1 1 604 401 1234",
Expand Down Expand Up @@ -275,7 +272,7 @@ describe("ContactForm component", () => {
async () => {
render(
<ContactForm
schema={contactsSchema}
schema={createContactSchema(contactsSchema, [], true)}
uiSchema={contactsUiSchema}
formData={{}}
isCreating
Expand Down Expand Up @@ -327,7 +324,6 @@ describe("ContactForm component", () => {
existing_bciers_user: false,
first_name: "John",
last_name: "Doe",
places_assigned: ["None"],
position_title: "Senior Officer",
email: "[email protected]",
phone_number: "+1 1 604 401 1234",
Expand Down Expand Up @@ -368,7 +364,6 @@ describe("ContactForm component", () => {
existing_bciers_user: false,
first_name: "John updated",
last_name: "Doe updated",
places_assigned: ["None"],
position_title: "Senior Officer",
email: "[email protected]",
phone_number: "+1 1 604 401 1234",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const createRegistrationPurposeSchema = async () => {
operation: {
type: "string",
title: "Select your operation:",
anyOf: operationsAnyOf,
...(operationsAnyOf && { anyOf: operationsAnyOf }),
},
operation_add: {
//Not an actual field in the db - this is just to make the form look like the wireframes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { act, render, screen, waitFor } from "@testing-library/react";
import { describe, expect, vi } from "vitest";
import React from "react";
import { useSession, useRouter } from "@bciers/testConfig/mocks";
import OperationInformationForm from "apps/registration/app/components/operations/registration/OperationInformationForm";
import {
Expand Down Expand Up @@ -394,14 +393,76 @@ describe("the OperationInformationForm component", () => {
steps={allOperationRegistrationSteps}
/>,
);
userEvent.click(screen.getByRole("button", { name: /save and continue/i }));
await waitFor(() => {
expect(screen.getAllByText(/Required field/i)).toHaveLength(6);
});
await waitFor(() => {
expect(
screen.getByText(/You must select or add an operation/i),
).toBeVisible();
});
await userEvent.click(
screen.getByRole("button", { name: /save and continue/i }),
);
expect(screen.getAllByText(/Required field/i)).toHaveLength(6);
expect(
screen.getByText(/You must select or add an operation/i),
).toBeVisible();
});
it("should not raise an error when we pass an empty array to anyOf (operation has empty array in response)", async () => {
// listen for console warnings
const consoleErrorMock = vi
.spyOn(console, "warn")
.mockImplementation(() => {});

// repeat the fetchFormEnums, except this time we will mock the operation response to have an empty array
// Regulated products
actionHandler.mockResolvedValueOnce([
{ id: 1, name: "BC-specific refinery complexity throughput" },
{ id: 2, name: "Cement equivalent" },
]);
// Operations
actionHandler.mockResolvedValueOnce([]);
// Purposes
actionHandler.mockResolvedValueOnce([
"Reporting Operation",
"Potential Reporting Operation",
"Electricity Import Operation",
]);
// Naics codes
actionHandler.mockResolvedValueOnce([
{
id: 1,
naics_code: "211110",
naics_description: "Oil and gas extraction (except oil sands)",
},
{
id: 2,
naics_code: "212114",
naics_description: "Bituminous coal mining",
},
]);
// Reporting activities
actionHandler.mockResolvedValueOnce([
{ id: 1, name: "Amonia production" },
{ id: 2, name: "Cement production" },
]);
// Business structures
actionHandler.mockResolvedValueOnce([
{ name: "General Partnership" },
{ name: "BC Corporation" },
]);

render(
<OperationInformationForm
rawFormData={{}}
schema={await createRegistrationOperationInformationSchema()}
step={1}
steps={allOperationRegistrationSteps}
/>,
);

// make sure the error was not logged(before fixing the issue we would see this error)
expect(consoleErrorMock).not.toHaveBeenCalledWith(
"Error encountered compiling schema:",
expect.objectContaining({
message: expect.stringContaining(
"schema is invalid: data/properties/section1/properties/operation/anyOf must NOT have fewer than 1 items",
),
}),
);
consoleErrorMock.mockRestore();
});
});

0 comments on commit 1a1f585

Please sign in to comment.