import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { listCompanyAccounts, createCompanyAccount, updateCompanyAccount, deactivateCompanyAccount, reactivateCompanyAccount, } from "./adminCompanyAccount"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); mocks.apiPut.mockReset(); }); describe("listCompanyAccounts", () => { it("GETs /api/admin/company-accounts/{prefix} and returns the rows", async () => { const rows = [ { prefix: "MY", memberCode: "MYHQ00002", name: "HQ Two", weight: 50, active: "Y", configVersion: 3, directDownline: 2, allocatedCount: 10, }, ]; mocks.apiGet.mockResolvedValue(rows); const result = await listCompanyAccounts("MY"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/company-accounts/MY"); expect(result).toEqual(rows); }); it("encodes the prefix in the URL", async () => { mocks.apiGet.mockResolvedValue([]); await listCompanyAccounts("my sg"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/company-accounts/my%20sg"); }); }); describe("createCompanyAccount", () => { it("POSTs the full payload to /api/admin/company-accounts", async () => { const payload = { prefix: "MY", memberCode: "MYHQ00002", name: "HQ Two", weight: 50, }; mocks.apiPost.mockResolvedValue({ memberId: "1", loginRequested: false, credentialCreated: false }); const result = await createCompanyAccount(payload); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts", payload); expect(result).toEqual({ memberId: "1", loginRequested: false, credentialCreated: false }); }); it("includes email/password when an optional login is requested", async () => { const payload = { prefix: "MY", memberCode: "MYHQ00003", name: "HQ Three", weight: 25, email: "hq3@babyboo.my", password: "Qweqwe123", }; mocks.apiPost.mockResolvedValue({ memberId: "2", loginRequested: true, credentialCreated: true, message: "Account and login created.", }); const result = await createCompanyAccount(payload); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts", payload); expect(result.credentialCreated).toBe(true); }); }); describe("updateCompanyAccount", () => { it("PUTs to /api/admin/company-accounts with prefix/memberCode locating the row", async () => { const payload = { prefix: "MY", memberCode: "MYHQ00002", weight: 75, active: "Y" as const }; mocks.apiPut.mockResolvedValue({ ...payload, name: "HQ Two", configVersion: 4 }); await updateCompanyAccount(payload); expect(mocks.apiPut).toHaveBeenCalledWith("/api/admin/company-accounts", payload); }); }); describe("deactivateCompanyAccount", () => { it("POSTs to /api/admin/company-accounts/{prefix}/{memberCode}/deactivate", async () => { mocks.apiPost.mockResolvedValue(undefined); await deactivateCompanyAccount("MY", "MYHQ00002"); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts/MY/MYHQ00002/deactivate"); }); }); describe("reactivateCompanyAccount", () => { it("POSTs to /api/admin/company-accounts/{prefix}/{memberCode}/reactivate", async () => { mocks.apiPost.mockResolvedValue(undefined); await reactivateCompanyAccount("MY", "MYHQ00002"); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts/MY/MYHQ00002/reactivate"); }); });