Files
claw3d/tests/unit/fetchJson.test.ts
T
Luke The Dev 4fa4f13558 First Release of Claw3D (#11)
Co-authored-by: iamlukethedev <iamlukethedev@users.noreply.github.com>
2026-03-19 23:14:04 -05:00

40 lines
1.0 KiB
TypeScript

import { describe, expect, it, vi, afterEach } from "vitest";
import { fetchJson } from "@/lib/http";
type MockResponse = {
ok: boolean;
status: number;
text: () => Promise<string>;
};
const createResponse = (body: string, ok: boolean, status: number): MockResponse => ({
ok,
status,
text: vi.fn().mockResolvedValue(body),
});
describe("fetchJson", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("throws when response is not ok", async () => {
const fetchMock = vi.fn().mockResolvedValue(
createResponse(JSON.stringify({ error: "Nope" }), false, 400)
);
globalThis.fetch = fetchMock as unknown as typeof fetch;
await expect(fetchJson("/api/test")).rejects.toThrow("Nope");
});
it("returns parsed JSON for ok responses", async () => {
const fetchMock = vi.fn().mockResolvedValue(
createResponse(JSON.stringify({ ok: true }), true, 200)
);
globalThis.fetch = fetchMock as unknown as typeof fetch;
await expect(fetchJson("/api/test")).resolves.toEqual({ ok: true });
});
});