Files

68 lines
2.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn(), apiDownload: vi.fn() }));
vi.mock("@/app/lib/clientApi", () => mocks);
import {
captureEventLead,
exportEventLeads,
getEventLead,
listEventLeadEvents,
listEventLeadInterests,
listEventLeads,
listEventSalespeople,
matchLead,
updateEventLead,
} from "./adminEventLeads";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
mocks.apiPut.mockReset();
});
describe("adminEventLeads", () => {
it("loads events, interest options and the event's salespeople", async () => {
mocks.apiGet.mockResolvedValue([]);
await listEventLeadEvents();
await listEventLeadInterests();
await listEventSalespeople("9");
expect(mocks.apiGet.mock.calls.map((c) => c[0])).toEqual([
"/api/event-leads/events",
"/api/event-leads/interest-options",
"/api/event-leads/9/salesperson-options",
]);
});
it("lists with only the set filters and a 0-based page", async () => {
mocks.apiGet.mockResolvedValue({ content: [{ eventLeadUuid: "1" }], totalElements: 41 });
const out = await listEventLeads("9", { page: 3, pageSize: 20, status: "OPEN", temperature: "", interestId: "4" });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/event-leads/9/leads?status=OPEN&interestId=4&page=2&size=20");
expect(out).toEqual({ items: [{ eventLeadUuid: "1" }], total: 41 });
});
it("sends the attended filter to the list and to the export", async () => {
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
await listEventLeads("9", { page: 1, pageSize: 20, attended: "false" });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/event-leads/9/leads?attended=false&page=0&size=20");
await exportEventLeads("9", { attended: "true" });
expect(mocks.apiDownload).toHaveBeenCalledWith("/api/event-leads/9/export?attended=true", "event-leads.xlsx");
});
it("treats a malformed page as empty", async () => {
mocks.apiGet.mockResolvedValue(null);
expect(await listEventLeads("9", { page: 1, pageSize: 20 })).toEqual({ items: [], total: 0 });
});
it("matches, captures, gets and updates under the event", async () => {
await matchLead("9", { mobile: "0123", email: null });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/event-leads/9/match", { mobile: "0123", email: null });
await captureEventLead("9", { name: "A", mobile: "0123" });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/event-leads/9", { name: "A", mobile: "0123" });
await getEventLead("9", "7");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/event-leads/9/leads/7");
await updateEventLead("9", "7", { status: "WON" });
expect(mocks.apiPut).toHaveBeenCalledWith("/api/event-leads/9/leads/7", { status: "WON" });
});
});