A binary mobile-api call with the same error shape, export proxy routes that pass Content-Type and Content-Disposition through, and an Export button under BO_LEADS_EXPORT that downloads with the current search or filters and shows EXPORT_TOO_LARGE on the page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
121 lines
5.9 KiB
TypeScript
121 lines
5.9 KiB
TypeScript
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ listLeads: vi.fn(), exportLeads: vi.fn(), useAdminAuth: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminLeads", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/adminLeads")>()),
|
|
listLeads: mocks.listLeads,
|
|
exportLeads: mocks.exportLeads,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import LeadsPage from "./page";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
const ROW = {
|
|
leadUuid: "l-1", name: "Aisyah", mobile: "+60123456701", email: "a@x.test", companyName: "Example Co", jobTitle: null,
|
|
eventCount: 2, lastEvent: { uuid: "e-2", name: "Penang Expo" }, lastCapturedAt: "2026-11-20T09:30:00Z",
|
|
latestSalesperson: { id: 7, name: "Ali" },
|
|
};
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/leads");
|
|
mocks.listLeads.mockReset().mockResolvedValue({ items: [ROW], total: 1 });
|
|
mocks.exportLeads.mockReset().mockResolvedValue(undefined);
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true });
|
|
});
|
|
|
|
describe("LeadsPage", () => {
|
|
it("renders NotAuthorized without BO_LEADS", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<LeadsPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(mocks.listLeads).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows events count, last event and latest salesperson, with the name linking to the detail", async () => {
|
|
render(<LeadsPage />);
|
|
const row = (await screen.findByRole("link", { name: "Aisyah" })).closest("tr")!;
|
|
expect(screen.getByRole("link", { name: "Aisyah" })).toHaveAttribute("href", "/leads/l-1");
|
|
const r = within(row);
|
|
for (const text of ["+60123456701", "a@x.test", "Example Co", "2", "Penang Expo", "Ali"]) expect(r.getByText(text)).toBeVisible();
|
|
});
|
|
|
|
it("sends q as page 1 after the search box changes", async () => {
|
|
const user = userEvent.setup();
|
|
render(<LeadsPage />);
|
|
await waitFor(() => expect(mocks.listLeads).toHaveBeenCalled());
|
|
await user.type(screen.getByRole("searchbox", { name: "Search name, mobile or email" }), "aisyah");
|
|
await waitFor(() => expect(mocks.listLeads).toHaveBeenLastCalledWith(expect.objectContaining({ q: "aisyah", page: 1 })));
|
|
expect(memoryRouter.getSearch().get("q")).toBe("aisyah");
|
|
});
|
|
|
|
it("shows the empty state, and a no-match variant while searching", async () => {
|
|
mocks.listLeads.mockResolvedValue({ items: [], total: 0 });
|
|
const { unmount } = render(<LeadsPage />);
|
|
expect(await screen.findByText("No leads found")).toBeVisible();
|
|
unmount();
|
|
memoryRouter.reset("/leads", "q=zzz");
|
|
render(<LeadsPage />);
|
|
expect(await screen.findByText("No leads match your filters")).toBeVisible();
|
|
});
|
|
|
|
it("shows the error state and retries", async () => {
|
|
mocks.listLeads.mockRejectedValueOnce(new Error("Server says: boom xyz"));
|
|
render(<LeadsPage />);
|
|
expect(await screen.findByText("Failed to load leads")).toBeVisible();
|
|
expect(screen.queryByText(/boom xyz/)).toBeNull();
|
|
await userEvent.click(screen.getByRole("button", { name: /retry/i }));
|
|
expect(await screen.findByRole("link", { name: "Aisyah" })).toBeVisible();
|
|
});
|
|
});
|
|
|
|
describe("LeadsPage export", () => {
|
|
it("hides Export without BO_LEADS_EXPORT", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: (m: string) => m !== "BO_LEADS_EXPORT" });
|
|
render(<LeadsPage />);
|
|
await screen.findByRole("link", { name: "Aisyah" });
|
|
expect(screen.queryByRole("button", { name: "Export" })).toBeNull();
|
|
});
|
|
|
|
it("exports with the current q and is disabled while running", async () => {
|
|
let done!: () => void;
|
|
mocks.exportLeads.mockReturnValue(new Promise<void>((r) => { done = r; }));
|
|
memoryRouter.reset("/leads", "q=aisyah");
|
|
const user = userEvent.setup();
|
|
render(<LeadsPage />);
|
|
await user.click(await screen.findByRole("button", { name: "Export" }));
|
|
expect(mocks.exportLeads).toHaveBeenCalledWith({ q: "aisyah" });
|
|
expect(screen.getByRole("button", { name: "Export" })).toBeDisabled();
|
|
done();
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Export" })).toBeEnabled());
|
|
});
|
|
|
|
it("shows the too-large message on EXPORT_TOO_LARGE", async () => {
|
|
mocks.exportLeads.mockRejectedValue(new ApiError("Too many", 422, { code: "EXPORT_TOO_LARGE", details: { limit: 20000 } }));
|
|
render(<LeadsPage />);
|
|
await userEvent.click(await screen.findByRole("button", { name: "Export" }));
|
|
expect(await screen.findByText("More than 20,000 rows. Narrow the search or filters.")).toBeVisible();
|
|
});
|
|
|
|
it("clears the export error once the search changes", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.exportLeads.mockRejectedValue(new ApiError("Too many", 422, { code: "EXPORT_TOO_LARGE", details: { limit: 20000 } }));
|
|
render(<LeadsPage />);
|
|
await user.click(await screen.findByRole("button", { name: "Export" }));
|
|
await screen.findByText("More than 20,000 rows. Narrow the search or filters.");
|
|
await user.type(screen.getByRole("searchbox", { name: "Search name, mobile or email" }), "aisyah");
|
|
await waitFor(() => expect(screen.queryByText("More than 20,000 rows. Narrow the search or filters.")).toBeNull());
|
|
});
|
|
|
|
it("shows the error message for other failures", async () => {
|
|
mocks.exportLeads.mockRejectedValue(new ApiError("Boom happened", 400));
|
|
render(<LeadsPage />);
|
|
await userEvent.click(await screen.findByRole("button", { name: "Export" }));
|
|
expect(await screen.findByText("Boom happened")).toBeVisible();
|
|
});
|
|
});
|