forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

homework.test.ts224 lines, 7.5 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
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";
20import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch";
21
22describe("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 the due window and class_course when set", async () => {
40 mockFetchSuccess({ items: [], total: 0 });
41 await getHomework({ due_after: 1700000000000, due_before: 1800000000000, class_course: "i1", limit: 50 });
42 expect(lastFetchCall()[0]).toBe("/api/homework?limit=50&due_after=1700000000000&due_before=1800000000000&class_course=i1");
43 });
44
45 it("getHomework omits unset filters — no bare keys", async () => {
46 mockFetchSuccess({ items: [], total: 0 });
47 await getHomework({ limit: 1 });
48 expect(lastFetchCall()[0]).toBe("/api/homework?limit=1");
49 await getHomework({ class_course: " " });
50 expect(lastFetchCall()[0]).toBe("/api/homework");
51 });
52
53 it("getHomeworkById calls /homework/:id", async () => {
54 const mockHomework = { id: "hw1" };
55 mockFetchSuccess(mockHomework);
56
57 const result = await getHomeworkById("hw1");
58 expect(result).toEqual(mockHomework);
59
60 const [url, init] = lastFetchCall();
61 expect(url).toBe("/api/homework/hw1");
62 expect(init?.method).toBe("GET");
63 });
64
65 it("patchHomeworkById calls /homework/:id", async () => {
66 const mockHomework = { id: "hw1", title: "New" };
67 mockFetchSuccess(mockHomework);
68
69 const body = { title: "New", assigned: null };
70 const result = await patchHomeworkById("hw1", body);
71 expect(result).toEqual(mockHomework);
72
73 const [url, init] = lastFetchCall();
74 expect(url).toBe("/api/homework/hw1");
75 expect(init?.method).toBe("PATCH");
76 expect(init?.body).toBe(JSON.stringify(body));
77 });
78
79 it("deleteHomeworkById calls /homework/:id", async () => {
80 mockFetch204();
81
82 await deleteHomeworkById("hw1");
83
84 const [url, init] = lastFetchCall();
85 expect(url).toBe("/api/homework/hw1");
86 expect(init?.method).toBe("DELETE");
87 });
88
89 it("postHomeworkSubmission calls /homework/:id/submission", async () => {
90 const mockSubmission = { homework: "hw1", text: "Done" };
91 mockFetchSuccess(mockSubmission);
92
93 const body = { text: "Done" };
94 const result = await postHomeworkSubmission("hw1", body);
95 expect(result).toEqual(mockSubmission);
96
97 const [url, init] = lastFetchCall();
98 expect(url).toBe("/api/homework/hw1/submission");
99 expect(init?.method).toBe("POST");
100 expect(init?.body).toBe(JSON.stringify(body));
101 });
102
103 it("getHomeworkSubmission calls /homework/:id/submission", async () => {
104 const mockSubmission = { homework: "hw1" };
105 mockFetchSuccess(mockSubmission);
106
107 const result = await getHomeworkSubmission("hw1");
108 expect(result).toEqual(mockSubmission);
109
110 const [url, init] = lastFetchCall();
111 expect(url).toBe("/api/homework/hw1/submission");
112 expect(init?.method).toBe("GET");
113 });
114
115 it("deleteHomeworkSubmission calls /homework/:id/submission", async () => {
116 mockFetch204();
117
118 await deleteHomeworkSubmission("hw1");
119
120 const [url, init] = lastFetchCall();
121 expect(url).toBe("/api/homework/hw1/submission");
122 expect(init?.method).toBe("DELETE");
123 });
124
125 it("postHomeworkSubmissionFile calls /homework/:id/submission/files with FormData", async () => {
126 const mockFile = { id: "f1", name: "a.txt" };
127 mockFetchSuccess(mockFile, 201);
128
129 const file = new File(["x"], "a.txt", { type: "text/plain" });
130 const result = await postHomeworkSubmissionFile("hw1", file);
131 expect(result).toEqual(mockFile);
132
133 const [url, init] = lastFetchCall();
134 expect(url).toBe("/api/homework/hw1/submission/files");
135 expect(init?.method).toBe("POST");
136 expect(init?.body).toBeInstanceOf(FormData);
137 });
138
139 it("getHomeworkSubmissionFileBlob calls /homework/:id/submission/files/:fid", async () => {
140 const blob = new Blob(["x"], { type: "text/plain" });
141 mockFetchBlob(blob);
142
143 const result = await getHomeworkSubmissionFileBlob("hw1", "f1");
144 expect(result.type).toBe("text/plain");
145
146 const [url, init] = lastFetchCall();
147 expect(url).toBe("/api/homework/hw1/submission/files/f1");
148 expect(init?.credentials).toBe("same-origin");
149 });
150
151 it("getHomeworkSubmissionFileUrl returns encoded URL", () => {
152 expect(getHomeworkSubmissionFileUrl("hw/1", "f?1")).toBe("/api/homework/hw%2F1/submission/files/f%3F1");
153 });
154
155 it("deleteHomeworkSubmissionFile calls /homework/:id/submission/files/:fid", async () => {
156 mockFetch204();
157
158 await deleteHomeworkSubmissionFile("hw1", "f1");
159
160 const [url, init] = lastFetchCall();
161 expect(url).toBe("/api/homework/hw1/submission/files/f1");
162 expect(init?.method).toBe("DELETE");
163 });
164
165 it("postHomeworkResult calls /homework/:id/results", async () => {
166 const mockResult = { status: "done", mark: 95 };
167 mockFetchSuccess(mockResult);
168
169 const body = { user: "u1", status: "done", mark: 95 };
170 const result = await postHomeworkResult("hw1", body);
171 expect(result).toEqual(mockResult);
172
173 const [url, init] = lastFetchCall();
174 expect(url).toBe("/api/homework/hw1/results");
175 expect(init?.method).toBe("POST");
176 expect(init?.body).toBe(JSON.stringify(body));
177 });
178
179 it("deleteHomeworkResultByUserId calls /homework/:id/results/:user", async () => {
180 mockFetch204();
181
182 await deleteHomeworkResultByUserId("hw1", "u1");
183
184 const [url, init] = lastFetchCall();
185 expect(url).toBe("/api/homework/hw1/results/u1");
186 expect(init?.method).toBe("DELETE");
187 });
188
189 it("getHomeworkResult calls /homework/:id/result", async () => {
190 const mockResult = { status: "done" };
191 mockFetchSuccess(mockResult);
192
193 const result = await getHomeworkResult("hw1");
194 expect(result).toEqual(mockResult);
195
196 const [url, init] = lastFetchCall();
197 expect(url).toBe("/api/homework/hw1/result");
198 expect(init?.method).toBe("GET");
199 });
200
201 it("getHomeworkSubmissions calls /homework/:id/submissions", async () => {
202 const mockPage = { items: [{ user: "u1" }], total: 1 };
203 mockFetchSuccess(mockPage);
204
205 const result = await getHomeworkSubmissions("hw1", { limit: 20 });
206 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
207
208 const [url, init] = lastFetchCall();
209 expect(url).toBe("/api/homework/hw1/submissions?limit=20");
210 expect(init?.method).toBe("GET");
211 });
212
213 it("getHomeworkReport calls /homework/report/:user", async () => {
214 const mockPage = { items: [{ homework: "hw1" }], total: 1 };
215 mockFetchSuccess(mockPage);
216
217 const result = await getHomeworkReport("u1", { offset: 10 });
218 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
219
220 const [url, init] = lastFetchCall();
221 expect(url).toBe("/api/homework/report/u1?offset=10");
222 expect(init?.method).toBe("GET");
223 });
224});