sentiment-scope / frontend /src /api.test.ts
melkholy's picture
feat: single-image Hugging Face Spaces deployment with rate limiting
c1fa0b4 verified
Raw
History Blame Contribute Delete
796 Bytes
import { afterEach, describe, expect, it, vi } from "vitest";
import { analyze } from "./api";
describe("api client", () => {
afterEach(() => vi.restoreAllMocks());
it("returns parsed JSON on success", async () => {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(
new Response(JSON.stringify({ label: "positive", scores: { negative: 0.1, neutral: 0.1, positive: 0.8 } })),
));
const result = await analyze("great stuff");
expect(result.label).toBe("positive");
});
it("throws the server's detail message on error", async () => {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(
new Response(JSON.stringify({ detail: "Model is not loaded" }), { status: 503 }),
));
await expect(analyze("hi")).rejects.toThrow("Model is not loaded");
});
});