forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

exams.test.ts187 lines, 6.9 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
52 it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => {
53 mockFetchBlob(new Blob(["img"], { type: "image/png" }));
54
55 const result = await getExamChoiceImageBlob("ex1", "q1", "c2");
56 expect(result).toBeInstanceOf(Blob);
57
58 const [url, init] = lastFetchCall();
59 expect(url).toBe("/api/exams/ex1/questions/q1/choices/c2/image");
60 expect(init?.method).toBeUndefined();
61 });
62
63 it("getExamById calls /exams/:id", async () => {
64 const mockExam = { id: "ex1", title: "Exam" };
65 mockFetchSuccess(mockExam);
66
67 const result = await getExamById("ex1");
68 expect(result).toEqual(mockExam);
69
70 const [url, init] = lastFetchCall();
71 expect(url).toBe("/api/exams/ex1");
72 expect(init?.method).toBe("GET");
73 });
74
75 it("patchExamById calls /exams/:id with updates", async () => {
76 const mockExam = { id: "ex1", title: "Updated" };
77 mockFetchSuccess(mockExam);
78
79 const updates = { title: "Updated" };
80 const result = await patchExamById("ex1", updates);
81 expect(result).toEqual(mockExam);
82
83 const [url, init] = lastFetchCall();
84 expect(url).toBe("/api/exams/ex1");
85 expect(init?.method).toBe("PATCH");
86 expect(init?.body).toBe(JSON.stringify(updates));
87 });
88
89 it("deleteExamById calls /exams/:id", async () => {
90 mockFetch204();
91
92 await deleteExamById("ex1");
93
94 const [url, init] = lastFetchCall();
95 expect(url).toBe("/api/exams/ex1");
96 expect(init?.method).toBe("DELETE");
97 });
98
99 it("getExamQuestionImageBlob fetches the question image blob", async () => {
100 const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" });
101 mockFetchBlob(blob);
102
103 const result = await getExamQuestionImageBlob("ex1", "q1");
104 expect(result).toBeInstanceOf(Blob);
105
106 const [url, init] = lastFetchCall();
107 expect(url).toBe("/api/exams/ex1/questions/q1/image");
108 expect(init?.method ?? "GET").toBe("GET");
109 expect(init?.credentials).toBe("same-origin");
110 });
111
112 it("postExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image with FormData", async () => {
113 const mockMeta = { content_type: "image/png", size: 100 };
114 mockFetchSuccess(mockMeta);
115
116 const file = new File(["test"], "answer.png", { type: "image/png" });
117 const result = await postExamAttemptAnswerImage("ex1", "q1", file);
118 expect(result).toEqual(mockMeta);
119
120 const [url, init] = lastFetchCall();
121 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
122 expect(init?.method).toBe("POST");
123 expect(init?.body).toBeInstanceOf(FormData);
124 expect((init?.body as FormData).get("file")).toBe(file);
125 });
126
127 it("deleteExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image", async () => {
128 mockFetch204();
129
130 await deleteExamAttemptAnswerImage("ex1", "q1");
131
132 const [url, init] = lastFetchCall();
133 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
134 expect(init?.method).toBe("DELETE");
135 });
136
137 it("getExamAnswerImageBlob fetches the student's own answer image blob", async () => {
138 const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" });
139 mockFetchBlob(blob);
140
141 const result = await getExamAnswerImageBlob("ex1", "q1");
142 expect(result).toBeInstanceOf(Blob);
143
144 const [url, init] = lastFetchCall();
145 expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image");
146 expect(init?.method ?? "GET").toBe("GET");
147 expect(init?.credentials).toBe("same-origin");
148 });
149 it("getExamAudience reads the whole audience array", async () => {
150 mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]);
151
152 const audience = await getExamAudience("x1");
153
154 const [url, init] = lastFetchCall();
155 expect(url).toBe("/api/exams/x1/audience");
156 expect(init?.method ?? "GET").toBe("GET");
157 expect(audience).toEqual([{ instance: "i1", class: "c1", course: "co1" }]);
158 });
159
160 it("postExamAudience announces the exam and returns the audience after the write", async () => {
161 const after = [
162 { instance: "i1", class: "c1", course: "co1" },
163 { instance: "i2", class: "c2", course: "co1" },
164 ];
165 mockFetchSuccess(after);
166
167 const audience = await postExamAudience("x1", "i2");
168
169 const [url, init] = lastFetchCall();
170 expect(url).toBe("/api/exams/x1/audience");
171 expect(init?.method).toBe("POST");
172 expect(init?.body).toBe(JSON.stringify({ instance: "i2" }));
173 expect(audience).toEqual(after);
174 });
175
176 it("deleteExamAudienceByInstanceId withdraws one instance and returns what is left", async () => {
177 mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]);
178
179 const audience = await deleteExamAudienceByInstanceId("x1", "i2");
180
181 const [url, init] = lastFetchCall();
182 expect(url).toBe("/api/exams/x1/audience/i2");
183 expect(init?.method).toBe("DELETE");
184 expect(audience).toHaveLength(1);
185 });
186
187});