forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

boards.test.ts150 lines, 4.8 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 deleteBoardById,
4 getBoardById,
5 getBoardEpochs,
6 getBoardHistory,
7 getBoardStrokes,
8 getBoards,
9 patchBoardById,
10 postBoard,
11 postBoardClear,
12 postBoardClose,
13 postBoardInvite,
14} from "../../boards";
15import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
16
17describe("boards API", () => {
18 afterEach(() => {
19 vi.restoreAllMocks();
20 });
21
22 it("postBoard calls POST /boards with the body", async () => {
23 mockFetchSuccess({ id: "b1", title: "Math" });
24 const body = { title: "Math", participants: ["u2", "u3"] };
25 await postBoard(body);
26
27 const [url, init] = lastFetchCall();
28 expect(url).toBe("/api/boards");
29 expect(init?.method).toBe("POST");
30 expect(init?.body).toBe(JSON.stringify(body));
31 });
32
33 it("getBoards calls GET /boards with pagination", async () => {
34 const mockPage = { items: [{ id: "b1" }], total: 1 };
35 mockFetchSuccess(mockPage);
36 const result = await getBoards({ limit: 10, offset: 20 });
37 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
38
39 const [url, init] = lastFetchCall();
40 expect(url).toBe("/api/boards?limit=10&offset=20");
41 expect(init?.method).toBe("GET");
42 });
43
44 it("getBoardById calls GET /boards/:id", async () => {
45 mockFetchSuccess({ id: "b1" });
46 await getBoardById("b1");
47
48 const [url, init] = lastFetchCall();
49 expect(url).toBe("/api/boards/b1");
50 expect(init?.method).toBe("GET");
51 });
52
53 it("getBoardStrokes calls GET /boards/:id/strokes", async () => {
54 mockFetchSuccess({ items: [], total: 0 });
55 await getBoardStrokes("b1", { limit: 200 });
56
57 const [url, init] = lastFetchCall();
58 expect(url).toBe("/api/boards/b1/strokes?limit=200");
59 expect(init?.method).toBe("GET");
60 });
61
62 it("getBoardHistory calls GET /boards/:id/history with epoch + pagination", async () => {
63 mockFetchSuccess({ items: [], total: 0 });
64 await getBoardHistory("b1", { epoch: 3, limit: 50 });
65
66 const [url, init] = lastFetchCall();
67 expect(url).toBe("/api/boards/b1/history?epoch=3&limit=50");
68 expect(init?.method).toBe("GET");
69 });
70
71 it("getBoardHistory omits epoch when not given", async () => {
72 mockFetchSuccess({ items: [], total: 0 });
73 await getBoardHistory("b1");
74
75 const [url] = lastFetchCall();
76 expect(url).toBe("/api/boards/b1/history");
77 });
78
79 it("getBoardEpochs calls GET /boards/:id/epochs", async () => {
80 mockFetchSuccess({ items: [], total: 0 });
81 await getBoardEpochs("b1");
82
83 const [url, init] = lastFetchCall();
84 expect(url).toBe("/api/boards/b1/epochs");
85 expect(init?.method).toBe("GET");
86 });
87
88 it("patchBoardById calls PATCH /boards/:id with the body", async () => {
89 mockFetchSuccess({ id: "b1" });
90 const body = { locked: true };
91 await patchBoardById("b1", body);
92
93 const [url, init] = lastFetchCall();
94 expect(url).toBe("/api/boards/b1");
95 expect(init?.method).toBe("PATCH");
96 expect(init?.body).toBe(JSON.stringify(body));
97 });
98
99 it("postBoardClear calls POST /boards/:id/clear", async () => {
100 mockFetchSuccess({ id: "s1", kind: "clear" });
101 await postBoardClear("b1");
102
103 const [url, init] = lastFetchCall();
104 expect(url).toBe("/api/boards/b1/clear");
105 expect(init?.method).toBe("POST");
106 });
107
108 it("postBoardClose calls POST /boards/:id/close", async () => {
109 mockFetchSuccess({ id: "b1", closed_at: 123 });
110 await postBoardClose("b1");
111
112 const [url, init] = lastFetchCall();
113 expect(url).toBe("/api/boards/b1/close");
114 expect(init?.method).toBe("POST");
115 });
116
117 it("deleteBoardById calls DELETE /boards/:id", async () => {
118 mockFetch204();
119 await deleteBoardById("b1");
120
121 const [url, init] = lastFetchCall();
122 expect(url).toBe("/api/boards/b1");
123 expect(init?.method).toBe("DELETE");
124 });
125
126 it("postBoardInvite POSTs a class invite to /boards/:id/invite", async () => {
127 mockFetchSuccess({ id: "b1", participants: ["u1", "u2"] });
128 const body = { kind: "class", class: "cl1" } as const;
129 await postBoardInvite("b1", body);
130
131 const [url, init] = lastFetchCall();
132 expect(url).toBe("/api/boards/b1/invite");
133 expect(init?.method).toBe("POST");
134 expect(init?.body).toBe(JSON.stringify(body));
135 });
136
137 it("postBoardInvite POSTs a course invite", async () => {
138 mockFetchSuccess({ id: "b1", participants: [] });
139 const body = { kind: "course", course: "co1" } as const;
140 await postBoardInvite("b1", body);
141 expect(lastFetchCall()[1]?.body).toBe(JSON.stringify(body));
142 });
143
144 it("postBoardInvite POSTs an event invite", async () => {
145 mockFetchSuccess({ id: "b1", participants: [] });
146 const body = { kind: "event", event: "e1" } as const;
147 await postBoardInvite("b1", body);
148 expect(lastFetchCall()[1]?.body).toBe(JSON.stringify(body));
149 });
150});