Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
326 lines
15 KiB
TypeScript
326 lines
15 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";
|
|
|
|
// --- test mocks ---
|
|
const mocks = vi.hoisted(() => ({
|
|
listEventLeads: vi.fn(),
|
|
exportEventLeads: vi.fn(),
|
|
listEventSalespeople: vi.fn(),
|
|
listEventLeadInterests: vi.fn(),
|
|
listEventLeadEvents: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminEventLeads", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/adminEventLeads")>()),
|
|
listEventLeads: mocks.listEventLeads,
|
|
exportEventLeads: mocks.exportEventLeads,
|
|
listEventSalespeople: mocks.listEventSalespeople,
|
|
listEventLeadInterests: mocks.listEventLeadInterests,
|
|
listEventLeadEvents: mocks.listEventLeadEvents,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("next/navigation", async () => ({
|
|
...(await import("@/test/memoryRouter")).navigationMock,
|
|
useParams: () => ({ eventUuid: "evt-1" }),
|
|
}));
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import EventLeadsDetailPage from "./page";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/event-leads/evt-1");
|
|
mocks.listEventLeads.mockReset().mockResolvedValue({ items: [], total: 0 });
|
|
mocks.exportEventLeads.mockReset().mockResolvedValue(undefined);
|
|
mocks.listEventSalespeople.mockReset().mockResolvedValue([{ id: 1, name: "Jane Sales", username: "jane" }]);
|
|
mocks.listEventLeadInterests.mockReset().mockResolvedValue([{ id: 1, name: "Product A", active: true }]);
|
|
mocks.listEventLeadEvents.mockReset().mockResolvedValue([{ uuid: "evt-1", name: "Test Event", location: "Hall A", startDate: "2025-06-01", endDate: "2025-06-02" }]);
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({
|
|
loading: false,
|
|
hasModule: () => true,
|
|
});
|
|
});
|
|
|
|
describe("EventLeadsDetailPage (listing)", { timeout: 20000 }, () => {
|
|
|
|
it("renders NotAuthorized when the EventLeads module is absent", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<EventLeadsDetailPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
});
|
|
|
|
it("sends eventUuid and each filter to listEventLeads", async () => {
|
|
const user = userEvent.setup();
|
|
render(<EventLeadsDetailPage />);
|
|
|
|
await waitFor(() => expect(mocks.listEventLeads).toHaveBeenCalledWith("evt-1", expect.objectContaining({ page: 1, pageSize: expect.any(Number) })));
|
|
|
|
// Filter by temperature
|
|
await user.click(screen.getByRole("combobox", { name: /temperature filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Hot" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ temperature: "HOT", page: 1 })),
|
|
);
|
|
|
|
// Filter by status
|
|
await user.click(screen.getByRole("combobox", { name: /status filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Won" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ status: "WON", page: 1 })),
|
|
);
|
|
|
|
// Filter by lead type
|
|
await user.click(screen.getByRole("combobox", { name: /lead type filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "New" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ leadType: "NEW", page: 1 })),
|
|
);
|
|
|
|
// Filter by salesperson
|
|
await user.click(screen.getByRole("combobox", { name: /salesperson filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Jane Sales" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ salespersonId: "1", page: 1 })),
|
|
);
|
|
|
|
// Filter by interest
|
|
await user.click(screen.getByRole("combobox", { name: /interest filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Product A" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ interestId: "1", page: 1 })),
|
|
);
|
|
});
|
|
|
|
it("renders lead cards on mobile view with data-testid", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({
|
|
items: [{
|
|
eventLeadUuid: "el-1",
|
|
eventUuid: "evt-1",
|
|
leadUuid: "lead-1",
|
|
name: "John Doe",
|
|
mobile: "+60123456789",
|
|
email: "john@example.com",
|
|
companyName: "Acme",
|
|
jobTitle: "Manager",
|
|
leadType: "NEW",
|
|
salesperson: { id: 1, name: "Jane Sales" },
|
|
temperature: "HOT",
|
|
status: "OPEN",
|
|
interests: [{ id: 1, name: "Product A", active: true }],
|
|
nextFollowUpDate: "2025-06-15",
|
|
notes: null,
|
|
capturedAt: "2025-06-01",
|
|
}],
|
|
total: 1,
|
|
});
|
|
render(<EventLeadsDetailPage />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByTestId("lead-card")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("shows Attended and Source as the last two columns", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({
|
|
items: [{
|
|
eventLeadUuid: "el-1", eventUuid: "evt-1", leadUuid: "lead-1", name: "John Doe", mobile: null, email: null,
|
|
companyName: null, jobTitle: null, leadType: "NEW", salesperson: null, temperature: null, status: "OPEN",
|
|
interests: [], nextFollowUpDate: null, notes: null, capturedAt: "2025-06-01", attended: true, source: "INVITE",
|
|
}],
|
|
total: 1,
|
|
});
|
|
render(<EventLeadsDetailPage />);
|
|
const row = (await screen.findByRole("link", { name: "John Doe" })).closest("tr")!;
|
|
const headers = screen.getAllByRole("columnheader").map((h) => h.textContent);
|
|
expect(headers.slice(-2)).toEqual(["Attended", "Source"]);
|
|
const cells = within(row).getAllByRole("cell").map((c) => c.textContent);
|
|
expect(cells.slice(-2)).toEqual(["Yes", "Invite"]);
|
|
});
|
|
|
|
it("shows Attended and Source on the phone card", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({
|
|
items: [{
|
|
eventLeadUuid: "el-1", eventUuid: "evt-1", leadUuid: "lead-1", name: "John Doe", mobile: null, email: null,
|
|
companyName: null, jobTitle: null, leadType: "NEW", salesperson: null, temperature: null, status: "OPEN",
|
|
interests: [], nextFollowUpDate: null, notes: null, capturedAt: "2025-06-01", attended: true, source: "QR",
|
|
}],
|
|
total: 1,
|
|
});
|
|
render(<EventLeadsDetailPage />);
|
|
const card = within(await screen.findByTestId("lead-card"));
|
|
expect(card.getByText("Attended: Yes")).toBeInTheDocument();
|
|
expect(card.getByText("Source: QR")).toBeInTheDocument();
|
|
});
|
|
|
|
it("labels ticket leads (buyers and consenting attendees) as Ticket", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({
|
|
items: [{
|
|
eventLeadUuid: "el-1", eventUuid: "evt-1", leadUuid: "lead-1", name: "John Doe", mobile: null, email: null,
|
|
companyName: null, jobTitle: null, leadType: "NEW", salesperson: null, temperature: null, status: "OPEN",
|
|
interests: [], nextFollowUpDate: null, notes: null, capturedAt: "2025-06-01", attended: null, source: "TICKET",
|
|
}],
|
|
total: 1,
|
|
});
|
|
render(<EventLeadsDetailPage />);
|
|
const row = (await screen.findByRole("link", { name: "John Doe" })).closest("tr")!;
|
|
expect(within(row).getAllByRole("cell").at(-1)!.textContent).toBe("Ticket");
|
|
expect(within(screen.getByTestId("lead-card")).getByText("Source: Ticket")).toBeInTheDocument();
|
|
});
|
|
|
|
it("filters by attendance for the list and the export", async () => {
|
|
const user = userEvent.setup();
|
|
render(<EventLeadsDetailPage />);
|
|
await user.click(await screen.findByRole("combobox", { name: /attended filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Not attended" }));
|
|
await waitFor(() =>
|
|
expect(mocks.listEventLeads).toHaveBeenLastCalledWith("evt-1", expect.objectContaining({ attended: "false", page: 1 })),
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "Export" }));
|
|
expect(mocks.exportEventLeads.mock.calls[0][1]).toEqual(expect.objectContaining({ attended: "false" }));
|
|
});
|
|
|
|
it("links each lead row/card to its detail page", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({
|
|
items: [{
|
|
eventLeadUuid: "el-1",
|
|
eventUuid: "evt-1",
|
|
leadUuid: "lead-1",
|
|
name: "John Doe",
|
|
mobile: "+60123456789",
|
|
email: "john@example.com",
|
|
companyName: "Acme",
|
|
jobTitle: "Manager",
|
|
leadType: "NEW",
|
|
salesperson: { id: 1, name: "Jane Sales" },
|
|
temperature: "HOT",
|
|
status: "OPEN",
|
|
interests: [{ id: 1, name: "Product A", active: true }],
|
|
nextFollowUpDate: "2025-06-15",
|
|
notes: null,
|
|
capturedAt: "2025-06-01",
|
|
}],
|
|
total: 1,
|
|
});
|
|
render(<EventLeadsDetailPage />);
|
|
|
|
await waitFor(() => {
|
|
// The lead card is a link to the detail page
|
|
const card = screen.getByTestId("lead-card");
|
|
expect(card).toHaveAttribute("href", "/event-leads/evt-1/el-1");
|
|
});
|
|
const card = within(screen.getByTestId("lead-card"));
|
|
for (const text of ["John Doe", "+60123456789", "New", "Hot", "Open"]) expect(card.getByText(text)).toBeInTheDocument();
|
|
expect(card.getByText(/Next follow-up.*15\/06\/2025/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("offers each filter's All choice once", async () => {
|
|
const user = userEvent.setup();
|
|
render(<EventLeadsDetailPage />);
|
|
await user.click(screen.getByRole("combobox", { name: /salesperson filter/i }));
|
|
await screen.findByRole("option", { name: "Jane Sales" });
|
|
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "Jane Sales"]);
|
|
});
|
|
|
|
it("falls back to the module title when the event is not in the list", async () => {
|
|
mocks.listEventLeadEvents.mockResolvedValue([]);
|
|
render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(screen.getByRole("heading", { level: 1, name: "Event leads" })).toBeInTheDocument());
|
|
});
|
|
|
|
it("shows the Add lead link to /event-leads/{eventUuid}/new", async () => {
|
|
render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(screen.getByRole("link", { name: /add lead/i })).toHaveAttribute("href", "/event-leads/evt-1/new"));
|
|
});
|
|
|
|
it("renders the header with the event name", async () => {
|
|
render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(screen.getByRole("heading", { level: 1, name: "Test Event" })).toBeInTheDocument());
|
|
});
|
|
|
|
it("shows the empty state when there are no leads", async () => {
|
|
render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(screen.queryByText("John Doe")).not.toBeInTheDocument());
|
|
});
|
|
|
|
it("shows the error state and lets the user retry on a load failure", async () => {
|
|
mocks.listEventLeads.mockRejectedValueOnce(new Error("network down"));
|
|
mocks.listEventLeads.mockResolvedValueOnce({ items: [], total: 0 });
|
|
render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(screen.getByText(/network down/i)).toBeInTheDocument());
|
|
await userEvent.click(screen.getByRole("button", { name: /retry/i }));
|
|
await waitFor(() => expect(mocks.listEventLeads).toHaveBeenCalledTimes(2));
|
|
});
|
|
|
|
it("hides the table below md and shows one pager per layout", async () => {
|
|
mocks.listEventLeads.mockResolvedValue({ items: [], total: 45 });
|
|
const { container } = render(<EventLeadsDetailPage />);
|
|
await waitFor(() => expect(mocks.listEventLeads).toHaveBeenCalled());
|
|
const table = screen.getByRole("table").closest("div.hidden");
|
|
expect(table).not.toBeNull();
|
|
expect(table!.className).toContain("md:block");
|
|
// DataTable brings its own pager; the phone pager (44px targets) must sit inside the md:hidden card list.
|
|
expect(container.querySelectorAll('[data-slot="table-pager"]')).toHaveLength(1);
|
|
const next = screen.getAllByRole("button", { name: /next/i }).find((b) => !b.closest('[data-slot="table-pager"]'));
|
|
expect(next?.closest(".md\\:hidden")).not.toBeNull();
|
|
});
|
|
|
|
it("labels temperature and status options through i18n keys", async () => {
|
|
const user = userEvent.setup();
|
|
render(<EventLeadsDetailPage />);
|
|
await user.click(screen.getByRole("combobox", { name: /temperature filter/i }));
|
|
expect(await screen.findByRole("option", { name: "Hot" })).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("EventLeadsDetailPage export", () => {
|
|
it("hides Export without BO_LEADS_EXPORT", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: (m: string) => m !== "BO_LEADS_EXPORT" });
|
|
render(<EventLeadsDetailPage />);
|
|
await screen.findByText("Test Event");
|
|
expect(screen.queryByRole("button", { name: "Export" })).toBeNull();
|
|
});
|
|
|
|
it("exports with the current filters (no paging) and is disabled while running", async () => {
|
|
let done!: () => void;
|
|
mocks.exportEventLeads.mockReturnValue(new Promise<void>((r) => { done = r; }));
|
|
memoryRouter.reset("/event-leads/evt-1", "status=OPEN&temperature=HOT");
|
|
const user = userEvent.setup();
|
|
render(<EventLeadsDetailPage />);
|
|
await user.click(await screen.findByRole("button", { name: "Export" }));
|
|
expect(mocks.exportEventLeads).toHaveBeenCalledTimes(1);
|
|
const [uuid, filters] = mocks.exportEventLeads.mock.calls[0];
|
|
expect(uuid).toBe("evt-1");
|
|
expect(filters).toMatchObject({ status: "OPEN", temperature: "HOT" });
|
|
expect(filters).not.toHaveProperty("page");
|
|
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.exportEventLeads.mockRejectedValue(new ApiError("Too many", 422, { code: "EXPORT_TOO_LARGE", details: { limit: 20000 } }));
|
|
render(<EventLeadsDetailPage />);
|
|
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 a filter changes", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.exportEventLeads.mockRejectedValue(new ApiError("Too many", 422, { code: "EXPORT_TOO_LARGE", details: { limit: 20000 } }));
|
|
render(<EventLeadsDetailPage />);
|
|
await user.click(await screen.findByRole("button", { name: "Export" }));
|
|
await screen.findByText("More than 20,000 rows. Narrow the search or filters.");
|
|
await user.click(screen.getByRole("combobox", { name: /temperature filter/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Hot" }));
|
|
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.exportEventLeads.mockRejectedValue(new ApiError("Boom happened", 400));
|
|
render(<EventLeadsDetailPage />);
|
|
await userEvent.click(await screen.findByRole("button", { name: "Export" }));
|
|
expect(await screen.findByText("Boom happened")).toBeVisible();
|
|
});
|
|
});
|