forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

terms.test.ts75 lines, 2.3 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getTerms } from "../../terms";
3import { getTermById } from "../../terms";
4import { postTerm } from "../../terms";
5import { patchTermById } from "../../terms";
6import { deleteTermById } from "../../terms";
7import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
8
9describe("terms API", () => {
10 afterEach(() => {
11 vi.restoreAllMocks();
12 });
13
14 it("getTerms calls /terms with pagination", async () => {
15 const mockPage = { items: [{ id: "t1" }], total: 1 };
16 mockFetchSuccess(mockPage);
17
18 const result = await getTerms({ limit: 10 });
19 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
20
21 const [url, init] = lastFetchCall();
22 expect(url).toBe("/api/terms?limit=10");
23 expect(init?.method).toBe("GET");
24 });
25
26 it("postTerm calls /terms with data", async () => {
27 const mockTerm = { id: "t1" };
28 mockFetchSuccess(mockTerm);
29
30 const data = { name: "Term 1", starts_at: 1000, ends_at: 2000 };
31 const result = await postTerm(data);
32 expect(result).toEqual(mockTerm);
33
34 const [url, init] = lastFetchCall();
35 expect(url).toBe("/api/terms");
36 expect(init?.method).toBe("POST");
37 expect(init?.body).toBe(JSON.stringify(data));
38 });
39
40 it("getTermById calls /terms/:id", async () => {
41 const mockTerm = { id: "t1", name: "Term 1" };
42 mockFetchSuccess(mockTerm);
43
44 const result = await getTermById("t1");
45 expect(result).toEqual(mockTerm);
46
47 const [url, init] = lastFetchCall();
48 expect(url).toBe("/api/terms/t1");
49 expect(init?.method).toBe("GET");
50 });
51
52 it("patchTermById calls /terms/:id with updates", async () => {
53 const mockTerm = { id: "t1", name: "Updated" };
54 mockFetchSuccess(mockTerm);
55
56 const updates = { name: "Updated" };
57 const result = await patchTermById("t1", updates);
58 expect(result).toEqual(mockTerm);
59
60 const [url, init] = lastFetchCall();
61 expect(url).toBe("/api/terms/t1");
62 expect(init?.method).toBe("PATCH");
63 expect(init?.body).toBe(JSON.stringify(updates));
64 });
65
66 it("deleteTermById calls /terms/:id", async () => {
67 mockFetch204();
68
69 await deleteTermById("t1");
70
71 const [url, init] = lastFetchCall();
72 expect(url).toBe("/api/terms/t1");
73 expect(init?.method).toBe("DELETE");
74 });
75});