forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

classes.test.ts215 lines, 8.0 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 postClass,
4 getClasses,
5 getClassById,
6 getMyClasses,
7 getClassesByUserId,
8 patchClassById,
9 deleteClassById,
10 postClassMember,
11 getClassMembers,
12 deleteClassMember,
13 postClassInstance,
14 getClassInstances,
15 deleteClassInstance,
16 postClassBlueprint,
17 getClassBlueprints,
18 getClassBlueprintByGrade,
19 patchClassBlueprintByGrade,
20 deleteClassBlueprintByGrade,
21 postClassBlueprintApply,
22 getClassBlueprintStatus,
23} from "../../classes";
24import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
25
26describe("classes API", () => {
27 afterEach(() => {
28 vi.restoreAllMocks();
29 });
30
31 // POST /classes answers with an envelope, not the bare class: the created
32 // row plus the pairs its grade's blueprint could not attach.
33 it("postClass POSTs /classes and returns the create envelope", async () => {
34 const envelope = { class: { id: "c1" }, skipped: [], stocked_from: null };
35 mockFetchSuccess(envelope);
36 const body = { name: "9-A", grade_level: 9, year: "y1" };
37 const created = await postClass(body);
38 expect(created).toEqual(envelope);
39 expect(created.class.id).toBe("c1");
40 const [url, init] = lastFetchCall();
41 expect(url).toBe("/api/classes");
42 expect(init?.method).toBe("POST");
43 expect(init?.body).toBe(JSON.stringify(body));
44 });
45
46 it("getClasses GETs /classes with pagination", async () => {
47 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
48 await getClasses({ limit: 5, offset: 10 });
49 const [url, init] = lastFetchCall();
50 expect(url).toBe("/api/classes?limit=5&offset=10");
51 expect(init?.method ?? "GET").toBe("GET");
52 });
53
54 it("getClasses sends grade_level only when set", async () => {
55 mockFetchSuccess({ items: [], total: 0 });
56 await getClasses({ grade_level: 9, limit: 5 });
57 expect(lastFetchCall()[0]).toBe("/api/classes?limit=5&grade_level=9");
58
59 await getClasses({ grade_level: undefined, limit: 5 });
60 expect(lastFetchCall()[0]).toBe("/api/classes?limit=5");
61 });
62
63 it("getClassById GETs /classes/:id", async () => {
64 mockFetchSuccess({ id: "c1" });
65 await getClassById("c1");
66 const [url] = lastFetchCall();
67 expect(url).toBe("/api/classes/c1");
68 });
69
70 it("getMyClasses GETs /classes/me with pagination", async () => {
71 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
72 await getMyClasses({ limit: 5, offset: 10 });
73 const [url, init] = lastFetchCall();
74 expect(url).toBe("/api/classes/me?limit=5&offset=10");
75 expect(init?.method ?? "GET").toBe("GET");
76 });
77
78 it("getClassesByUserId GETs /classes/user/:id with pagination", async () => {
79 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
80 await getClassesByUserId("u1", { limit: 5, offset: 10 });
81 const [url] = lastFetchCall();
82 expect(url).toBe("/api/classes/user/u1?limit=5&offset=10");
83 });
84
85 it("patchClassById PATCHes /classes/:id with the body", async () => {
86 mockFetchSuccess({ id: "c1" });
87 const body = { name: "9-B", grade_level: 10, year: null };
88 await patchClassById("c1", body);
89 const [url, init] = lastFetchCall();
90 expect(url).toBe("/api/classes/c1");
91 expect(init?.method).toBe("PATCH");
92 expect(init?.body).toBe(JSON.stringify(body));
93 });
94
95 it("deleteClassById DELETEs /classes/:id", async () => {
96 mockFetch204();
97 await deleteClassById("c1");
98 const [url, init] = lastFetchCall();
99 expect(url).toBe("/api/classes/c1");
100 expect(init?.method).toBe("DELETE");
101 });
102
103 it("postClassMember POSTs /classes/:id/members with the body", async () => {
104 mockFetchSuccess({ id: "m1" });
105 const body = { user_id: "u1" };
106 await postClassMember("c1", body);
107 const [url, init] = lastFetchCall();
108 expect(url).toBe("/api/classes/c1/members");
109 expect(init?.method).toBe("POST");
110 expect(init?.body).toBe(JSON.stringify(body));
111 });
112
113 it("getClassMembers GETs /classes/:id/members with pagination", async () => {
114 mockFetchSuccess({ items: [], total: 0 });
115 await getClassMembers("c1", { limit: 5, offset: 10 });
116 const [url] = lastFetchCall();
117 expect(url).toBe("/api/classes/c1/members?limit=5&offset=10");
118 });
119
120 it("deleteClassMember DELETEs /classes/:id/members/:user", async () => {
121 mockFetch204();
122 await deleteClassMember("c1", "u1");
123 const [url, init] = lastFetchCall();
124 expect(url).toBe("/api/classes/c1/members/u1");
125 expect(init?.method).toBe("DELETE");
126 });
127
128 it("postClassInstance POSTs /classes/:id/instances with the body", async () => {
129 mockFetchSuccess({ id: "cc1" });
130 const body = { course_id: "co1" };
131 await postClassInstance("c1", body);
132 const [url, init] = lastFetchCall();
133 expect(url).toBe("/api/classes/c1/instances");
134 expect(init?.method).toBe("POST");
135 expect(init?.body).toBe(JSON.stringify(body));
136 });
137
138 it("getClassInstances GETs /classes/:id/instances with pagination", async () => {
139 mockFetchSuccess({ items: [], total: 0 });
140 await getClassInstances("c1", { limit: 5, offset: 10 });
141 const [url] = lastFetchCall();
142 expect(url).toBe("/api/classes/c1/instances?limit=5&offset=10");
143 });
144
145 // The second segment is the instance id, not the course id — two şubeler
146 // teaching the same course hold two instances.
147 it("deleteClassInstance DELETEs /classes/:id/instances/:instance", async () => {
148 mockFetch204();
149 await deleteClassInstance("c1", "i1");
150 const [url, init] = lastFetchCall();
151 expect(url).toBe("/api/classes/c1/instances/i1");
152 expect(init?.method).toBe("DELETE");
153 });
154
155 it("postClassBlueprint POSTs /classes/blueprints with the body", async () => {
156 mockFetchSuccess({ blueprint: { grade_level: 9, courses: [], creator: {} }, matched: 2, skipped: [] });
157 const body = { grade_level: 9, course_ids: ["co1", "co2"] };
158 await postClassBlueprint(body);
159 const [url, init] = lastFetchCall();
160 expect(url).toBe("/api/classes/blueprints");
161 expect(init?.method).toBe("POST");
162 expect(init?.body).toBe(JSON.stringify(body));
163 });
164
165 it("getClassBlueprints GETs /classes/blueprints with pagination", async () => {
166 mockFetchSuccess({ items: [], total: 0 });
167 await getClassBlueprints({ limit: 20, offset: 40 });
168 const [url] = lastFetchCall();
169 expect(url).toBe("/api/classes/blueprints?limit=20&offset=40");
170 });
171
172 it("getClassBlueprintByGrade keys the blueprint by its grade level", async () => {
173 mockFetchSuccess({ grade_level: 0, courses: [], creator: {} });
174 await getClassBlueprintByGrade(0);
175 const [url] = lastFetchCall();
176 expect(url).toBe("/api/classes/blueprints/0");
177 });
178
179 it("patchClassBlueprintByGrade PATCHes the whole course set", async () => {
180 mockFetchSuccess({ blueprint: { grade_level: 9, courses: [], creator: {} }, matched: 2, skipped: [] });
181 const body = { course_ids: ["co1"] };
182 await patchClassBlueprintByGrade(9, body);
183 const [url, init] = lastFetchCall();
184 expect(url).toBe("/api/classes/blueprints/9");
185 expect(init?.method).toBe("PATCH");
186 expect(init?.body).toBe(JSON.stringify(body));
187 });
188
189 it("deleteClassBlueprintByGrade DELETEs /classes/blueprints/:grade", async () => {
190 mockFetch204();
191 await deleteClassBlueprintByGrade(11);
192 const [url, init] = lastFetchCall();
193 expect(url).toBe("/api/classes/blueprints/11");
194 expect(init?.method).toBe("DELETE");
195 });
196
197 it("postClassBlueprintApply POSTs /classes/:id/blueprint with no body", async () => {
198 mockFetchSuccess({ skipped: [] });
199 await postClassBlueprintApply("c1");
200 const [url, init] = lastFetchCall();
201 expect(url).toBe("/api/classes/c1/blueprint");
202 expect(init?.method).toBe("POST");
203 expect(init?.body).toBeUndefined();
204 });
205
206 it("getClassBlueprintStatus GETs /classes/blueprints/:grade/status", async () => {
207 const mockStatus = { grade_level: 9, courses: ["co1"], matched: 3, sections: [] };
208 mockFetchSuccess(mockStatus);
209 const result = await getClassBlueprintStatus(9);
210 expect(result).toEqual(mockStatus);
211 const [url, init] = lastFetchCall();
212 expect(url).toBe("/api/classes/blueprints/9/status");
213 expect(init?.method).toBe("GET");
214 });
215});