Skip to content

Commit

Permalink
add test for updated router logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tbolt committed Jan 22, 2025
1 parent d57ccf5 commit cd243ea
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions react-app/src/router.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
import { describe, it } from "vitest";
import { render } from "@testing-library/react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { describe, expect, it, vi } from "vitest";
import { router } from "./router";

describe("Route timeout modal tests", () => {
it("Private route includes <TimeoutModal>", () => {});
vi.mock("@/components", () => ({
TimeoutModal: () => <div data-testid="timeout-modal">TimeoutModal</div>,
Layout: ({ children }) => <div>{children}</div>,
}));

it("Public route does not include <TimeoutModal>", () => {});
vi.mock(import("@/features"), async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
Welcome: () => <div data-testid="welcome-page">Welcome</div>,
Dashboard: () => <div data-testid="dashboard-page">Dashboard</div>,
dashboardLoader: vi.fn(),
loader: vi.fn(),
};
});

describe("Router tests", () => {
it("should include <TimeoutModal /> in private routes", () => {
const { getByTestId } = render(
<MemoryRouter initialEntries={["/dashboard"]}>
<Routes>
{router.routes[0].children.map((route) => (
<Route key={route.path} path={route.path} element={route.element} />
))}
</Routes>
</MemoryRouter>,
);

expect(getByTestId("timeout-modal")).toBeDefined();
});

it("should not include <TimeoutModal /> in public routes", () => {
const { queryByTestId } = render(
<MemoryRouter initialEntries={["/"]}>
<Routes>
{router.routes[0].children.map((route) => (
<Route key={route.path} path={route.path} element={route.element} />
))}
</Routes>
</MemoryRouter>,
);

expect(queryByTestId("timeout-modal")).toBeNull();
});
});

0 comments on commit cd243ea

Please sign in to comment.