// @vitest-environment jsdom import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { getRegistration, submitRegistration } from "./publicRegistration"; import { ApiError } from "@/app/lib/clientApi"; const body = { name: "A", mobile: "", email: "a@b.co", companyName: "", jobTitle: "", interestIds: [], consent: true, website: "" }; describe("publicRegistration service", () => { const originalLocation = window.location; beforeEach(() => { Object.defineProperty(window, "location", { value: { ...window.location, assign: vi.fn() }, writable: true, configurable: true }); }); afterEach(() => { Object.defineProperty(window, "location", { value: originalLocation, writable: true, configurable: true }); vi.unstubAllGlobals(); }); it("never sends a visitor to the admin login on a 401", async () => { vi.stubGlobal("fetch", vi.fn().mockImplementation(async () => new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }))); await expect(getRegistration("t")).rejects.toMatchObject({ status: 401 }); await expect(submitRegistration("t", body)).rejects.toBeInstanceOf(ApiError); expect(window.location.assign).not.toHaveBeenCalled(); }); it("posts JSON and returns the parsed body", async () => { const f = vi.fn().mockResolvedValue(new Response(JSON.stringify({ eventName: "Expo" }), { status: 200 })); vi.stubGlobal("fetch", f); expect(await submitRegistration("a/b", body)).toEqual({ eventName: "Expo" }); expect(f.mock.calls[0][0]).toBe("/api/public/registration/a%2Fb"); expect(f.mock.calls[0][1].method).toBe("POST"); }); });