Skip to content

Commit

Permalink
test: FE tests for prime admin approval
Browse files Browse the repository at this point in the history
  • Loading branch information
BCerki committed Dec 11, 2024
1 parent 7be080e commit 2a162cd
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 78 deletions.
12 changes: 6 additions & 6 deletions bciers/apps/administration/tests/components/operations/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const getOperation = vi.fn();
const getOperationWithDocuments = vi.fn();
const getNaicsCodes = vi.fn();
const getReportingActivities = vi.fn();
const getBusinessStructures = vi.fn();
const getRegulatedProducts = vi.fn();
const getRegistrationPurposes = vi.fn();
const getBusinessStructures = vi.fn();

vi.mock("libs/actions/src/api/getOperation", () => ({
default: getOperation,
Expand All @@ -22,10 +22,6 @@ vi.mock("libs/actions/src/api/getReportingActivities", () => ({
default: getReportingActivities,
}));

vi.mock("libs/actions/src/api/getBusinessStructures", () => ({
default: getBusinessStructures,
}));

vi.mock("libs/actions/src/api/getRegulatedProducts", () => ({
default: getRegulatedProducts,
}));
Expand All @@ -34,12 +30,16 @@ vi.mock("libs/actions/src/api/getRegistrationPurposes", () => ({
default: getRegistrationPurposes,
}));

vi.mock("libs/actions/src/api/getBusinessStructures", () => ({
default: getBusinessStructures,
}));

export {
getOperation,
getOperationWithDocuments,
getNaicsCodes,
getReportingActivities,
getBusinessStructures,
getRegulatedProducts,
getRegistrationPurposes,
getBusinessStructures,
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { render, screen, act, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { actionHandler, useSession, useRouter } from "@bciers/testConfig/mocks";

import { operatorSchema } from "apps/administration/app/data/jsonSchema/operator";
import OperatorForm from "apps/administration/app/components/operators/OperatorForm";
import { createOperatorSchema } from "apps/administration/app/components/operators/OperatorPage";

import expectButton from "@bciers/testConfig/helpers/expectButton";
import expectField from "@bciers/testConfig/helpers/expectField";
import expectHeader from "@bciers/testConfig/helpers/expectHeader";
import { mockUseSession } from "@bciers/testConfig/helpers/mockUseSession";

import { FrontendMessages } from "@bciers/utils/src/enums";
import { createOperatorSchema } from "@/administration/app/data/jsonSchema/operator";
import { getBusinessStructures } from "../operations/mocks";

useSession.mockReturnValue({
get: vi.fn(),
Expand Down Expand Up @@ -80,16 +80,6 @@ const operatorFormData = {
mailing_address: 5,
};

const testSchema = createOperatorSchema(operatorSchema, [
{ name: "General Partnership" },
{ name: "BC Corporation" },
{ name: "Extra Provincially Registered Company" },
{ name: "Sole Proprietorship" },
{ name: "Limited Liability Partnership" },
{ name: "BC Incorporated Society" },
{ name: "Extraprovincial Non-Share Corporation" },
]);

const formHeaders: string[] = [
"Operator Information",
"Operator Address",
Expand Down Expand Up @@ -306,11 +296,16 @@ const fillPartnerParentFields = async () => {
describe("OperatorForm component", () => {
beforeEach(async () => {
vi.clearAllMocks();
getBusinessStructures.mockResolvedValue([
{ name: "General Partnership" },
{ name: "BC Corporation" },
{ name: "Limited Liability Partnership" },
]);
});
it("renders the empty operator form when creating a new operator", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={{}}
isCreating={true}
isInternalUser={false}
Expand All @@ -331,7 +326,7 @@ describe("OperatorForm component", () => {
it("does not allow new operator form submission if there are validation errors", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={{}}
isCreating={true}
isInternalUser={false}
Expand All @@ -353,7 +348,7 @@ describe("OperatorForm component", () => {

render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={{}}
isCreating={true}
isInternalUser={false}
Expand Down Expand Up @@ -398,7 +393,7 @@ describe("OperatorForm component", () => {
it("fills the partner and parent form fields, creates new operator, and redirects on success", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={{}}
isCreating={true}
isInternalUser={false}
Expand Down Expand Up @@ -431,7 +426,7 @@ describe("OperatorForm component", () => {
actionHandler.mockReturnValue({ error: null });
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={operatorFormData}
isInternalUser={false}
/>,
Expand Down Expand Up @@ -596,7 +591,7 @@ describe("OperatorForm component", () => {
it("loads existing readonly Operator form data for an internal user", async () => {
const { container } = render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={operatorFormData}
isInternalUser={true}
/>,
Expand Down Expand Up @@ -722,7 +717,7 @@ describe("OperatorForm component", () => {
it("calls the router.back function if is creating", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={operatorFormData}
isCreating={true}
isInternalUser={false}
Expand All @@ -734,7 +729,7 @@ describe("OperatorForm component", () => {
it("calls the router.back function if user is internal", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={operatorFormData}
isInternalUser={true}
/>,
Expand All @@ -745,7 +740,7 @@ describe("OperatorForm component", () => {
it("calls the router.push function if user is not internal and is not creating", async () => {
render(
<OperatorForm
schema={testSchema}
schema={await createOperatorSchema()}
formData={operatorFormData}
isCreating={false}
isInternalUser={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
useSession,
} from "@bciers/testConfig/mocks";
import { mockUseSession } from "@bciers/testConfig/helpers/mockUseSession";
import { getBusinessStructures, getCurrentOperator } from "./mocks";
import { getCurrentOperator } from "./mocks";
import OperatorPage from "apps/administration/app/components/operators/OperatorPage";
import { getBusinessStructures } from "../operations/mocks";

useRouter.mockReturnValue({
query: {},
Expand Down Expand Up @@ -101,7 +102,7 @@ describe("Operator component", () => {
bc_corporate_registry_number: "def1234567",
mailing_address: 5,
});
getBusinessStructures.mockReturnValueOnce([
getBusinessStructures.mockReturnValue([
{ name: "General Partnership" },
{ name: "BC Corporation" },
{ name: "Extra Provincially Registered Company" },
Expand All @@ -121,12 +122,15 @@ describe("Operator component", () => {
// Mock the session data
mockUseSession();
// Mock getBusinessStructures for create mode
getBusinessStructures.mockReturnValueOnce([
getBusinessStructures.mockReturnValue([
{ name: "General Partnership" },
{ name: "BC Corporation" },
{ name: "Extra Provincially Registered Company" },
{ name: "Sole Proprietorship" },
{ name: "Limited Liability Partnership" },
{ name: "BC Incorporated Society" },
{ name: "Extraprovincial Non-Share Corporation" },
]);

// Render the page with isCreating set to true
render(await OperatorPage({ isCreating: true }));

Expand Down
37 changes: 29 additions & 8 deletions bciers/apps/administration/tests/components/operators/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
const getCurrentOperator = vi.fn();
const getBusinessStructures = vi.fn();

const getOperator = vi.fn();
const getOperatorHasAdmin = vi.fn();
const getOperatorConfirmationInfo = vi.fn();
const getOperatorAccessDeclined = vi.fn();

vi.mock(
"apps/administration/app/components/operators/getCurrentOperator",
() => ({
default: getCurrentOperator,
}),
);

vi.mock("apps/administration/app/components/operators/getOperator", () => ({
default: getOperator,
}));
vi.mock(
"apps/administration/app/components/operators/getBusinessStructures",
"apps/administration/app/components/operators/getOperatorHasAdmin",
() => ({
default: getBusinessStructures,
default: getOperatorHasAdmin,
}),
);

vi.mock(
"apps/administration/app/components/operators/getOperatorConfirmationInfo",
() => ({
default: getOperatorConfirmationInfo,
}),
);
vi.mock(
"apps/administration/app/components/operators/getOperatorAccessDeclined",
() => ({
default: getOperatorAccessDeclined,
}),
);
vi.mock("@/administration/app/components/operators/getOperator", () => ({
default: getOperator,
}));

export { getCurrentOperator, getBusinessStructures, getOperator };
export {
getCurrentOperator,
getOperator,
getOperatorHasAdmin,
getOperatorConfirmationInfo,
getOperatorAccessDeclined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ const operatorJSON = {
legal_name: "Operator 1 Legal Name",
trade_name: "Operator 1 Trade Name",
cra_business_number: "123456789",
physical_street_address: "123 Main St",
bc_corporate_registry_number: "abc1234567",
business_structure: "Sole Proprietorship",
physical_municipality: "Victoria",
physical_province: "BC",
physical_postal_code: "V1V 1V1",
mailing_street_address: "test mailing street address",
mailing_municipality: "test mailing municipality",
mailing_province: "BC",
mailing_postal_code: "V0V0V0",
street_address: "123 Main St",
municipality: "Victoria",
province: "BC",
postal_code: "V1V 1V1",
website: "https://www.example2.com",
contacts: [101, 102, 103],
};
Expand Down Expand Up @@ -51,10 +47,8 @@ describe("Select Operator Confirm Form", () => {
screen.getByText(operatorJSON.cra_business_number),
).toBeInTheDocument();

expect(screen.getByText("Physical Address")).toBeInTheDocument();
expect(
screen.getByText(operatorJSON.physical_street_address),
).toBeInTheDocument();
expect(screen.getByText("Street Address")).toBeInTheDocument();
expect(screen.getByText(operatorJSON.street_address)).toBeInTheDocument();

expectButton("Yes this is my operator");
expect(screen.getByText(/This is not my operator./)).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { render, screen } from "@testing-library/react";
import { notFound } from "@bciers/testConfig/mocks";
import {
getOperator,
getOperatorAccessDeclined,
getOperatorConfirmationInfo,
getOperatorHasAdmin,
} from "../userOperators/mocks";
} from "../operators/mocks";

import SelectOperatorConfirmPage from "apps/administration/app/components/userOperators/SelectOperatorConfirmPage";

Expand All @@ -18,7 +18,7 @@ const mockOperatorState = (
hasAdmin: boolean,
accessDeclined: boolean,
): void => {
getOperator.mockReturnValueOnce(operatorJSON);
getOperatorConfirmationInfo.mockReturnValueOnce(operatorJSON);
getOperatorHasAdmin.mockReturnValueOnce(hasAdmin);
getOperatorAccessDeclined.mockReturnValueOnce(accessDeclined);
};
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("Select Operator Confirm Page", () => {
expectLink("Select another operator", "/select-operator");
});
it("renders error when getOperator fails", async () => {
getOperator.mockReturnValueOnce({
getOperatorConfirmationInfo.mockReturnValueOnce({
error: "operator error",
});
getOperatorHasAdmin.mockReturnValueOnce(false);
Expand All @@ -72,7 +72,7 @@ describe("Select Operator Confirm Page", () => {
}).rejects.toThrow("Failed to retrieve operator information.");
});
it("renders error when getOperatorHasAdmin fails", async () => {
getOperator.mockReturnValueOnce(operatorJSON);
getOperatorConfirmationInfo.mockReturnValueOnce(operatorJSON);
getOperatorAccessDeclined.mockReturnValueOnce(false);
getOperatorHasAdmin.mockReturnValueOnce({
error: "operator admin error",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { render } from "@testing-library/react";
import { notFound } from "@bciers/testConfig/mocks";
import { getOperator, getOperatorHasAdmin } from "../userOperators/mocks";

import SelectOperatorReceivedPage from "apps/administration/app/components/userOperators/SelectOperatorReceivedPage";

import { expectIcon } from "@bciers/testConfig/helpers/expectIcon";
import { expectMessage } from "@bciers/testConfig/helpers/expectMessage";
import { id, operatorJSON } from "./constants";
import {
getOperatorConfirmationInfo,
getOperatorHasAdmin,
} from "../operators/mocks";

// ⛏️ Helper function to mock the state of the operator based on admin presence and access decline status
const mockOperatorState = (hasAdmin: boolean): void => {
getOperator.mockReturnValueOnce(operatorJSON);
getOperatorConfirmationInfo.mockReturnValueOnce(operatorJSON);
getOperatorHasAdmin.mockReturnValueOnce(hasAdmin);
};

Expand Down Expand Up @@ -56,7 +59,7 @@ describe("Select Operator Received Page", () => {
);
});
it("renders error when getOperator fails", async () => {
getOperator.mockReturnValueOnce({
getOperatorConfirmationInfo.mockReturnValueOnce({
error: "operator error",
});
getOperatorHasAdmin.mockReturnValueOnce(false);
Expand All @@ -70,7 +73,7 @@ describe("Select Operator Received Page", () => {
}).rejects.toThrow("Failed to retrieve operator information.");
});
it("renders error when getOperatorHasAdmin fails", async () => {
getOperator.mockReturnValueOnce(operatorJSON);
getOperatorConfirmationInfo.mockReturnValueOnce(operatorJSON);
getOperatorHasAdmin.mockReturnValueOnce({
error: "operator admin error",
});
Expand Down
Loading

0 comments on commit 2a162cd

Please sign in to comment.