Files
openclaw-studio/tests/unit/fetchJson.test.ts
T
George Pickett af7554f9cd Add configuration and testing infrastructure for Clawdbot Agent UI
- Introduced components.json for UI configuration with Tailwind and icon library settings.
- Updated ESLint configuration to include Prettier for consistent code formatting.
- Added Playwright and Vitest configurations for end-to-end and unit testing.
- Implemented environment variable validation using Zod and added logging utilities.
- Refactored API routes to improve error handling and logging.
- Enhanced global styles and markdown rendering capabilities.
- Established a new directory structure for better organization of features and components.
2026-01-27 12:41:25 -08: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 });
});
});