forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB2c19dffBurak Boduroğlu2 months ago566 commits

classes.test.ts201 lines, 7.3 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 it("postClass POSTs /classes with the body", async () => {
32 mockFetchSuccess({ id: "c1" });
33 const body = { name: "9-A", grade: "9", term_id: "t1" };
34 await postClass(body);
35 const [url, init] = lastFetchCall();
36 expect(url).toBe("/api/classes");
37 expect(init?.method).toBe("POST");
38 expect(init?.body).toBe(JSON.stringify(body));
39 });
40
41 it("getClasses GETs /classes with pagination", async () => {
42 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
43 await getClasses({ limit: 5, offset: 10 });
44 const [url, init] = lastFetchCall();
45 expect(url).toBe("/api/classes?limit=5&offset=10");
46 expect(init?.method ?? "GET").toBe("GET");
47 });
48
49 it("getClassById GETs /classes/:id", async () => {
50 mockFetchSuccess({ id: "c1" });
51 await getClassById("c1");
52 const [url] = lastFetchCall();
53 expect(url).toBe("/api/classes/c1");
54 });
55
56 it("getMyClasses GETs /classes/me with pagination", async () => {
57 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
58 await getMyClasses({ limit: 5, offset: 10 });
59 const [url, init] = lastFetchCall();
60 expect(url).toBe("/api/classes/me?limit=5&offset=10");
61 expect(init?.method ?? "GET").toBe("GET");
62 });
63
64 it("getClassesByUserId GETs /classes/user/:id with pagination", async () => {
65 mockFetchSuccess({ items: [{ id: "c1" }], total: 1 });
66 await getClassesByUserId("u1", { limit: 5, offset: 10 });
67 const [url] = lastFetchCall();
68 expect(url).toBe("/api/classes/user/u1?limit=5&offset=10");
69 });
70
71 it("patchClassById PATCHes /classes/:id with the body", async () => {
72 mockFetchSuccess({ id: "c1" });
73 const body = { name: "9-B", grade: null, term_id: null };
74 await patchClassById("c1", body);
75 const [url, init] = lastFetchCall();
76 expect(url).toBe("/api/classes/c1");
77 expect(init?.method).toBe("PATCH");
78 expect(init?.body).toBe(JSON.stringify(body));
79 });
80
81 it("deleteClassById DELETEs /classes/:id", async () => {
82 mockFetch204();
83 await deleteClassById("c1");
84 const [url, init] = lastFetchCall();
85 expect(url).toBe("/api/classes/c1");
86 expect(init?.method).toBe("DELETE");
87 });
88
89 it("postClassMember POSTs /classes/:id/members with the body", async () => {
90 mockFetchSuccess({ id: "m1" });
91 const body = { user_id: "u1" };
92 await postClassMember("c1", body);
93 const [url, init] = lastFetchCall();
94 expect(url).toBe("/api/classes/c1/members");
95 expect(init?.method).toBe("POST");
96 expect(init?.body).toBe(JSON.stringify(body));
97 });
98
99 it("getClassMembers GETs /classes/:id/members with pagination", async () => {
100 mockFetchSuccess({ items: [], total: 0 });
101 await getClassMembers("c1", { limit: 5, offset: 10 });
102 const [url] = lastFetchCall();
103 expect(url).toBe("/api/classes/c1/members?limit=5&offset=10");
104 });
105
106 it("deleteClassMember DELETEs /classes/:id/members/:user", async () => {
107 mockFetch204();
108 await deleteClassMember("c1", "u1");
109 const [url, init] = lastFetchCall();
110 expect(url).toBe("/api/classes/c1/members/u1");
111 expect(init?.method).toBe("DELETE");
112 });
113
114 it("postClassInstance POSTs /classes/:id/instances with the body", async () => {
115 mockFetchSuccess({ id: "cc1" });
116 const body = { course_id: "co1" };
117 await postClassInstance("c1", body);
118 const [url, init] = lastFetchCall();
119 expect(url).toBe("/api/classes/c1/instances");
120 expect(init?.method).toBe("POST");
121 expect(init?.body).toBe(JSON.stringify(body));
122 });
123
124 it("getClassInstances GETs /classes/:id/instances with pagination", async () => {
125 mockFetchSuccess({ items: [], total: 0 });
126 await getClassInstances("c1", { limit: 5, offset: 10 });
127 const [url] = lastFetchCall();
128 expect(url).toBe("/api/classes/c1/instances?limit=5&offset=10");
129 });
130
131 // The second segment is the instance id, not the course id — two şubeler
132 // teaching the same course hold two instances.
133 it("deleteClassInstance DELETEs /classes/:id/instances/:instance", async () => {
134 mockFetch204();
135 await deleteClassInstance("c1", "i1");
136 const [url, init] = lastFetchCall();
137 expect(url).toBe("/api/classes/c1/instances/i1");
138 expect(init?.method).toBe("DELETE");
139 });
140
141 it("postClassBlueprint POSTs /classes/blueprints with the body", async () => {
142 mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, skipped: [] });
143 const body = { grade: "9", course_ids: ["co1", "co2"] };
144 await postClassBlueprint(body);
145 const [url, init] = lastFetchCall();
146 expect(url).toBe("/api/classes/blueprints");
147 expect(init?.method).toBe("POST");
148 expect(init?.body).toBe(JSON.stringify(body));
149 });
150
151 it("getClassBlueprints GETs /classes/blueprints with pagination", async () => {
152 mockFetchSuccess({ items: [], total: 0 });
153 await getClassBlueprints({ limit: 20, offset: 40 });
154 const [url] = lastFetchCall();
155 expect(url).toBe("/api/classes/blueprints?limit=20&offset=40");
156 });
157
158 it("getClassBlueprintByGrade encodes the grade, which is the record key", async () => {
159 mockFetchSuccess({ grade: "9/A", courses: [], creator: {} });
160 await getClassBlueprintByGrade("9/A");
161 const [url] = lastFetchCall();
162 expect(url).toBe("/api/classes/blueprints/9%2FA");
163 });
164
165 it("patchClassBlueprintByGrade PATCHes the whole course set", async () => {
166 mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, skipped: [] });
167 const body = { course_ids: ["co1"] };
168 await patchClassBlueprintByGrade("9", body);
169 const [url, init] = lastFetchCall();
170 expect(url).toBe("/api/classes/blueprints/9");
171 expect(init?.method).toBe("PATCH");
172 expect(init?.body).toBe(JSON.stringify(body));
173 });
174
175 it("deleteClassBlueprintByGrade DELETEs /classes/blueprints/:grade", async () => {
176 mockFetch204();
177 await deleteClassBlueprintByGrade("9/A");
178 const [url, init] = lastFetchCall();
179 expect(url).toBe("/api/classes/blueprints/9%2FA");
180 expect(init?.method).toBe("DELETE");
181 });
182
183 it("postClassBlueprintApply POSTs /classes/:id/blueprint with no body", async () => {
184 mockFetchSuccess({ skipped: [] });
185 await postClassBlueprintApply("c1");
186 const [url, init] = lastFetchCall();
187 expect(url).toBe("/api/classes/c1/blueprint");
188 expect(init?.method).toBe("POST");
189 expect(init?.body).toBeUndefined();
190 });
191
192 it("getClassBlueprintStatus GETs /classes/blueprints/:grade/status", async () => {
193 const mockStatus = { grade: "9/A", courses: ["co1"], matched: 3, sections: [] };
194 mockFetchSuccess(mockStatus);
195 const result = await getClassBlueprintStatus("9/A");
196 expect(result).toEqual(mockStatus);
197 const [url, init] = lastFetchCall();
198 expect(url).toBe("/api/classes/blueprints/9%2FA/status");
199 expect(init?.method).toBe("GET");
200 });
201});