Files

84 lines
3.1 KiB
TypeScript

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 {
listAdminEvents,
getAdminEvent,
saveAdminEvent,
listSalespersonOptions,
} from "./adminEvents";
import type { AdminEventPayload } from "@/app/lib/types";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
mocks.apiPut.mockReset();
});
describe("listAdminEvents", () => {
it("translates 1-based page to a 0-based query and normalizes the page shape", async () => {
mocks.apiGet.mockResolvedValue({ content: [{ uuid: "e1" }], totalElements: 1 });
const result = await listAdminEvents({ page: 2, pageSize: 20 });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/events?page=1&size=20");
expect(result).toEqual({ items: [{ uuid: "e1" }], total: 1 });
});
it("sends q, enabled, sortBy, sortDir when given", async () => {
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
await listAdminEvents({ q: "event", enabled: "true", page: 1, pageSize: 20, sortBy: "name", sortDir: "asc" });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/events?q=event&enabled=true&page=0&size=20&sortBy=name&sortDir=asc");
});
});
describe("getAdminEvent", () => {
it("calls GET /api/events/{uuid}", async () => {
mocks.apiGet.mockResolvedValue({ uuid: "abc-123", name: "Event" });
const result = await getAdminEvent("abc-123");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/events/abc-123");
expect(result).toEqual({ uuid: "abc-123", name: "Event" });
});
});
describe("saveAdminEvent", () => {
it("POSTs when no uuid given", async () => {
mocks.apiPost.mockResolvedValue({ uuid: "new-1", name: "New Event" });
const payload: AdminEventPayload = {
name: "New Event",
location: "Place",
startDate: "2025-01-01",
endDate: "2025-01-02",
enabled: true,
salespersonIds: [],
};
const result = await saveAdminEvent(payload);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/events", payload);
expect(result).toEqual({ uuid: "new-1", name: "New Event" });
});
it("PUTs to /api/events/{uuid} when uuid is present", async () => {
mocks.apiPut.mockResolvedValue({ uuid: "existing", name: "Updated" });
const payload: AdminEventPayload = {
name: "Updated",
location: "Place",
startDate: "2025-01-01",
endDate: "2025-01-02",
enabled: true,
salespersonIds: [],
};
const result = await saveAdminEvent(payload, "existing");
expect(mocks.apiPut).toHaveBeenCalledWith("/api/events/existing", payload);
expect(result).toEqual({ uuid: "existing", name: "Updated" });
});
});
describe("listSalespersonOptions", () => {
it("calls GET /api/events/salesperson-options", async () => {
mocks.apiGet.mockResolvedValue([{ id: 1, name: "Jane", username: "jane.doe" }]);
const result = await listSalespersonOptions();
expect(mocks.apiGet).toHaveBeenCalledWith("/api/events/salesperson-options");
expect(result).toEqual([{ id: 1, name: "Jane", username: "jane.doe" }]);
});
});