forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

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