Skip to content

Commit

Permalink
Add better mocking for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelltaylor committed May 3, 2024
1 parent 6744628 commit a30ecce
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 26 deletions.
10 changes: 10 additions & 0 deletions florist/app/jobs/hooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import useSWR from "swr";
import { fetcher } from "../client_imports";

export default function useJobStatus(status: string) {
const endpoint = "/api/server/job/".concat(status);
const { data, error, isLoading } = useSWR(endpoint, fetcher, {
refresh_interval: 1000,
});
return { data, error, isLoading };
}
17 changes: 8 additions & 9 deletions florist/app/jobs/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import { ReactElement } from "react/React";
import useSWR from "swr";
import { fetcher } from "../client_imports";
import useJobStatus from "./hooks";

const valid_statuses = {
NOT_STARTED: "Not Started",
Expand Down Expand Up @@ -45,12 +44,9 @@ export default function Page(): ReactElement {
}

export function Status({ status }: StatusProp): ReactElement {
const endpoint = "/api/server/job/".concat(status);
const { data, error, isLoading } = useSWR(endpoint, fetcher, {
refresh_interval: 1000,
});
if (error) return <span> </span>;
if (isLoading) return <span> </span>;
const { data, error, isLoading } = useJobStatus(status);
if (error) return <span> Help1</span>;
if (isLoading) return <span> Help2 </span>;

return (
<div>
Expand Down Expand Up @@ -87,7 +83,10 @@ export function StatusTable({
} else {
return (
<div>
<span> No jobs to display. </span>
<span data-testid={`status-no-jobs-${status}`}>
{" "}
No jobs to display.{" "}
</span>
</div>
);
}
Expand Down
106 changes: 89 additions & 17 deletions florist/tests/unit/app/jobs/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import useSWR from "swr";
import "@testing-library/jest-dom";
import { getByText, render, cleanup } from "@testing-library/react";
import { describe, beforeEach, afterEach, it, expect } from "@jest/globals";
import { describe, afterEach, it, expect } from "@jest/globals";

import Page from "../../../../app/jobs/page";
import useJobStatus from "../../../../app/jobs/hooks";

jest.mock("swr", () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock("../../../../app/jobs/hooks");

afterEach(() => {
jest.clearAllMocks();
cleanup();
});

function mock_job_data(
model: string,
Expand All @@ -27,26 +29,53 @@ function mock_job_data(
return data;
}

beforeEach(() => {
const mock_jobs = [
mock_job_data("MNIST", "localhost:8080", ["localhost:7080"]),
];
useSWR.mockImplementation(() => ({
data: mock_jobs,
error: null,
isLoading: false,
}));
});
function setupMock(
valid_statuses: Array<string>,
data: Array<object>,
error: boolean,
isLoading: boolean,
) {
useJobStatus.mockImplementation((status: string) => {
if (valid_statuses.includes(status)) {
return {
data,
error,
isLoading,
};
} else {
return {
data: [],
error: false,
isLoading: false,
};
}
});
}

describe("List Jobs Page", () => {
it("Renders Page Title correct", () => {
setupMock([], [], false, false);
const { container } = render(<Page />);
const h1 = container.querySelector("h1");
expect(h1).toBeInTheDocument();
expect(h1).toHaveTextContent("Job Status");
});

it("Renders Status Components Headers", () => {
const data = [
mock_job_data("MNIST", "localhost:8080", ["localhost:7080"]),
];
setupMock(
[
"NOT_STARTED",
"IN_PROGRESS",
"FINISHED_SUCCESSFULLY",
"FINISHED_WITH_ERROR",
],
data,
false,
false,
);
const { getByTestId } = render(<Page />);
const ns_status = getByTestId("status-header-NOT_STARTED");
const ip_status = getByTestId("status-header-IN_PROGRESS");
Expand All @@ -62,7 +91,21 @@ describe("List Jobs Page", () => {
expect(fe_status).toHaveTextContent("Finished with Error");
});

it("Renders Status Table", () => {
it("Renders Status Table With Table with Data", () => {
const data = [
mock_job_data("MNIST", "localhost:8080", ["localhost:7080"]),
];
setupMock(
[
"NOT_STARTED",
"IN_PROGRESS",
"FINISHED_SUCCESSFULLY",
"FINISHED_WITH_ERROR",
],
data,
false,
false,
);
const { getByTestId } = render(<Page />);
const ns_table = getByTestId("status-table-NOT_STARTED");

Expand Down Expand Up @@ -108,4 +151,33 @@ describe("List Jobs Page", () => {
expect(getByText(fe_table, "localhost:8080")).toBeInTheDocument();
expect(getByText(fe_table, "localhost:7080")).toBeInTheDocument();
});

it("Renders Status Table With Table without Data", () => {
setupMock([], [], false, false);
const { getByTestId } = render(<Page />);

const not_started = getByTestId("status-no-jobs-NOT_STARTED");
expect(
getByText(not_started, "No jobs to display."),
).toBeInTheDocument();

const in_progress = getByTestId("status-no-jobs-IN_PROGRESS");
expect(
getByText(in_progress, "No jobs to display."),
).toBeInTheDocument();

const finished_success = getByTestId(
"status-no-jobs-FINISHED_SUCCESSFULLY",
);
expect(
getByText(finished_success, "No jobs to display."),
).toBeInTheDocument();

const finished_error = getByTestId(
"status-no-jobs-FINISHED_WITH_ERROR",
);
expect(
getByText(finished_error, "No jobs to display."),
).toBeInTheDocument();
});
});

0 comments on commit a30ecce

Please sign in to comment.