Skip to content

Commit

Permalink
test: vitests for transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
BCerki committed Nov 13, 2024
1 parent f9598ec commit 293dd60
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import "@testing-library/jest-dom";
import { render, screen, within } from "@testing-library/react";
import { useRouter, useSearchParams } from "@bciers/testConfig/mocks";
import React from "react";
import TransfersDataGrid from "@/registration/app/components/transfers/TransfersDataGrid";

useRouter.mockReturnValue({
query: {},
replace: vi.fn(),
});

useSearchParams.mockReturnValue({
get: vi.fn(),
});

const mockResponse = {
rows: [
{
id: "3b5b95ea-2a1a-450d-8e2e-2e15feed96c9",
operation__name: "Operation 1",
facilities__name: "N/A",
status: "Transferred",
created_at: "Jan 5, 2024\n4:25:37 p.m. PDT",
effective_date: "Feb 1, 2025\n1:00:00 a.m. PST",
},
{
id: "d99725a7-1c3a-47cb-a59b-e2388ce0fa18",
operation__name: "Operation 2",
facilities__name: "N/A",
status: "To be transferred",
created_at: "Jul 5, 2024\n4:25:37 p.m. PDT",
effective_date: "Aug 21, 2024\n2:00:00 a.m. PDT",
},
{
id: "f486f2fb-62ed-438d-bb3e-0819b51e3aeb",
operation__name: "N/A",
facilities__name: "Facility 1",
status: "Completed",
created_at: "Jul 5, 2024\n4:25:37 p.m. PDT",
effective_date: "Dec 25, 2024\n1:00:00 a.m. PST",
},
{
id: "459b80f9-b5f3-48aa-9727-90c30eaf3a58",
operation__name: "N/A",
facilities__name: "Facility 2",
status: "Completed",
created_at: "Jul 5, 2024\n4:25:37 p.m. PDT",
effective_date: "Dec 25, 2024\n1:00:00 a.m. PST",
},
],
row_count: 4,
};

describe("TransfersDataGrid component", () => {
beforeEach(async () => {
vi.clearAllMocks();
});

it("renders the TransfersDataGrid grid ", async () => {
render(<TransfersDataGrid initialData={mockResponse} />);

// correct headers
expect(
screen.getByRole("columnheader", { name: "Submission Date" }),
).toBeVisible();
expect(
screen.queryByRole("columnheader", { name: "Operation" }),
).toBeInTheDocument();
expect(
screen.getByRole("columnheader", { name: "Facility" }),
).toBeVisible();
expect(screen.getByRole("columnheader", { name: "Status" })).toBeVisible();
expect(
screen.getByRole("columnheader", { name: "Effective Date" }),
).toBeVisible();

expect(screen.queryAllByPlaceholderText(/Search/i)).toHaveLength(3);

const rows = screen.getAllByRole("row"); // row 0 is headers and row 1 is filter cells

const operation1Row = rows[2];
expect(
within(operation1Row).getByText("Jan 5, 2024 4:25:37 p.m. PDT"),
).toBeInTheDocument();
expect(within(operation1Row).getByText("Operation 1")).toBeInTheDocument();
expect(within(operation1Row).getByText("N/A")).toBeInTheDocument();
expect(within(operation1Row).getByText("Transferred")).toBeInTheDocument();
expect(
within(operation1Row).getByText("Feb 1, 2025 1:00:00 a.m. PST"),
).toBeInTheDocument();
expect(within(operation1Row).getByText("View Details")).toBeInTheDocument();

const opeartion2Row = rows[3];
expect(
within(opeartion2Row).getByText("Jul 5, 2024 4:25:37 p.m. PDT"),
).toBeInTheDocument();
expect(within(opeartion2Row).getByText("Operation 2")).toBeInTheDocument();
expect(within(opeartion2Row).getByText("N/A")).toBeInTheDocument();
expect(
within(opeartion2Row).getByText("To be transferred"),
).toBeInTheDocument();
expect(
within(opeartion2Row).getByText("Aug 21, 2024 2:00:00 a.m. PDT"),
).toBeInTheDocument();
expect(within(opeartion2Row).getByText("View Details")).toBeInTheDocument();
const facility1Row = rows[4];

expect(
within(facility1Row).getByText("Jul 5, 2024 4:25:37 p.m. PDT"),
).toBeInTheDocument();
expect(within(facility1Row).getByText("N/A")).toBeInTheDocument();
expect(within(facility1Row).getByText("Facility 1")).toBeInTheDocument();
expect(within(facility1Row).getByText("Completed")).toBeInTheDocument();
expect(
within(facility1Row).getByText("Dec 25, 2024 1:00:00 a.m. PST"),
).toBeInTheDocument();
expect(within(facility1Row).getByText("View Details")).toBeInTheDocument();

const facility2Row = rows[5];
expect(
within(facility2Row).getByText("Jul 5, 2024 4:25:37 p.m. PDT"),
).toBeInTheDocument();
expect(within(facility2Row).getByText("N/A")).toBeInTheDocument();
expect(within(facility2Row).getByText("Facility 2")).toBeInTheDocument();
expect(within(facility2Row).getByText("Completed")).toBeInTheDocument();
expect(
within(facility2Row).getByText("Dec 25, 2024 1:00:00 a.m. PST"),
).toBeInTheDocument();
expect(within(facility2Row).getByText("View Details")).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { render, screen } from "@testing-library/react";
import {
fetchTransferEventsPageData,
useRouter,
useSearchParams,
} from "@bciers/testConfig/mocks";
import TransfersDataGridPage from "@/registration/app/components/transfers/TransfersDataGridPage";

useRouter.mockReturnValue({
query: {},
replace: vi.fn(),
});

useSearchParams.mockReturnValue({
get: vi.fn(),
});

vi.mock(
"apps/registration/app/components/transfers/fetchTransferEventsPageData",
() => ({
default: fetchTransferEventsPageData,
}),
);

const mockResponse = {
items: [
{
operation__id: "3b5b95ea-2a1a-450d-8e2e-2e15feed96c9",
operation__name: "Operation 3",
facilities__name: null,
facility__id: null,
id: "3b5b95ea-2a1a-450d-8e2e-2e15feed96c9",
effective_date: "2025-02-01T09:00:00Z",
status: "Transferred",
created_at: "2024-07-05T23:25:37.892Z",
},
{
operation__id: "d99725a7-1c3a-47cb-a59b-e2388ce0fa18",
operation__name: "Operation 6",
facilities__name: null,
facility__id: null,
id: "d99725a7-1c3a-47cb-a59b-e2388ce0fa18",
effective_date: "2024-08-21T09:00:00Z",
status: "To be transferred",
created_at: "2024-07-05T23:25:37.892Z",
},
{
operation__id: null,
operation__name: null,
facilities__name: "Facility 1",
facility__id: "f486f2fb-62ed-438d-bb3e-0819b51e3aeb",
id: "f486f2fb-62ed-438d-bb3e-0819b51e3aeb",
effective_date: "2024-12-25T09:00:00Z",
status: "Completed",
created_at: "2024-07-05T23:25:37.892Z",
},
{
operation__id: null,
operation__name: null,
facilities__name: "Facility 2",
facility__id: "459b80f9-b5f3-48aa-9727-90c30eaf3a58",
id: "459b80f9-b5f3-48aa-9727-90c30eaf3a58",
effective_date: "2024-12-25T09:00:00Z",
status: "Completed",
created_at: "2024-07-05T23:25:37.892Z",
},
],
row_count: 4,
};

describe("Transfers component", () => {
beforeEach(async () => {
vi.clearAllMocks();
});

it("renders a message when there are no transfers in the database", async () => {
fetchTransferEventsPageData.mockReturnValueOnce(undefined);
render(await TransfersDataGridPage({ searchParams: {} }));
expect(screen.queryByRole("grid")).not.toBeInTheDocument();
expect(screen.getByText(/No transfers data in database./i)).toBeVisible();
});

it("renders the TransfersDataGrid component when there are transfers in the database", async () => {
fetchTransferEventsPageData.mockReturnValueOnce(mockResponse);
render(await TransfersDataGridPage({ searchParams: {} }));
expect(screen.getByRole("grid")).toBeVisible();
expect(
screen.queryByText(/No transfers data in database./i),
).not.toBeInTheDocument();
});
});
2 changes: 2 additions & 0 deletions bciers/libs/testConfig/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const useSession = vi.fn();
const auth = vi.fn();
const fetchOperationsPageData = vi.fn();
const fetchOperatorsPageData = vi.fn();
const fetchTransferEventsPageData = vi.fn();

export {
actionHandler,
Expand All @@ -49,4 +50,5 @@ export {
fetchOperationsPageData,
fetchOperatorsPageData,
notFound,
fetchTransferEventsPageData,
};

0 comments on commit 293dd60

Please sign in to comment.