hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { |
| 3 | deleteHomeworkById, |
| 4 | deleteHomeworkResultByUserId, |
| 5 | deleteHomeworkSubmission, |
| 6 | deleteHomeworkSubmissionFile, |
| 7 | getHomework, |
| 8 | getHomeworkById, |
| 9 | getHomeworkReport, |
| 10 | getHomeworkResult, |
| 11 | getHomeworkSubmission, |
| 12 | getHomeworkSubmissionFileBlob, |
| 13 | getHomeworkSubmissionFileUrl, |
| 14 | getHomeworkSubmissions, |
| 15 | patchHomeworkById, |
| 16 | postHomeworkResult, |
| 17 | postHomeworkSubmission, |
| 18 | postHomeworkSubmissionFile, |
| 19 | } from "../../homework"; |
| 20 | import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 21 | |
| 22 | describe("homework API", () => { |
| 23 | afterEach(() => { |
| 24 | vi.restoreAllMocks(); |
| 25 | }); |
| 26 | |
| 27 | it("getHomework calls /homework with pagination", async () => { |
| 28 | const mockPage = { items: [{ id: "hw1" }], total: 1 }; |
| 29 | mockFetchSuccess(mockPage); |
| 30 | |
| 31 | const result = await getHomework({ limit: 10, offset: 5 }); |
| 32 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 33 | |
| 34 | const [url, init] = lastFetchCall(); |
| 35 | expect(url).toBe("/api/homework?limit=10&offset=5"); |
| 36 | expect(init?.method).toBe("GET"); |
| 37 | }); |
| 38 | |
| 39 | it("getHomework sends pagination only — the endpoint has no due-date window", async () => { |
| 40 | mockFetchSuccess({ items: [], total: 0 }); |
| 41 | await getHomework({ limit: 1 }); |
| 42 | expect(lastFetchCall()[0]).toBe("/api/homework?limit=1"); |
| 43 | }); |
| 44 | |
| 45 | it("getHomeworkById calls /homework/:id", async () => { |
| 46 | const mockHomework = { id: "hw1" }; |
| 47 | mockFetchSuccess(mockHomework); |
| 48 | |
| 49 | const result = await getHomeworkById("hw1"); |
| 50 | expect(result).toEqual(mockHomework); |
| 51 | |
| 52 | const [url, init] = lastFetchCall(); |
| 53 | expect(url).toBe("/api/homework/hw1"); |
| 54 | expect(init?.method).toBe("GET"); |
| 55 | }); |
| 56 | |
| 57 | it("patchHomeworkById calls /homework/:id", async () => { |
| 58 | const mockHomework = { id: "hw1", title: "New" }; |
| 59 | mockFetchSuccess(mockHomework); |
| 60 | |
| 61 | const body = { title: "New", assigned: null }; |
| 62 | const result = await patchHomeworkById("hw1", body); |
| 63 | expect(result).toEqual(mockHomework); |
| 64 | |
| 65 | const [url, init] = lastFetchCall(); |
| 66 | expect(url).toBe("/api/homework/hw1"); |
| 67 | expect(init?.method).toBe("PATCH"); |
| 68 | expect(init?.body).toBe(JSON.stringify(body)); |
| 69 | }); |
| 70 | |
| 71 | it("deleteHomeworkById calls /homework/:id", async () => { |
| 72 | mockFetch204(); |
| 73 | |
| 74 | await deleteHomeworkById("hw1"); |
| 75 | |
| 76 | const [url, init] = lastFetchCall(); |
| 77 | expect(url).toBe("/api/homework/hw1"); |
| 78 | expect(init?.method).toBe("DELETE"); |
| 79 | }); |
| 80 | |
| 81 | it("postHomeworkSubmission calls /homework/:id/submission", async () => { |
| 82 | const mockSubmission = { homework: "hw1", text: "Done" }; |
| 83 | mockFetchSuccess(mockSubmission); |
| 84 | |
| 85 | const body = { text: "Done" }; |
| 86 | const result = await postHomeworkSubmission("hw1", body); |
| 87 | expect(result).toEqual(mockSubmission); |
| 88 | |
| 89 | const [url, init] = lastFetchCall(); |
| 90 | expect(url).toBe("/api/homework/hw1/submission"); |
| 91 | expect(init?.method).toBe("POST"); |
| 92 | expect(init?.body).toBe(JSON.stringify(body)); |
| 93 | }); |
| 94 | |
| 95 | it("getHomeworkSubmission calls /homework/:id/submission", async () => { |
| 96 | const mockSubmission = { homework: "hw1" }; |
| 97 | mockFetchSuccess(mockSubmission); |
| 98 | |
| 99 | const result = await getHomeworkSubmission("hw1"); |
| 100 | expect(result).toEqual(mockSubmission); |
| 101 | |
| 102 | const [url, init] = lastFetchCall(); |
| 103 | expect(url).toBe("/api/homework/hw1/submission"); |
| 104 | expect(init?.method).toBe("GET"); |
| 105 | }); |
| 106 | |
| 107 | it("deleteHomeworkSubmission calls /homework/:id/submission", async () => { |
| 108 | mockFetch204(); |
| 109 | |
| 110 | await deleteHomeworkSubmission("hw1"); |
| 111 | |
| 112 | const [url, init] = lastFetchCall(); |
| 113 | expect(url).toBe("/api/homework/hw1/submission"); |
| 114 | expect(init?.method).toBe("DELETE"); |
| 115 | }); |
| 116 | |
| 117 | it("postHomeworkSubmissionFile calls /homework/:id/submission/files with FormData", async () => { |
| 118 | const mockFile = { id: "f1", name: "a.txt" }; |
| 119 | mockFetchSuccess(mockFile, 201); |
| 120 | |
| 121 | const file = new File(["x"], "a.txt", { type: "text/plain" }); |
| 122 | const result = await postHomeworkSubmissionFile("hw1", file); |
| 123 | expect(result).toEqual(mockFile); |
| 124 | |
| 125 | const [url, init] = lastFetchCall(); |
| 126 | expect(url).toBe("/api/homework/hw1/submission/files"); |
| 127 | expect(init?.method).toBe("POST"); |
| 128 | expect(init?.body).toBeInstanceOf(FormData); |
| 129 | }); |
| 130 | |
| 131 | it("getHomeworkSubmissionFileBlob calls /homework/:id/submission/files/:fid", async () => { |
| 132 | const blob = new Blob(["x"], { type: "text/plain" }); |
| 133 | mockFetchBlob(blob); |
| 134 | |
| 135 | const result = await getHomeworkSubmissionFileBlob("hw1", "f1"); |
| 136 | expect(result.type).toBe("text/plain"); |
| 137 | |
| 138 | const [url, init] = lastFetchCall(); |
| 139 | expect(url).toBe("/api/homework/hw1/submission/files/f1"); |
| 140 | expect(init?.credentials).toBe("same-origin"); |
| 141 | }); |
| 142 | |
| 143 | it("getHomeworkSubmissionFileUrl returns encoded URL", () => { |
| 144 | expect(getHomeworkSubmissionFileUrl("hw/1", "f?1")).toBe("/api/homework/hw%2F1/submission/files/f%3F1"); |
| 145 | }); |
| 146 | |
| 147 | it("deleteHomeworkSubmissionFile calls /homework/:id/submission/files/:fid", async () => { |
| 148 | mockFetch204(); |
| 149 | |
| 150 | await deleteHomeworkSubmissionFile("hw1", "f1"); |
| 151 | |
| 152 | const [url, init] = lastFetchCall(); |
| 153 | expect(url).toBe("/api/homework/hw1/submission/files/f1"); |
| 154 | expect(init?.method).toBe("DELETE"); |
| 155 | }); |
| 156 | |
| 157 | it("postHomeworkResult calls /homework/:id/results", async () => { |
| 158 | const mockResult = { status: "done", mark: 95 }; |
| 159 | mockFetchSuccess(mockResult); |
| 160 | |
| 161 | const body = { user: "u1", status: "done", mark: 95 }; |
| 162 | const result = await postHomeworkResult("hw1", body); |
| 163 | expect(result).toEqual(mockResult); |
| 164 | |
| 165 | const [url, init] = lastFetchCall(); |
| 166 | expect(url).toBe("/api/homework/hw1/results"); |
| 167 | expect(init?.method).toBe("POST"); |
| 168 | expect(init?.body).toBe(JSON.stringify(body)); |
| 169 | }); |
| 170 | |
| 171 | it("deleteHomeworkResultByUserId calls /homework/:id/results/:user", async () => { |
| 172 | mockFetch204(); |
| 173 | |
| 174 | await deleteHomeworkResultByUserId("hw1", "u1"); |
| 175 | |
| 176 | const [url, init] = lastFetchCall(); |
| 177 | expect(url).toBe("/api/homework/hw1/results/u1"); |
| 178 | expect(init?.method).toBe("DELETE"); |
| 179 | }); |
| 180 | |
| 181 | it("getHomeworkResult calls /homework/:id/result", async () => { |
| 182 | const mockResult = { status: "done" }; |
| 183 | mockFetchSuccess(mockResult); |
| 184 | |
| 185 | const result = await getHomeworkResult("hw1"); |
| 186 | expect(result).toEqual(mockResult); |
| 187 | |
| 188 | const [url, init] = lastFetchCall(); |
| 189 | expect(url).toBe("/api/homework/hw1/result"); |
| 190 | expect(init?.method).toBe("GET"); |
| 191 | }); |
| 192 | |
| 193 | it("getHomeworkSubmissions calls /homework/:id/submissions", async () => { |
| 194 | const mockPage = { items: [{ user: "u1" }], total: 1 }; |
| 195 | mockFetchSuccess(mockPage); |
| 196 | |
| 197 | const result = await getHomeworkSubmissions("hw1", { limit: 20 }); |
| 198 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 199 | |
| 200 | const [url, init] = lastFetchCall(); |
| 201 | expect(url).toBe("/api/homework/hw1/submissions?limit=20"); |
| 202 | expect(init?.method).toBe("GET"); |
| 203 | }); |
| 204 | |
| 205 | it("getHomeworkReport calls /homework/report/:user", async () => { |
| 206 | const mockPage = { items: [{ homework: "hw1" }], total: 1 }; |
| 207 | mockFetchSuccess(mockPage); |
| 208 | |
| 209 | const result = await getHomeworkReport("u1", { offset: 10 }); |
| 210 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 211 | |
| 212 | const [url, init] = lastFetchCall(); |
| 213 | expect(url).toBe("/api/homework/report/u1?offset=10"); |
| 214 | expect(init?.method).toBe("GET"); |
| 215 | }); |
| 216 | }); |