forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

instances.test.ts312 lines, 10.8 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 deleteInstanceEnrollmentByUserId,
4 deleteInstanceTeacherByUserId,
5 getInstanceById,
6 getInstanceEnrollments,
7 getInstanceExams,
8 getInstanceHomework,
9 getInstanceSessions,
10 getMyInstances,
11 patchInstanceById,
12 postInstanceEnrollment,
13 postInstanceExam,
14 postInstanceHomework,
15 postInstanceSession,
16 postInstanceTeacher,
17 deleteInstanceExamWeightByKind,
18 deleteInstanceExamWeights,
19 deleteInstanceSubjectById,
20 deleteInstanceSubjects,
21 deleteInstanceWeeklyPlan,
22 deleteInstanceWeeklySlotById,
23 getInstanceExamWeights,
24 getInstanceSubjects,
25 getInstanceWeeklyPlan,
26 patchInstanceExamWeight,
27 postInstanceReset,
28 postInstanceSubject,
29 postInstanceWeeklyPlanMaterialize,
30 postInstanceWeeklySlot,
31} from "../../instances";
32import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
33
34describe("instances API", () => {
35 afterEach(() => {
36 vi.restoreAllMocks();
37 });
38
39 it("getMyInstances GETs /instances/me with pagination", async () => {
40 mockFetchSuccess({ items: [{ id: "i1" }], total: 1 });
41
42 const result = await getMyInstances({ limit: 20, offset: 40 });
43 expect(result.items).toEqual([{ id: "i1" }]);
44
45 const [url, init] = lastFetchCall();
46 expect(url).toBe("/api/instances/me?limit=20&offset=40");
47 expect(init?.method).toBe("GET");
48 });
49
50 it("getInstanceById GETs /instances/:id", async () => {
51 mockFetchSuccess({ id: "i1" });
52
53 await getInstanceById("i1");
54
55 const [url, init] = lastFetchCall();
56 expect(url).toBe("/api/instances/i1");
57 expect(init?.method).toBe("GET");
58 });
59
60 it("patchInstanceById PATCHes /instances/:id with the policy body", async () => {
61 mockFetchSuccess({ id: "i1" });
62 const body = { title: "9-A Matematik", description: "Sayısal", ders_saati: 4, counts_toward_karne: false };
63
64 await patchInstanceById("i1", body);
65
66 const [url, init] = lastFetchCall();
67 expect(url).toBe("/api/instances/i1");
68 expect(init?.method).toBe("PATCH");
69 expect(init?.body).toBe(JSON.stringify(body));
70 });
71
72 it("getInstanceEnrollments GETs /instances/:id/enrollments with pagination", async () => {
73 mockFetchSuccess({ items: [], total: 0 });
74
75 await getInstanceEnrollments("i1", { limit: 5 });
76
77 const [url] = lastFetchCall();
78 expect(url).toBe("/api/instances/i1/enrollments?limit=5");
79 });
80
81 it("postInstanceEnrollment POSTs /instances/:id/enrollments with the user id", async () => {
82 mockFetchSuccess({ id: "e1" });
83
84 await postInstanceEnrollment("i1", "u1");
85
86 const [url, init] = lastFetchCall();
87 expect(url).toBe("/api/instances/i1/enrollments");
88 expect(init?.method).toBe("POST");
89 expect(init?.body).toBe(JSON.stringify({ user_id: "u1" }));
90 });
91
92 it("deleteInstanceEnrollmentByUserId DELETEs /instances/:id/enrollments/:user", async () => {
93 mockFetch204();
94
95 await deleteInstanceEnrollmentByUserId("i1", "u1");
96
97 const [url, init] = lastFetchCall();
98 expect(url).toBe("/api/instances/i1/enrollments/u1");
99 expect(init?.method).toBe("DELETE");
100 });
101
102 it("getInstanceExams GETs /instances/:id/exams", async () => {
103 mockFetchSuccess({ items: [], total: 0 });
104
105 await getInstanceExams("i1");
106
107 const [url] = lastFetchCall();
108 expect(url).toBe("/api/instances/i1/exams");
109 });
110
111 it("postInstanceExam POSTs /instances/:id/exams with the term", async () => {
112 mockFetchSuccess({ id: "x1" });
113 const body = { title: "Quiz 1", kind: "quiz", term: "t1" };
114
115 await postInstanceExam("i1", body);
116
117 const [url, init] = lastFetchCall();
118 expect(url).toBe("/api/instances/i1/exams");
119 expect(init?.method).toBe("POST");
120 expect(init?.body).toBe(JSON.stringify(body));
121 });
122
123 it("getInstanceHomework GETs /instances/:id/homework", async () => {
124 mockFetchSuccess({ items: [], total: 0 });
125
126 await getInstanceHomework("i1");
127
128 const [url] = lastFetchCall();
129 expect(url).toBe("/api/instances/i1/homework");
130 });
131
132 it("postInstanceHomework POSTs /instances/:id/homework with the body", async () => {
133 mockFetchSuccess({ id: "h1" });
134 const body = { title: "Read ch. 3", subject_id: "s1", due_at: 1900000000000 };
135
136 await postInstanceHomework("i1", body);
137
138 const [url, init] = lastFetchCall();
139 expect(url).toBe("/api/instances/i1/homework");
140 expect(init?.method).toBe("POST");
141 expect(init?.body).toBe(JSON.stringify(body));
142 });
143
144 it("getInstanceSessions GETs /instances/:id/sessions", async () => {
145 mockFetchSuccess({ items: [], total: 0 });
146
147 await getInstanceSessions("i1");
148
149 const [url] = lastFetchCall();
150 expect(url).toBe("/api/instances/i1/sessions");
151 });
152
153 it("getInstanceSessions sends the half-open start window when set", async () => {
154 mockFetchSuccess({ items: [], total: 0 });
155
156 await getInstanceSessions("i1", { starts_after: 1700000000000, starts_before: 1800000000000, limit: 100 });
157
158 const [url] = lastFetchCall();
159 expect(url).toBe("/api/instances/i1/sessions?limit=100&starts_after=1700000000000&starts_before=1800000000000");
160 });
161
162 it("postInstanceSession POSTs /instances/:id/sessions with the body", async () => {
163 mockFetchSuccess({ id: "se1" });
164 const body = { starts_at: 1900000000000, topic: "Limits" };
165
166 await postInstanceSession("i1", body);
167
168 const [url, init] = lastFetchCall();
169 expect(url).toBe("/api/instances/i1/sessions");
170 expect(init?.method).toBe("POST");
171 expect(init?.body).toBe(JSON.stringify(body));
172 });
173
174 it("postInstanceTeacher POSTs /instances/:id/teachers with the user id", async () => {
175 mockFetchSuccess({ id: "i1" });
176
177 await postInstanceTeacher("i1", "t1");
178
179 const [url, init] = lastFetchCall();
180 expect(url).toBe("/api/instances/i1/teachers");
181 expect(init?.method).toBe("POST");
182 expect(init?.body).toBe(JSON.stringify({ user_id: "t1" }));
183 });
184
185 it("deleteInstanceTeacherByUserId DELETEs /instances/:id/teachers/:user", async () => {
186 mockFetch204();
187
188 await deleteInstanceTeacherByUserId("i1", "t1");
189
190 const [url, init] = lastFetchCall();
191 expect(url).toBe("/api/instances/i1/teachers/t1");
192 expect(init?.method).toBe("DELETE");
193 });
194
195 // Per-section overrides over the grade-level offering.
196 it("postInstanceReset POSTs /instances/i1/reset", async () => {
197 mockFetchSuccess({ id: "x1" });
198 await postInstanceReset("i1", ["title", "weekly_plan"]);
199 const [url, init] = lastFetchCall();
200 expect(url).toBe("/api/instances/i1/reset");
201 expect(init?.method).toBe("POST");
202 expect(init?.body).toBe(JSON.stringify({ fields: ["title", "weekly_plan"] }));
203 });
204
205 it("getInstanceSubjects GETs /instances/i1/subjects", async () => {
206 mockFetchSuccess({ subjects: [], subjects_inherited: true });
207 await getInstanceSubjects("i1");
208 const [url, init] = lastFetchCall();
209 expect(url).toBe("/api/instances/i1/subjects");
210 expect(init?.method).toBe("GET");
211 });
212
213 it("postInstanceSubject POSTs /instances/i1/subjects", async () => {
214 mockFetchSuccess({ id: "x1" });
215 await postInstanceSubject("i1", "s1");
216 const [url, init] = lastFetchCall();
217 expect(url).toBe("/api/instances/i1/subjects");
218 expect(init?.method).toBe("POST");
219 expect(init?.body).toBe(JSON.stringify({ subject: "s1" }));
220 });
221
222 it("deleteInstanceSubjects DELETEs /instances/i1/subjects", async () => {
223 mockFetch204();
224 await deleteInstanceSubjects("i1");
225 const [url, init] = lastFetchCall();
226 expect(url).toBe("/api/instances/i1/subjects");
227 expect(init?.method).toBe("DELETE");
228 });
229
230 it("deleteInstanceSubjectById DELETEs /instances/i1/subjects/s1", async () => {
231 mockFetch204();
232 await deleteInstanceSubjectById("i1", "s1");
233 const [url, init] = lastFetchCall();
234 expect(url).toBe("/api/instances/i1/subjects/s1");
235 expect(init?.method).toBe("DELETE");
236 });
237
238 it("getInstanceWeeklyPlan GETs /instances/i1/weekly-plan", async () => {
239 mockFetchSuccess({ weekly_plan: [], weekly_plan_inherited: true });
240 await getInstanceWeeklyPlan("i1");
241 const [url, init] = lastFetchCall();
242 expect(url).toBe("/api/instances/i1/weekly-plan");
243 expect(init?.method).toBe("GET");
244 });
245
246 it("postInstanceWeeklySlot POSTs /instances/i1/weekly-plan", async () => {
247 mockFetchSuccess({ id: "x1" });
248 await postInstanceWeeklySlot("i1", { weekday: 3, starts_at: 600, ends_at: 640 });
249 const [url, init] = lastFetchCall();
250 expect(url).toBe("/api/instances/i1/weekly-plan");
251 expect(init?.method).toBe("POST");
252 expect(init?.body).toBe(JSON.stringify({ weekday: 3, starts_at: 600, ends_at: 640 }));
253 });
254
255 it("deleteInstanceWeeklyPlan DELETEs /instances/i1/weekly-plan", async () => {
256 mockFetch204();
257 await deleteInstanceWeeklyPlan("i1");
258 const [url, init] = lastFetchCall();
259 expect(url).toBe("/api/instances/i1/weekly-plan");
260 expect(init?.method).toBe("DELETE");
261 });
262
263 it("deleteInstanceWeeklySlotById DELETEs /instances/i1/weekly-plan/sl1", async () => {
264 mockFetch204();
265 await deleteInstanceWeeklySlotById("i1", "sl1");
266 const [url, init] = lastFetchCall();
267 expect(url).toBe("/api/instances/i1/weekly-plan/sl1");
268 expect(init?.method).toBe("DELETE");
269 });
270
271 it("postInstanceWeeklyPlanMaterialize POSTs the range as a dry run", async () => {
272 mockFetchSuccess({ applied: false, created: [] });
273 await postInstanceWeeklyPlanMaterialize("i1", { from: 10, to: 20, apply: false });
274 const [url, init] = lastFetchCall();
275 expect(url).toBe("/api/instances/i1/weekly-plan/materialize");
276 expect(init?.method).toBe("POST");
277 expect(init?.body).toBe(JSON.stringify({ from: 10, to: 20, apply: false }));
278 });
279
280 it("getInstanceExamWeights GETs /instances/i1/exam-weights", async () => {
281 mockFetchSuccess({ inherited: true, weights: [] });
282 await getInstanceExamWeights("i1");
283 const [url, init] = lastFetchCall();
284 expect(url).toBe("/api/instances/i1/exam-weights");
285 expect(init?.method).toBe("GET");
286 });
287
288 it("patchInstanceExamWeight PATCHs /instances/i1/exam-weights", async () => {
289 mockFetchSuccess({ inherited: false, weights: [] });
290 await patchInstanceExamWeight("i1", { kind: "quiz", weight: 10 });
291 const [url, init] = lastFetchCall();
292 expect(url).toBe("/api/instances/i1/exam-weights");
293 expect(init?.method).toBe("PATCH");
294 expect(init?.body).toBe(JSON.stringify({ kind: "quiz", weight: 10 }));
295 });
296
297 it("deleteInstanceExamWeights DELETEs /instances/i1/exam-weights", async () => {
298 mockFetch204();
299 await deleteInstanceExamWeights("i1");
300 const [url, init] = lastFetchCall();
301 expect(url).toBe("/api/instances/i1/exam-weights");
302 expect(init?.method).toBe("DELETE");
303 });
304
305 it("deleteInstanceExamWeightByKind DELETEs /instances/i1/exam-weights/quiz", async () => {
306 mockFetch204();
307 await deleteInstanceExamWeightByKind("i1", "quiz");
308 const [url, init] = lastFetchCall();
309 expect(url).toBe("/api/instances/i1/exam-weights/quiz");
310 expect(init?.method).toBe("DELETE");
311 });
312});