forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB0069847Burak Boduroğlu2 months ago552 commits

classes.test.ts199 lines, 7.2 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 postClassCourse,
14 getClassCourses,
15 deleteClassCourse,
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("postClassCourse POSTs /classes/:id/courses with the body", async () => {
115 mockFetchSuccess({ id: "cc1" });
116 const body = { course_id: "co1" };
117 await postClassCourse("c1", body);
118 const [url, init] = lastFetchCall();
119 expect(url).toBe("/api/classes/c1/courses");
120 expect(init?.method).toBe("POST");
121 expect(init?.body).toBe(JSON.stringify(body));
122 });
123
124 it("getClassCourses GETs /classes/:id/courses with pagination", async () => {
125 mockFetchSuccess({ items: [], total: 0 });
126 await getClassCourses("c1", { limit: 5, offset: 10 });
127 const [url] = lastFetchCall();
128 expect(url).toBe("/api/classes/c1/courses?limit=5&offset=10");
129 });
130
131 it("deleteClassCourse DELETEs /classes/:id/courses/:course", async () => {
132 mockFetch204();
133 await deleteClassCourse("c1", "co1");
134 const [url, init] = lastFetchCall();
135 expect(url).toBe("/api/classes/c1/courses/co1");
136 expect(init?.method).toBe("DELETE");
137 });
138
139 it("postClassBlueprint POSTs /classes/blueprints with the body", async () => {
140 mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, skipped: [] });
141 const body = { grade: "9", course_ids: ["co1", "co2"] };
142 await postClassBlueprint(body);
143 const [url, init] = lastFetchCall();
144 expect(url).toBe("/api/classes/blueprints");
145 expect(init?.method).toBe("POST");
146 expect(init?.body).toBe(JSON.stringify(body));
147 });
148
149 it("getClassBlueprints GETs /classes/blueprints with pagination", async () => {
150 mockFetchSuccess({ items: [], total: 0 });
151 await getClassBlueprints({ limit: 20, offset: 40 });
152 const [url] = lastFetchCall();
153 expect(url).toBe("/api/classes/blueprints?limit=20&offset=40");
154 });
155
156 it("getClassBlueprintByGrade encodes the grade, which is the record key", async () => {
157 mockFetchSuccess({ grade: "9/A", courses: [], creator: {} });
158 await getClassBlueprintByGrade("9/A");
159 const [url] = lastFetchCall();
160 expect(url).toBe("/api/classes/blueprints/9%2FA");
161 });
162
163 it("patchClassBlueprintByGrade PATCHes the whole course set", async () => {
164 mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, skipped: [] });
165 const body = { course_ids: ["co1"] };
166 await patchClassBlueprintByGrade("9", body);
167 const [url, init] = lastFetchCall();
168 expect(url).toBe("/api/classes/blueprints/9");
169 expect(init?.method).toBe("PATCH");
170 expect(init?.body).toBe(JSON.stringify(body));
171 });
172
173 it("deleteClassBlueprintByGrade DELETEs /classes/blueprints/:grade", async () => {
174 mockFetch204();
175 await deleteClassBlueprintByGrade("9/A");
176 const [url, init] = lastFetchCall();
177 expect(url).toBe("/api/classes/blueprints/9%2FA");
178 expect(init?.method).toBe("DELETE");
179 });
180
181 it("postClassBlueprintApply POSTs /classes/:id/blueprint with no body", async () => {
182 mockFetchSuccess({ skipped: [] });
183 await postClassBlueprintApply("c1");
184 const [url, init] = lastFetchCall();
185 expect(url).toBe("/api/classes/c1/blueprint");
186 expect(init?.method).toBe("POST");
187 expect(init?.body).toBeUndefined();
188 });
189
190 it("getClassBlueprintStatus GETs /classes/blueprints/:grade/status", async () => {
191 const mockStatus = { grade: "9/A", courses: ["co1"], matched: 3, sections: [] };
192 mockFetchSuccess(mockStatus);
193 const result = await getClassBlueprintStatus("9/A");
194 expect(result).toEqual(mockStatus);
195 const [url, init] = lastFetchCall();
196 expect(url).toBe("/api/classes/blueprints/9%2FA/status");
197 expect(init?.method).toBe("GET");
198 });
199});