forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

terms.test.ts100 lines, 3.1 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 { postTermArchive } from "../../terms";
8import { postTermUnarchive } from "../../terms";
9import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
10describe("terms API", () => {
11 afterEach(() => {
12 vi.restoreAllMocks();
13 });
14
15 it("getTerms calls /terms with pagination", async () => {
16 const mockPage = { items: [{ id: "t1" }], total: 1 };
17 mockFetchSuccess(mockPage);
18
19 const result = await getTerms({ limit: 10 });
20 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
21
22 const [url, init] = lastFetchCall();
23 expect(url).toBe("/api/terms?limit=10");
24 expect(init?.method).toBe("GET");
25 });
26
27 it("postTerm calls /terms with data", async () => {
28 const mockTerm = { id: "t1" };
29 mockFetchSuccess(mockTerm);
30
31 const data = { name: "Term 1", starts_at: 1000, ends_at: 2000 };
32 const result = await postTerm(data);
33 expect(result).toEqual(mockTerm);
34
35 const [url, init] = lastFetchCall();
36 expect(url).toBe("/api/terms");
37 expect(init?.method).toBe("POST");
38 expect(init?.body).toBe(JSON.stringify(data));
39 });
40
41 it("getTermById calls /terms/:id", async () => {
42 const mockTerm = { id: "t1", name: "Term 1" };
43 mockFetchSuccess(mockTerm);
44
45 const result = await getTermById("t1");
46 expect(result).toEqual(mockTerm);
47
48 const [url, init] = lastFetchCall();
49 expect(url).toBe("/api/terms/t1");
50 expect(init?.method).toBe("GET");
51 });
52
53 it("patchTermById calls /terms/:id with updates", async () => {
54 const mockTerm = { id: "t1", name: "Updated" };
55 mockFetchSuccess(mockTerm);
56
57 const updates = { name: "Updated" };
58 const result = await patchTermById("t1", updates);
59 expect(result).toEqual(mockTerm);
60
61 const [url, init] = lastFetchCall();
62 expect(url).toBe("/api/terms/t1");
63 expect(init?.method).toBe("PATCH");
64 expect(init?.body).toBe(JSON.stringify(updates));
65 });
66
67 it("deleteTermById calls /terms/:id", async () => {
68 mockFetch204();
69
70 await deleteTermById("t1");
71
72 const [url, init] = lastFetchCall();
73 expect(url).toBe("/api/terms/t1");
74 expect(init?.method).toBe("DELETE");
75 });
76
77 it("postTermArchive calls /terms/:id/archive", async () => {
78 const mockTerm = { id: "t1", archived_at: 1756896000000 };
79 mockFetchSuccess(mockTerm);
80
81 const result = await postTermArchive("t1");
82 expect(result).toEqual(mockTerm);
83
84 const [url, init] = lastFetchCall();
85 expect(url).toBe("/api/terms/t1/archive");
86 expect(init?.method).toBe("POST");
87 });
88
89 it("postTermUnarchive calls /terms/:id/unarchive", async () => {
90 const mockTerm = { id: "t1", archived_at: null };
91 mockFetchSuccess(mockTerm);
92
93 const result = await postTermUnarchive("t1");
94 expect(result).toEqual(mockTerm);
95
96 const [url, init] = lastFetchCall();
97 expect(url).toBe("/api/terms/t1/unarchive");
98 expect(init?.method).toBe("POST");
99 });
100});