Files
cyclone/src/lib/api.test.ts
T

74 lines
2.2 KiB
TypeScript

import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { api } from "./api";
const originalFetch = global.fetch;
const mockFetch = vi.fn();
describe("api GET helpers", () => {
beforeEach(() => {
global.fetch = mockFetch as unknown as typeof fetch;
mockFetch.mockReset();
});
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
});
it("listBatches hits /api/batches and unwraps body.items", async () => {
mockFetch.mockResolvedValueOnce(
new Response(JSON.stringify({ items: [] }), {
status: 200,
headers: { "content-type": "application/json" },
})
);
const out = await api.listBatches();
expect(out).toEqual([]);
expect(mockFetch).toHaveBeenCalledTimes(1);
const called = mockFetch.mock.calls[0][0] as string;
expect(called).toContain("/api/batches");
expect(called).not.toContain("?");
});
it("listClaims builds the right URL with status + sort + order", async () => {
mockFetch.mockResolvedValueOnce(
new Response(
JSON.stringify({ items: [], total: 0, returned: 0, has_more: false }),
{ status: 200, headers: { "content-type": "application/json" } }
)
);
await api.listClaims({
status: "submitted",
sort: "billedAmount",
order: "desc",
});
const called = mockFetch.mock.calls[0][0] as string;
expect(called).toContain("/api/claims?");
expect(called).toContain("status=submitted");
expect(called).toContain("sort=billedAmount");
expect(called).toContain("order=desc");
});
it("getBatch returns the result with the right id in the URL", async () => {
mockFetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
envelope: {
sender_id: "X",
receiver_id: "Y",
control_number: "1",
transaction_date: "2026-01-01",
},
claims: [],
summary: {},
kind: "835",
}),
{ status: 200, headers: { "content-type": "application/json" } }
)
);
const out = await api.getBatch("abc");
expect(out).toBeTruthy();
const called = mockFetch.mock.calls[0][0] as string;
expect(called).toContain("/api/batches/abc");
});
});