Skip to content

Commit

Permalink
Merge pull request #2643 from bcgov/2624-fix-new-contact-new-operation
Browse files Browse the repository at this point in the history
fix: populate anyOf only if we have options
  • Loading branch information
Sepehr-Sobhani authored Jan 3, 2025
2 parents 91f80e9 + 1a1f585 commit ad97a58
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ export const createContactSchema = (
// For now, we show the `existing_bciers_user` toggle and `selected_user` combobox only when creating a new contact. We should not show `places_assigned` when creating
if (isCreating) {
delete localSchema.properties.section1.properties.places_assigned;
const userOperatorUserOptions = userOperatorUsers?.map((user) => ({
const userOperatorUserOptions = userOperatorUsers.map((user) => ({
type: "string",
title: user.full_name,
enum: [user.id],
value: user.id,
}));

if (Array.isArray(userOperatorUserOptions)) {
if (
Array.isArray(userOperatorUserOptions) &&
userOperatorUserOptions.length > 0
) {
localSchema.properties.section1.allOf[0].then.properties.selected_user.anyOf =
userOperatorUserOptions;
}
Expand Down
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: "john.doe@example.com",
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: "john.doe@example.com",
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: "john.doe@example.com",
phone_number: "+1 1 604 401 1234",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export const createRegistrationPurposeSchema = async () => {
const regulatedProducts: { id: number; name: string }[] =
await getRegulatedProducts();
const operations = await getCurrentUsersOperations();
// Using empty array for anyOf will cause the field to not show up and raise an error
let operationsAnyOf;
if (Array.isArray(operations) && operations.length > 0) {
operationsAnyOf = operations.map(
(operation: { id: UUID; name: string }) => ({
const: operation.id,
title: operation.name,
}),
);
}

const registrationPurposes = await getRegistrationPurposes();

// create the schema with the fetched values
Expand Down Expand Up @@ -51,10 +62,7 @@ export const createRegistrationPurposeSchema = async () => {
operation: {
type: "string",
title: "Select your operation:",
anyOf: operations.map((operation: { id: UUID; name: string }) => ({
const: operation.id,
title: operation.name,
})),
...(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 ad97a58

Please sign in to comment.