forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

users.test.ts255 lines, 8.9 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getMe } from "../../users";
3import { patchMe } from "../../users";
4import { getUsers } from "../../users";
5import { getUserById } from "../../users";
6import { getUserSearch } from "../../users";
7import { patchUserProfile } from "../../users";
8import { patchUserRole } from "../../users";
9import { patchMyPreferences } from "../../users";
10import { patchUserPreferences } from "../../users";
11import { getMyProfile } from "../../users";
12import { getUserProfile } from "../../users";
13import { postMyAvatar } from "../../users";
14import { deleteMyAvatar } from "../../users";
15import { deleteUserAvatar } from "../../users";
16import { getUserAvatarUrl } from "../../users";
17import { postUser } from "../../users";
18import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
19
20describe("users API", () => {
21 afterEach(() => {
22 vi.restoreAllMocks();
23 });
24
25 it("getMe calls /users/me", async () => {
26 const mockUser = { id: "u1", username: "test" };
27 mockFetchSuccess(mockUser);
28
29 const result = await getMe();
30 expect(result).toEqual(mockUser);
31
32 const [url, init] = lastFetchCall();
33 expect(url).toBe("/api/auth/me");
34 expect(init?.method).toBe("GET");
35 });
36
37 it("patchMe calls /users/me with updates", async () => {
38 const mockUser = { id: "u1", username: "test" };
39 mockFetchSuccess(mockUser);
40
41 const updates = { name: "New Name" };
42 const result = await patchMe(updates);
43 expect(result).toEqual(mockUser);
44
45 const [url, init] = lastFetchCall();
46 expect(url).toBe("/api/users/me");
47 expect(init?.method).toBe("PATCH");
48 expect(init?.body).toBe(JSON.stringify(updates));
49 });
50
51 it("getUsers calls /users with pagination", async () => {
52 const mockPage = { items: [{ id: "u1" }], total: 1 };
53 mockFetchSuccess(mockPage);
54
55 const result = await getUsers({ limit: 10, offset: 20 });
56 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
57
58 const [url, init] = lastFetchCall();
59 expect(url).toBe("/api/users?limit=10&offset=20");
60 expect(init?.method).toBe("GET");
61 });
62
63 it("getUserSearch calls /users/search with query, role, and pagination", async () => {
64 const mockPage = { items: [{ id: "u1" }], total: 1 };
65 mockFetchSuccess(mockPage);
66
67 const result = await getUserSearch("john", undefined, "student", { limit: 5 });
68 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
69
70 const [url, init] = lastFetchCall();
71 expect(url).toBe("/api/users/search?q=john&role=student&limit=5");
72 expect(init?.method).toBe("GET");
73 });
74
75 it("getUserById calls /users/:id", async () => {
76 const mockUser = { id: "u1", username: "test" };
77 mockFetchSuccess(mockUser);
78
79 const result = await getUserById("u1");
80 expect(result).toEqual(mockUser);
81
82 const [url, init] = lastFetchCall();
83 expect(url).toBe("/api/users/u1");
84 expect(init?.method).toBe("GET");
85 });
86
87 it("patchUserProfile calls /users/:id/profile", async () => {
88 const mockUser = { id: "u1", name: "Updated" };
89 mockFetchSuccess(mockUser);
90
91 const updates = { name: "Updated" };
92 const result = await patchUserProfile("u1", updates);
93 expect(result).toEqual(mockUser);
94
95 const [url, init] = lastFetchCall();
96 expect(url).toBe("/api/users/u1/profile");
97 expect(init?.method).toBe("PATCH");
98 expect(init?.body).toBe(JSON.stringify(updates));
99 });
100
101 it("patchUserRole calls /users/:id/role", async () => {
102 const mockUser = { id: "u1", role: "teacher" };
103 mockFetchSuccess(mockUser);
104
105 const result = await patchUserRole("u1", "teacher");
106 expect(result).toEqual(mockUser);
107
108 const [url, init] = lastFetchCall();
109 expect(url).toBe("/api/users/u1/role");
110 expect(init?.method).toBe("PATCH");
111 expect(init?.body).toBe(JSON.stringify({ role: "teacher" }));
112 });
113
114 it("patchMyPreferences calls /users/me/preferences", async () => {
115 const mockUser = { id: "u1", theme: "dark" };
116 mockFetchSuccess(mockUser);
117
118 const updates = { theme: "dark" as const };
119 const result = await patchMyPreferences(updates);
120 expect(result).toEqual(mockUser);
121
122 const [url, init] = lastFetchCall();
123 expect(url).toBe("/api/users/me/preferences");
124 expect(init?.method).toBe("PATCH");
125 expect(init?.body).toBe(JSON.stringify(updates));
126 });
127
128 it("patchMyPreferences sends palette_color to set the accent", async () => {
129 const mockUser = { id: "u1", palette_color: "#fefae0" };
130 mockFetchSuccess(mockUser);
131
132 const updates = { palette_color: "#fefae0" };
133 await patchMyPreferences(updates);
134
135 const [url, init] = lastFetchCall();
136 expect(url).toBe("/api/users/me/preferences");
137 expect(init?.method).toBe("PATCH");
138 expect(init?.body).toBe(JSON.stringify(updates));
139 });
140
141 it("patchMyPreferences sends empty palette_color to clear the accent", async () => {
142 const mockUser = { id: "u1", palette_color: null };
143 mockFetchSuccess(mockUser);
144
145 const updates = { palette_color: "" };
146 await patchMyPreferences(updates);
147
148 const [, init] = lastFetchCall();
149 expect(init?.body).toBe(JSON.stringify({ palette_color: "" }));
150 });
151
152 it("patchUserPreferences calls /users/:id/preferences", async () => {
153 const mockUser = { id: "u1", language: "tr" };
154 mockFetchSuccess(mockUser);
155
156 const updates = { language: "tr" as const };
157 const result = await patchUserPreferences("u1", updates);
158 expect(result).toEqual(mockUser);
159
160 const [url, init] = lastFetchCall();
161 expect(url).toBe("/api/users/u1/preferences");
162 expect(init?.method).toBe("PATCH");
163 expect(init?.body).toBe(JSON.stringify(updates));
164 });
165
166 it("getMyProfile calls /users/me/profile", async () => {
167 const profile = { id: "u1", username: "test", badges: [] };
168 mockFetchSuccess(profile);
169
170 const result = await getMyProfile();
171 expect(result).toEqual(profile);
172 const [url] = lastFetchCall();
173 expect(url).toBe("/api/users/me/profile");
174 });
175
176 it("getUserProfile calls /users/:id/profile", async () => {
177 mockFetchSuccess({ id: "u2", username: "other", badges: [] });
178 await getUserProfile("u2");
179 const [url] = lastFetchCall();
180 expect(url).toBe("/api/users/u2/profile");
181 });
182
183 it("postMyAvatar POSTs the file as multipart under the `file` field", async () => {
184 mockFetchSuccess({ content_type: "image/png", size: 1024 }, 201);
185
186 const file = new File(["x"], "me.png", { type: "image/png" });
187 const result = await postMyAvatar(file);
188 expect(result).toEqual({ content_type: "image/png", size: 1024 });
189
190 const [url, init] = lastFetchCall();
191 expect(url).toBe("/api/users/me/avatar");
192 expect(init?.method).toBe("POST");
193 expect(init?.body).toBeInstanceOf(FormData);
194 expect((init?.body as FormData).get("file")).toBe(file);
195 });
196
197 it("deleteMyAvatar calls DELETE /users/me/avatar", async () => {
198 mockFetch204();
199 await deleteMyAvatar();
200 const [url, init] = lastFetchCall();
201 expect(url).toBe("/api/users/me/avatar");
202 expect(init?.method).toBe("DELETE");
203 });
204
205 it("deleteUserAvatar calls DELETE /users/:id/avatar", async () => {
206 mockFetch204();
207 await deleteUserAvatar("u2");
208 const [url, init] = lastFetchCall();
209 expect(url).toBe("/api/users/u2/avatar");
210 expect(init?.method).toBe("DELETE");
211 });
212
213 it("getUserAvatarUrl builds a same-origin URL, with the cache-buster only when versioned", () => {
214 expect(getUserAvatarUrl("u1")).toBe("/api/users/u1/avatar");
215 expect(getUserAvatarUrl("u1", 3)).toBe("/api/users/u1/avatar?v=3");
216 expect(getUserAvatarUrl("u/1")).toBe("/api/users/u%2F1/avatar");
217 });
218 it("postUser posts the new account to /users", async () => {
219 const mockUser = { id: "u9", username: "ayse", role: "teacher" };
220 mockFetchSuccess(mockUser);
221
222 const body = { username: "ayse", password: "secret123", role: "teacher" as const };
223 const result = await postUser(body);
224 expect(result).toEqual(mockUser);
225
226 const [url, init] = lastFetchCall();
227 expect(url).toBe("/api/users");
228 expect(init?.method).toBe("POST");
229 expect(JSON.parse(init?.body as string)).toEqual(body);
230 });
231
232 it("postUser carries the school-issued student number", async () => {
233 mockFetchSuccess({ id: "u9", username: "ayse", role: "student", student_number: "1234" });
234
235 const body = { username: "ayse", password: "secret123", role: "student" as const, student_number: "1234" };
236 await postUser(body);
237
238 const [url, init] = lastFetchCall();
239 expect(url).toBe("/api/users");
240 expect(init?.method).toBe("POST");
241 expect(JSON.parse(init?.body as string)).toEqual(body);
242 });
243
244 it("patchUserProfile sends an empty student number to clear it", async () => {
245 mockFetchSuccess({ id: "u1", student_number: null });
246
247 const updates = { student_number: "" };
248 await patchUserProfile("u1", updates);
249
250 const [url, init] = lastFetchCall();
251 expect(url).toBe("/api/users/u1/profile");
252 expect(init?.method).toBe("PATCH");
253 expect(JSON.parse(init?.body as string)).toEqual(updates);
254 });
255});