hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { |
| 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"; |
| 15 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 16 | |
| 17 | describe("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("getBoards sends q with pagination when given", async () => { |
| 45 | mockFetchSuccess({ items: [], total: 0 }); |
| 46 | await getBoards({ limit: 10, offset: 20, q: "math, physics" }); |
| 47 | |
| 48 | const [url] = lastFetchCall(); |
| 49 | expect(url).toBe("/api/boards?q=math%2C+physics&limit=10&offset=20"); |
| 50 | }); |
| 51 | |
| 52 | it("getBoards omits a blank q", async () => { |
| 53 | mockFetchSuccess({ items: [], total: 0 }); |
| 54 | await getBoards({ q: " " }); |
| 55 | |
| 56 | const [url] = lastFetchCall(); |
| 57 | expect(url).toBe("/api/boards"); |
| 58 | }); |
| 59 | |
| 60 | it("getBoardById calls GET /boards/:id", async () => { |
| 61 | mockFetchSuccess({ id: "b1" }); |
| 62 | await getBoardById("b1"); |
| 63 | |
| 64 | const [url, init] = lastFetchCall(); |
| 65 | expect(url).toBe("/api/boards/b1"); |
| 66 | expect(init?.method).toBe("GET"); |
| 67 | }); |
| 68 | |
| 69 | it("getBoardStrokes calls GET /boards/:id/strokes", async () => { |
| 70 | mockFetchSuccess({ items: [], total: 0 }); |
| 71 | await getBoardStrokes("b1", { limit: 200 }); |
| 72 | |
| 73 | const [url, init] = lastFetchCall(); |
| 74 | expect(url).toBe("/api/boards/b1/strokes?limit=200"); |
| 75 | expect(init?.method).toBe("GET"); |
| 76 | }); |
| 77 | |
| 78 | it("getBoardHistory calls GET /boards/:id/history with epoch + pagination", async () => { |
| 79 | mockFetchSuccess({ items: [], total: 0 }); |
| 80 | await getBoardHistory("b1", { epoch: 3, limit: 50 }); |
| 81 | |
| 82 | const [url, init] = lastFetchCall(); |
| 83 | expect(url).toBe("/api/boards/b1/history?epoch=3&limit=50"); |
| 84 | expect(init?.method).toBe("GET"); |
| 85 | }); |
| 86 | |
| 87 | it("getBoardHistory omits epoch when not given", async () => { |
| 88 | mockFetchSuccess({ items: [], total: 0 }); |
| 89 | await getBoardHistory("b1"); |
| 90 | |
| 91 | const [url] = lastFetchCall(); |
| 92 | expect(url).toBe("/api/boards/b1/history"); |
| 93 | }); |
| 94 | |
| 95 | it("getBoardEpochs calls GET /boards/:id/epochs", async () => { |
| 96 | mockFetchSuccess({ items: [], total: 0 }); |
| 97 | await getBoardEpochs("b1"); |
| 98 | |
| 99 | const [url, init] = lastFetchCall(); |
| 100 | expect(url).toBe("/api/boards/b1/epochs"); |
| 101 | expect(init?.method).toBe("GET"); |
| 102 | }); |
| 103 | |
| 104 | it("patchBoardById calls PATCH /boards/:id with the body", async () => { |
| 105 | mockFetchSuccess({ id: "b1" }); |
| 106 | const body = { locked: true }; |
| 107 | await patchBoardById("b1", body); |
| 108 | |
| 109 | const [url, init] = lastFetchCall(); |
| 110 | expect(url).toBe("/api/boards/b1"); |
| 111 | expect(init?.method).toBe("PATCH"); |
| 112 | expect(init?.body).toBe(JSON.stringify(body)); |
| 113 | }); |
| 114 | |
| 115 | it("postBoardClear calls POST /boards/:id/clear", async () => { |
| 116 | mockFetchSuccess({ id: "s1", kind: "clear" }); |
| 117 | await postBoardClear("b1"); |
| 118 | |
| 119 | const [url, init] = lastFetchCall(); |
| 120 | expect(url).toBe("/api/boards/b1/clear"); |
| 121 | expect(init?.method).toBe("POST"); |
| 122 | }); |
| 123 | |
| 124 | it("postBoardClose calls POST /boards/:id/close", async () => { |
| 125 | mockFetchSuccess({ id: "b1", closed_at: 123 }); |
| 126 | await postBoardClose("b1"); |
| 127 | |
| 128 | const [url, init] = lastFetchCall(); |
| 129 | expect(url).toBe("/api/boards/b1/close"); |
| 130 | expect(init?.method).toBe("POST"); |
| 131 | }); |
| 132 | |
| 133 | it("deleteBoardById calls DELETE /boards/:id", async () => { |
| 134 | mockFetch204(); |
| 135 | await deleteBoardById("b1"); |
| 136 | |
| 137 | const [url, init] = lastFetchCall(); |
| 138 | expect(url).toBe("/api/boards/b1"); |
| 139 | expect(init?.method).toBe("DELETE"); |
| 140 | }); |
| 141 | |
| 142 | it("postBoardInvite POSTs a class invite to /boards/:id/invite", async () => { |
| 143 | mockFetchSuccess({ id: "b1", participants: ["u1", "u2"] }); |
| 144 | const body = { kind: "class", class: "cl1" } as const; |
| 145 | await postBoardInvite("b1", body); |
| 146 | |
| 147 | const [url, init] = lastFetchCall(); |
| 148 | expect(url).toBe("/api/boards/b1/invite"); |
| 149 | expect(init?.method).toBe("POST"); |
| 150 | expect(init?.body).toBe(JSON.stringify(body)); |
| 151 | }); |
| 152 | |
| 153 | it("postBoardInvite POSTs a course invite", async () => { |
| 154 | mockFetchSuccess({ id: "b1", participants: [] }); |
| 155 | const body = { kind: "course", course: "co1" } as const; |
| 156 | await postBoardInvite("b1", body); |
| 157 | expect(lastFetchCall()[1]?.body).toBe(JSON.stringify(body)); |
| 158 | }); |
| 159 | |
| 160 | it("postBoardInvite POSTs an event invite", async () => { |
| 161 | mockFetchSuccess({ id: "b1", participants: [] }); |
| 162 | const body = { kind: "event", event: "e1" } as const; |
| 163 | await postBoardInvite("b1", body); |
| 164 | expect(lastFetchCall()[1]?.body).toBe(JSON.stringify(body)); |
| 165 | }); |
| 166 | }); |