Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
71 lines
3.4 KiB
TypeScript
71 lines
3.4 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
matchLead: vi.fn(),
|
|
listEventSalespeople: vi.fn(),
|
|
listEventLeadEvents: vi.fn(),
|
|
listEventLeadInterests: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
useCompanyScope: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminEventLeads", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/adminEventLeads")>()),
|
|
matchLead: mocks.matchLead,
|
|
listEventSalespeople: mocks.listEventSalespeople,
|
|
listEventLeadEvents: mocks.listEventLeadEvents,
|
|
listEventLeadInterests: mocks.listEventLeadInterests,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope }));
|
|
vi.mock("next/navigation", async () => ({
|
|
...(await import("@/test/memoryRouter")).navigationMock,
|
|
useParams: () => ({ eventUuid: "evt-1" }),
|
|
}));
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import NewEventLeadPage from "./event-leads/[eventUuid]/new/page";
|
|
|
|
const COMPANIES = [
|
|
{ id: 1, name: "Ins Co", code: "INS", status: "A" },
|
|
{ id: 2, name: "CV Co", code: "CV", status: "A" },
|
|
];
|
|
const scope = (companyId?: number) => ({ companyId, companies: COMPANIES, showCompanyColumn: false, codeOf: () => "", setCompanyId: vi.fn() });
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/event-leads/evt-1/new");
|
|
mocks.matchLead.mockReset().mockResolvedValue({ mobile: "+65911", email: "a@b.com", candidates: [] });
|
|
mocks.listEventSalespeople.mockReset().mockResolvedValue([]);
|
|
mocks.listEventLeadEvents.mockReset().mockResolvedValue([{ uuid: "evt-1", name: "Expo", companyId: 2 }]);
|
|
mocks.listEventLeadInterests.mockReset().mockResolvedValue([]);
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, isSuper: true, hasModule: () => true });
|
|
mocks.useCompanyScope.mockReset().mockReturnValue(scope(2));
|
|
});
|
|
|
|
describe("event-lead capture company", () => {
|
|
it("shows no Company picker to a super admin and sends the event's own company with the lead match", async () => {
|
|
mocks.useCompanyScope.mockReturnValue(scope());
|
|
const user = userEvent.setup();
|
|
render(<NewEventLeadPage />);
|
|
expect(screen.queryByRole("combobox", { name: "Company" })).not.toBeInTheDocument();
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Next" })).toBeEnabled());
|
|
await user.type(screen.getByLabelText("Mobile"), "91111111");
|
|
await user.type(screen.getByLabelText("Email"), "a@b.com");
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
|
await waitFor(() => expect(mocks.matchLead).toHaveBeenCalledWith("evt-1", expect.objectContaining({ email: "a@b.com" }), 2));
|
|
});
|
|
|
|
it("does not show Company to staff and sends no companyId", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, isSuper: false, hasModule: () => true });
|
|
mocks.useCompanyScope.mockReturnValue(scope());
|
|
const user = userEvent.setup();
|
|
render(<NewEventLeadPage />);
|
|
expect(screen.queryByRole("combobox", { name: "Company" })).not.toBeInTheDocument();
|
|
await user.type(screen.getByLabelText("Mobile"), "91111111");
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
|
await waitFor(() => expect(mocks.matchLead).toHaveBeenCalled());
|
|
expect(mocks.matchLead.mock.calls[0]).toHaveLength(2);
|
|
});
|
|
});
|