Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import { POST } from "./route";
|
|
|
|
const call = (body: unknown) =>
|
|
POST(new Request("http://localhost/x", { method: "POST", body: JSON.stringify(body) }) as never, { params: Promise.resolve({ id: "7" }) });
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset().mockResolvedValue("jwt");
|
|
});
|
|
|
|
describe("POST /api/companies/[id]/code", () => {
|
|
it("returns 401 without a session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
expect((await call({ code: "new" })).status).toBe(401);
|
|
});
|
|
|
|
it("forwards the code to /api/admin/companies/{id}/code", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 7, code: "new" } });
|
|
await call({ code: "new" });
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/companies/7/code", {
|
|
method: "POST",
|
|
body: { code: "new" },
|
|
token: "jwt",
|
|
});
|
|
});
|
|
});
|