forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

exams.test.ts198 lines, 7.6 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getExams } from "../../exams";
3import { getExamById } from "../../exams";
4import { patchExamById } from "../../exams";
5import { deleteExamById } from "../../exams";
6import { getExamQuestionImageBlob } from "../../exams";
7import { getExamChoiceImageBlob } from "../../exams";
8import { postExamAttemptAnswerImage } from "../../exams";
9import { deleteExamAttemptAnswerImage } from "../../exams";
10import { getExamAnswerImageBlob } from "../../exams";
11import { getExamAudience } from "../../exams";
12import { postExamAudience } from "../../exams";
13import { deleteExamAudienceByInstanceId } from "../../exams";
14import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch";
15
16describe("exams API - core", () => {
17 afterEach(() => {
18 vi.restoreAllMocks();
19 });
20
21 it("getExams calls /exams with pagination", async () => {
22 const mockPage = { items: [{ id: "ex1" }], total: 1 };
23 mockFetchSuccess(mockPage);
24
25 const result = await getExams({ limit: 10 });
26 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
27
28 const [url, init] = lastFetchCall();
29 expect(url).toBe("/api/exams?limit=10");
30 expect(init?.method).toBe("GET");
31 });
32
33 it("getExams sends the schedule window and drops undefined params", async () => {
34 mockFetchSuccess({ items: [{ id: "ex1" }], total: 1 });
35
36 await getExams({ ends_after: 1_700_000_000_000, limit: 50 });
37 expect(lastFetchCall()[0]).toBe("/api/exams?limit=50&ends_after=1700000000000");
38
39 mockFetchSuccess({ items: [], total: 0 });
40 await getExams({ starts_after: 1_700_000_000_000 });
41 expect(lastFetchCall()[0]).toBe("/api/exams?starts_after=1700000000000");
42
43 // A blank value (`?ends_after=`) is a hard 400 server-side, so an
44 // undefined filter must leave the key out of the query string entirely.
45 mockFetchSuccess({ items: [], total: 0 });
46 await getExams({ limit: 10, ends_after: undefined, starts_after: undefined });
47 expect(lastFetchCall()[0]).toBe("/api/exams?limit=10");
48 expect(lastFetchCall()[0]).not.toContain("ends_after");
49 expect(lastFetchCall()[0]).not.toContain("starts_after");
50
51 // Upper bounds serialize after the earlier keys, in declaration order.
52 mockFetchSuccess({ items: [], total: 0 });
53 await getExams({ starts_after: 1, ends_after: 2, starts_before: 3, ends_before: 4 });
54 expect(lastFetchCall()[0]).toBe("/api/exams?starts_after=1&ends_after=2&starts_before=3&ends_before=4");
55
56 mockFetchSuccess({ items: [], total: 0 });
57 await getExams({ limit: 10, starts_before: undefined, ends_before: undefined });
58 expect(lastFetchCall()[0]).toBe("/api/exams?limit=10");
59 expect(lastFetchCall()[0]).not.toContain("starts_before");
60 expect(lastFetchCall()[0]).not.toContain("ends_before");
61 });
62
63 it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => {
64 mockFetchBlob(new Blob(["img"], { type: "image/png" }));
65
66 const result = await getExamChoiceImageBlob("ex1", "q1", "c2");
67 expect(result).toBeInstanceOf(Blob);
68
69 const [url, init] = lastFetchCall();
70 expect(url).toBe("/api/exams/ex1/questions/q1/choices/c2/image");
71 expect(init?.method).toBeUndefined();
72 });
73
74 it("getExamById calls /exams/:id", async () => {
75 const mockExam = { id: "ex1", title: "Exam" };
76 mockFetchSuccess(mockExam);
77
78 const result = await getExamById("ex1");
79 expect(result).toEqual(mockExam);
80
81 const [url, init] = lastFetchCall();
82 expect(url).toBe("/api/exams/ex1");
83 expect(init?.method).toBe("GET");
84 });
85
86 it("patchExamById calls /exams/:id with updates", async () => {
87 const mockExam = { id: "ex1", title: "Updated" };
88 mockFetchSuccess(mockExam);
89
90 const updates = { title: "Updated" };
91 const result = await patchExamById("ex1", updates);
92 expect(result).toEqual(mockExam);
93
94 const [url, init] = lastFetchCall();
95 expect(url).toBe("/api/exams/ex1");
96 expect(init?.method).toBe("PATCH");
97 expect(init?.body).toBe(JSON.stringify(updates));
98 });
99
100 it("deleteExamById calls /exams/:id", async () => {
101 mockFetch204();
102
103 await deleteExamById("ex1");
104
105 const [url, init] = lastFetchCall();
106 expect(url).toBe("/api/exams/ex1");
107 expect(init?.method).toBe("DELETE");
108 });
109
110 it("getExamQuestionImageBlob fetches the question image blob", async () => {
111 const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" });
112 mockFetchBlob(blob);
113
114 const result = await getExamQuestionImageBlob("ex1", "q1");
115 expect(result).toBeInstanceOf(Blob);
116
117 const [url, init] = lastFetchCall();
118 expect(url).toBe("/api/exams/ex1/questions/q1/image");
119 expect(init?.method ?? "GET").toBe("GET");
120 expect(init?.credentials).toBe("same-origin");
121 });
122
123 it("postExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image with FormData", async () => {
124 const mockMeta = { content_type: "image/png", size: 100 };
125 mockFetchSuccess(mockMeta);
126
127 const file = new File(["test"], "answer.png", { type: "image/png" });
128 const result = await postExamAttemptAnswerImage("ex1", "q1", file);
129 expect(result).toEqual(mockMeta);
130
131 const [url, init] = lastFetchCall();
132 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
133 expect(init?.method).toBe("POST");
134 expect(init?.body).toBeInstanceOf(FormData);
135 expect((init?.body as FormData).get("file")).toBe(file);
136 });
137
138 it("deleteExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image", async () => {
139 mockFetch204();
140
141 await deleteExamAttemptAnswerImage("ex1", "q1");
142
143 const [url, init] = lastFetchCall();
144 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
145 expect(init?.method).toBe("DELETE");
146 });
147
148 it("getExamAnswerImageBlob fetches the student's own answer image blob", async () => {
149 const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" });
150 mockFetchBlob(blob);
151
152 const result = await getExamAnswerImageBlob("ex1", "q1");
153 expect(result).toBeInstanceOf(Blob);
154
155 const [url, init] = lastFetchCall();
156 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
157 expect(init?.method ?? "GET").toBe("GET");
158 expect(init?.credentials).toBe("same-origin");
159 });
160 it("getExamAudience reads the whole audience array", async () => {
161 mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]);
162
163 const audience = await getExamAudience("x1");
164
165 const [url, init] = lastFetchCall();
166 expect(url).toBe("/api/exams/x1/audience");
167 expect(init?.method ?? "GET").toBe("GET");
168 expect(audience).toEqual([{ instance: "i1", class: "c1", course: "co1" }]);
169 });
170
171 it("postExamAudience announces the exam and returns the audience after the write", async () => {
172 const after = [
173 { instance: "i1", class: "c1", course: "co1" },
174 { instance: "i2", class: "c2", course: "co1" },
175 ];
176 mockFetchSuccess(after);
177
178 const audience = await postExamAudience("x1", "i2");
179
180 const [url, init] = lastFetchCall();
181 expect(url).toBe("/api/exams/x1/audience");
182 expect(init?.method).toBe("POST");
183 expect(init?.body).toBe(JSON.stringify({ instance: "i2" }));
184 expect(audience).toEqual(after);
185 });
186
187 it("deleteExamAudienceByInstanceId withdraws one instance and returns what is left", async () => {
188 mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]);
189
190 const audience = await deleteExamAudienceByInstanceId("x1", "i2");
191
192 const [url, init] = lastFetchCall();
193 expect(url).toBe("/api/exams/x1/audience/i2");
194 expect(init?.method).toBe("DELETE");
195 expect(audience).toHaveLength(1);
196 });
197
198});