The picker tests waited only for the load call, then read the cards before they rendered, which failed intermittently in the full suite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
107 lines
4.9 KiB
TypeScript
107 lines
4.9 KiB
TypeScript
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
listEventLeadEvents: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminEventLeads", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/adminEventLeads")>()),
|
|
listEventLeadEvents: mocks.listEventLeadEvents,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import EventLeadsPage from "./page";
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/event-leads");
|
|
mocks.listEventLeadEvents.mockReset();
|
|
mocks.useAdminAuth.mockReset();
|
|
mocks.useAdminAuth.mockReturnValue({
|
|
loading: false,
|
|
hasModule: () => true,
|
|
});
|
|
});
|
|
|
|
describe("EventLeadsPage (picker)", () => {
|
|
it("renders NotAuthorized when the EventLeads module is absent from aclMenu", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<EventLeadsPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
});
|
|
|
|
it("lists events as tappable card links", async () => {
|
|
mocks.listEventLeadEvents.mockResolvedValue([
|
|
{ uuid: "evt-1", name: "Event Alpha", location: "Hall A", startDate: "2025-06-01", endDate: "2025-06-02" },
|
|
{ uuid: "evt-2", name: "Event Beta", location: "Hall B", startDate: "2025-07-01", endDate: "2025-07-02" },
|
|
]);
|
|
render(<EventLeadsPage />);
|
|
|
|
expect(await screen.findByText("Event Alpha")).toBeInTheDocument();
|
|
expect(screen.getByText("Event Beta")).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /Event Alpha/ })).toHaveAttribute("href", "/event-leads/evt-1");
|
|
expect(screen.getByRole("link", { name: /Event Beta/ })).toHaveAttribute("href", "/event-leads/evt-2");
|
|
});
|
|
|
|
it("shows location and dates formatted with formatDmy", async () => {
|
|
mocks.listEventLeadEvents.mockResolvedValue([
|
|
{ uuid: "evt-1", name: "Event Alpha", location: "Hall A", startDate: "2025-06-01", endDate: "2025-06-02" },
|
|
]);
|
|
render(<EventLeadsPage />);
|
|
|
|
expect(await screen.findByText("Hall A")).toBeInTheDocument();
|
|
expect(screen.getByText(/01\/06\/2025/)).toBeInTheDocument();
|
|
expect(screen.getByText(/02\/06\/2025/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("puts events running today first", async () => {
|
|
// Use a date that's "today" relative to the test run
|
|
const today = new Date();
|
|
const iso = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, "0")}-${String(today.getDate()).padStart(2, "0")}`;
|
|
mocks.listEventLeadEvents.mockResolvedValue([
|
|
{ uuid: "evt-far", name: "Far Event", location: "Hall A", startDate: "2099-01-01", endDate: "2099-01-02" },
|
|
{ uuid: "evt-today", name: "Today Event", location: "Hall B", startDate: iso, endDate: iso },
|
|
]);
|
|
render(<EventLeadsPage />);
|
|
|
|
await screen.findByText("Today Event");
|
|
const links = Array.from(screen.getAllByRole("link").filter((l) => l.getAttribute("href")?.startsWith("/event-leads/")));
|
|
expect(links[0]).toHaveAttribute("href", "/event-leads/evt-today");
|
|
expect(links[1]).toHaveAttribute("href", "/event-leads/evt-far");
|
|
});
|
|
|
|
it("shows an error state on load failure", async () => {
|
|
mocks.listEventLeadEvents.mockRejectedValueOnce(new Error("network down"));
|
|
render(<EventLeadsPage />);
|
|
await waitFor(() => expect(screen.getByText(/network down/i)).toBeInTheDocument());
|
|
});
|
|
|
|
it("counts an event that started earlier and ends later as running today", async () => {
|
|
const d = (offset: number) => {
|
|
const x = new Date();
|
|
x.setDate(x.getDate() + offset);
|
|
return `${x.getFullYear()}-${String(x.getMonth() + 1).padStart(2, "0")}-${String(x.getDate()).padStart(2, "0")}`;
|
|
};
|
|
mocks.listEventLeadEvents.mockResolvedValue([
|
|
{ uuid: "evt-later", name: "Later", location: null, startDate: d(5), endDate: d(6) },
|
|
{ uuid: "evt-running", name: "Running", location: null, startDate: d(-1), endDate: d(1) },
|
|
]);
|
|
render(<EventLeadsPage />);
|
|
await waitFor(() => expect(screen.getByText("Running")).toBeInTheDocument());
|
|
const links = screen.getAllByRole("link").filter((l) => l.getAttribute("href")?.startsWith("/event-leads/"));
|
|
expect(links[0]).toHaveAttribute("href", "/event-leads/evt-running");
|
|
});
|
|
|
|
it("retries the load after a failure", async () => {
|
|
mocks.listEventLeadEvents.mockRejectedValueOnce(new Error("network down"))
|
|
.mockResolvedValueOnce([{ uuid: "evt-1", name: "Back", location: null, startDate: null, endDate: null }]);
|
|
render(<EventLeadsPage />);
|
|
const retry = await screen.findByRole("button", { name: /retry/i });
|
|
await act(async () => retry.click());
|
|
expect(await screen.findByText("Back")).toBeInTheDocument();
|
|
});
|
|
});
|