forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

schools.test.ts142 lines, 4.7 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { deleteSchool } from "../../schools";
3import { deleteSchoolModule } from "../../schools";
4import { getSchool } from "../../schools";
5import { getSchoolModules } from "../../schools";
6import { getSchools } from "../../schools";
7import { patchSchool } from "../../schools";
8import { patchSchoolModules } from "../../schools";
9import { postSchool } from "../../schools";
10import { postSchoolAdminPassword } from "../../schools";
11import { postSchoolEnter } from "../../schools";
12import { postSchoolModule } from "../../schools";
13import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
14
15const schoolId = "019732e3-7b00-7000-8000-00000000dead";
16const school = { id: schoolId, name: "Ata Koleji", status: "active", created_at: 1, modules: ["courses"] };
17const modules = { enabled: ["courses"], disabled: ["exams"] };
18
19describe("schools API", () => {
20 afterEach(() => {
21 vi.restoreAllMocks();
22 });
23
24 it("getSchools pages /schools", async () => {
25 mockFetchSuccess({ items: [school], total: 1, limit: 20, offset: 0 });
26
27 const result = await getSchools({ limit: 20, offset: 0 });
28 expect(result.items).toEqual([school]);
29
30 const [url, init] = lastFetchCall();
31 expect(url).toBe("/api/schools?limit=20&offset=0");
32 expect(init?.method).toBe("GET");
33 });
34
35 it("getSchool reads /schools/{id}", async () => {
36 mockFetchSuccess(school);
37
38 await getSchool(schoolId);
39
40 const [url, init] = lastFetchCall();
41 expect(url).toBe(`/api/schools/${schoolId}`);
42 expect(init?.method).toBe("GET");
43 });
44
45 it("postSchool posts the school and its first admin", async () => {
46 mockFetchSuccess(school, 201);
47 const body = { name: "Ata Koleji", admin_username: "admin", admin_password: "secret123", modules: ["courses"] };
48
49 await postSchool(body);
50
51 const [url, init] = lastFetchCall();
52 expect(url).toBe("/api/schools");
53 expect(init?.method).toBe("POST");
54 expect(init?.body).toBe(JSON.stringify(body));
55 });
56
57 it("patchSchool patches name and status", async () => {
58 mockFetchSuccess({ ...school, status: "suspended" });
59
60 await patchSchool(schoolId, { status: "suspended" });
61
62 const [url, init] = lastFetchCall();
63 expect(url).toBe(`/api/schools/${schoolId}`);
64 expect(init?.method).toBe("PATCH");
65 expect(init?.body).toBe(JSON.stringify({ status: "suspended" }));
66 });
67
68 it("deleteSchool deletes /schools/{id}", async () => {
69 mockFetch204();
70
71 await deleteSchool(schoolId);
72
73 const [url, init] = lastFetchCall();
74 expect(url).toBe(`/api/schools/${schoolId}`);
75 expect(init?.method).toBe("DELETE");
76 });
77
78 it("postSchoolAdminPassword re-keys an admin", async () => {
79 mockFetch204();
80
81 await postSchoolAdminPassword(schoolId, { username: "admin", password: "newsecret" });
82
83 const [url, init] = lastFetchCall();
84 expect(url).toBe(`/api/schools/${schoolId}/admin-password`);
85 expect(init?.method).toBe("POST");
86 expect(init?.body).toBe(JSON.stringify({ username: "admin", password: "newsecret" }));
87 });
88
89 it("postSchoolEnter enters as a named admin", async () => {
90 mockFetchSuccess({ id: "u1", username: "admin", role: "admin" });
91
92 await postSchoolEnter(schoolId, { username: "admin" });
93
94 const [url, init] = lastFetchCall();
95 expect(url).toBe(`/api/schools/${schoolId}/enter`);
96 expect(init?.method).toBe("POST");
97 expect(init?.body).toBe(JSON.stringify({ username: "admin" }));
98 });
99
100 it("getSchoolModules reads both halves of the catalog", async () => {
101 mockFetchSuccess(modules);
102
103 const result = await getSchoolModules(schoolId);
104 expect(result).toEqual(modules);
105
106 const [url, init] = lastFetchCall();
107 expect(url).toBe(`/api/schools/${schoolId}/modules`);
108 expect(init?.method).toBe("GET");
109 });
110
111 it("patchSchoolModules re-sells the shelf in one write", async () => {
112 mockFetchSuccess(modules);
113 const body = { enable: ["exams"], disable_packages: ["ai"] };
114
115 await patchSchoolModules(schoolId, body);
116
117 const [url, init] = lastFetchCall();
118 expect(url).toBe(`/api/schools/${schoolId}/modules`);
119 expect(init?.method).toBe("PATCH");
120 expect(init?.body).toBe(JSON.stringify(body));
121 });
122
123 it("postSchoolModule enables one module", async () => {
124 mockFetchSuccess(modules);
125
126 await postSchoolModule(schoolId, "exams");
127
128 const [url, init] = lastFetchCall();
129 expect(url).toBe(`/api/schools/${schoolId}/modules/exams`);
130 expect(init?.method).toBe("POST");
131 });
132
133 it("deleteSchoolModule disables one module", async () => {
134 mockFetchSuccess(modules);
135
136 await deleteSchoolModule(schoolId, "exams");
137
138 const [url, init] = lastFetchCall();
139 expect(url).toBe(`/api/schools/${schoolId}/modules/exams`);
140 expect(init?.method).toBe("DELETE");
141 });
142});