Skip to content

Commit

Permalink
issue #231: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k-allagbe committed Feb 7, 2025
1 parent a8d4e09 commit 5cfffbc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import FileUploaded from "@/classe/File";
import { QuantityChips } from "@/components/QuantityChip";
import useUploadedFilesStore from "@/stores/fileStore";
import useLabelDataStore from "@/stores/labelDataStore";
import { Quantity } from "@/types/types";
import {
VERIFIED_LABEL_DATA,
VERIFIED_LABEL_DATA_WITH_ID,
} from "@/utils/client/constants";
import { fireEvent, render, screen } from "@testing-library/react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import axios from "axios";
import LabelDataConfirmationPage from "../page";
import { QuantityChips } from "@/components/QuantityChip";
import { Quantity } from "@/types/types";

const mockedRouterPush = jest.fn();
jest.mock("next/navigation", () => ({
Expand Down Expand Up @@ -235,7 +235,9 @@ describe("Notes Section Tests", () => {

it("should update the comment value when text is entered", () => {
useLabelDataStore.getState().setLabelData(VERIFIED_LABEL_DATA);
expect(useLabelDataStore.getState().labelData?.comment).toBe("Compliant with regulations.");
expect(useLabelDataStore.getState().labelData?.comment).toBe(
"Compliant with regulations.",
);
render(<LabelDataConfirmationPage />);
const notesTextbox = screen
.getByTestId("notes-textbox")
Expand Down Expand Up @@ -263,3 +265,42 @@ describe("Notes Section Tests", () => {
expect(notesTextbox).not.toBeDisabled();
});
});

describe("LabelDataConfirmationPage - Ingredients Section", () => {
it("should render the nutrients table when recordKeeping is false or undefined", async () => {
useLabelDataStore.getState().setLabelData({
...VERIFIED_LABEL_DATA,
ingredients: {
...VERIFIED_LABEL_DATA.ingredients,
recordKeeping: { value: false, verified: true },
},
});
expect(
useLabelDataStore.getState().labelData?.ingredients.recordKeeping,
).toEqual({ value: false, verified: true });

render(<LabelDataConfirmationPage />);

expect(screen.getByTestId("ingredients-section")).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText("ingredients.nutrients")).toBeInTheDocument();
expect(screen.getByText("Ammonium phosphate")).toBeInTheDocument();
});

useLabelDataStore.getState().setLabelData({
...VERIFIED_LABEL_DATA,
ingredients: {
...VERIFIED_LABEL_DATA.ingredients,
recordKeeping: { value: true, verified: true },
},
});

expect(screen.getByTestId("ingredients-section")).toBeInTheDocument();
await waitFor(() => {
expect(
screen.queryByText("ingredients.nutrients"),
).not.toBeInTheDocument();
expect(screen.queryByText("Ammonium phosphate")).not.toBeInTheDocument();
});
});
});
6 changes: 0 additions & 6 deletions src/app/label-data-confirmation/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ import { useTranslation } from "react-i18next";
const LabelDataConfirmationPage = () => {
const labelData = useLabelDataStore((state) => state.labelData);
const setLabelData = useLabelDataStore((state) => state.setLabelData);
const resetLabelData = useLabelDataStore((state) => state.resetLabelData);
const setComment = useLabelDataStore((state) => state.setComment);
const uploadedFiles = useUploadedFilesStore((state) => state.uploadedFiles);
const clearUploadedFiles = useUploadedFilesStore(
(state) => state.clearUploadedFiles,
);
const imageFiles = uploadedFiles.map((file) => file.getFile());
const router = useRouter();
const [confirmLoading, setConfirmLoading] = useState(false);
Expand Down Expand Up @@ -70,8 +66,6 @@ const LabelDataConfirmationPage = () => {
)
.then(() => {
showAlert("Label data saved successfully.", "success");
resetLabelData();
clearUploadedFiles();
router.push("/");
})
.catch((error) => {
Expand Down
23 changes: 22 additions & 1 deletion src/components/__tests__/IngredientsForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_LABEL_DATA, LabelData } from "@/types/types";
import { render, screen } from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import { useEffect, useState } from "react";
import { FormProvider, useForm } from "react-hook-form";
import IngredientsForm from "../IngredientsForm";
Expand Down Expand Up @@ -40,4 +40,25 @@ describe("IngredientsForm Rendering", () => {
screen.getByTestId("table-container-ingredients.nutrients"),
).toBeInTheDocument();
});

it("should not render the nutrient section when record-keeping is set to yes, then render it again when set to no", () => {
render(<Wrapper initialData={DEFAULT_LABEL_DATA} />);
fireEvent.click(
screen.getByTestId("radio-yes-field-ingredients.recordKeeping.value"),
);
setTimeout(() => {
expect(
screen.queryByTestId("table-container-ingredients.nutrients"),
).not.toBeInTheDocument();

fireEvent.click(
screen.getByTestId("radio-no-field-ingredients.recordKeeping.value"),
);
setTimeout(() => {
expect(
screen.getByTestId("table-container-ingredients.nutrients"),
).toBeInTheDocument();
}, 350);
}, 350);
});
});
12 changes: 12 additions & 0 deletions src/utils/server/__tests__/modelTransformation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,16 @@ describe("mapLabelDataToInspectionUpdate", () => {
});
expect(result.ingredients).toEqual({ en: [], fr: [] });
});

it("should return empty ingredients when recordKeeping is true", () => {
const modifiedLabelData: LabelData = {
...labelData,
ingredients: {
...labelData.ingredients,
recordKeeping: { value: true, verified: false },
},
};
const result = mapLabelDataToInspectionUpdate(modifiedLabelData);
expect(result.ingredients).toEqual({ en: [], fr: [] });
});
});

0 comments on commit 5cfffbc

Please sign in to comment.