Files
co-lite/solvway-admin-portal/app/api/companies/route.test.ts
T

47 lines
1.8 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 { GET, POST } from "./route";
const post = (body: unknown) =>
new Request("http://localhost/api/companies", { method: "POST", body: JSON.stringify(body) }) as unknown as Parameters<typeof POST>[0];
beforeEach(() => {
mocks.callMobileApi.mockReset();
mocks.getToken.mockReset().mockResolvedValue("jwt");
});
describe("/api/companies", () => {
it("GET returns 401 without a session", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await GET()).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("GET forwards to /api/admin/companies", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: [] });
await GET();
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/companies", { token: "jwt" });
});
it("POST forwards id, name, code and status", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 1 } });
await POST(post({ id: 1, name: "Ins", code: "ins", status: "A", extra: "x" }));
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/companies", {
method: "POST",
body: { id: 1, name: "Ins", code: "ins", status: "A" },
token: "jwt",
});
});
it("POST relays a 409 from the API", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "Code already used" });
const res = await POST(post({ name: "Ins", code: "ins", status: "A" }));
expect(res.status).toBe(409);
expect((await res.json()).error).toBe("Code already used");
});
});