forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

users.test.ts158 lines, 5.3 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 { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch";
12
13describe("users API", () => {
14 afterEach(() => {
15 vi.restoreAllMocks();
16 });
17
18 it("getMe calls /users/me", async () => {
19 const mockUser = { id: "u1", username: "test" };
20 mockFetchSuccess(mockUser);
21
22 const result = await getMe();
23 expect(result).toEqual(mockUser);
24
25 const [url, init] = lastFetchCall();
26 expect(url).toBe("/api/auth/me");
27 expect(init?.method).toBe("GET");
28 });
29
30 it("patchMe calls /users/me with updates", async () => {
31 const mockUser = { id: "u1", username: "test" };
32 mockFetchSuccess(mockUser);
33
34 const updates = { name: "New Name" };
35 const result = await patchMe(updates);
36 expect(result).toEqual(mockUser);
37
38 const [url, init] = lastFetchCall();
39 expect(url).toBe("/api/users/me");
40 expect(init?.method).toBe("PATCH");
41 expect(init?.body).toBe(JSON.stringify(updates));
42 });
43
44 it("getUsers calls /users with pagination", async () => {
45 const mockPage = { items: [{ id: "u1" }], total: 1 };
46 mockFetchSuccess(mockPage);
47
48 const result = await getUsers({ limit: 10, offset: 20 });
49 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
50
51 const [url, init] = lastFetchCall();
52 expect(url).toBe("/api/users?limit=10&offset=20");
53 expect(init?.method).toBe("GET");
54 });
55
56 it("getUserSearch calls /users/search with query, role, and pagination", async () => {
57 const mockPage = { items: [{ id: "u1" }], total: 1 };
58 mockFetchSuccess(mockPage);
59
60 const result = await getUserSearch("john", undefined, "student", { limit: 5 });
61 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); // Normalized
62
63 const [url, init] = lastFetchCall();
64 expect(url).toBe("/api/users/search?q=john&role=student&limit=5");
65 expect(init?.method).toBe("GET");
66 });
67
68 it("getUserById calls /users/:id", async () => {
69 const mockUser = { id: "u1", username: "test" };
70 mockFetchSuccess(mockUser);
71
72 const result = await getUserById("u1");
73 expect(result).toEqual(mockUser);
74
75 const [url, init] = lastFetchCall();
76 expect(url).toBe("/api/users/u1");
77 expect(init?.method).toBe("GET");
78 });
79
80 it("patchUserProfile calls /users/:id/profile", async () => {
81 const mockUser = { id: "u1", name: "Updated" };
82 mockFetchSuccess(mockUser);
83
84 const updates = { name: "Updated" };
85 const result = await patchUserProfile("u1", updates);
86 expect(result).toEqual(mockUser);
87
88 const [url, init] = lastFetchCall();
89 expect(url).toBe("/api/users/u1/profile");
90 expect(init?.method).toBe("PATCH");
91 expect(init?.body).toBe(JSON.stringify(updates));
92 });
93
94 it("patchUserRole calls /users/:id/role", async () => {
95 const mockUser = { id: "u1", role: "teacher" };
96 mockFetchSuccess(mockUser);
97
98 const result = await patchUserRole("u1", "teacher");
99 expect(result).toEqual(mockUser);
100
101 const [url, init] = lastFetchCall();
102 expect(url).toBe("/api/users/u1/role");
103 expect(init?.method).toBe("PATCH");
104 expect(init?.body).toBe(JSON.stringify({ role: "teacher" }));
105 });
106
107 it("patchMyPreferences calls /users/me/preferences", async () => {
108 const mockUser = { id: "u1", theme: "dark" };
109 mockFetchSuccess(mockUser);
110
111 const updates = { theme: "dark" as const };
112 const result = await patchMyPreferences(updates);
113 expect(result).toEqual(mockUser);
114
115 const [url, init] = lastFetchCall();
116 expect(url).toBe("/api/users/me/preferences");
117 expect(init?.method).toBe("PATCH");
118 expect(init?.body).toBe(JSON.stringify(updates));
119 });
120
121 it("patchMyPreferences sends palette_color to set the accent", async () => {
122 const mockUser = { id: "u1", palette_color: "#fefae0" };
123 mockFetchSuccess(mockUser);
124
125 const updates = { palette_color: "#fefae0" };
126 await patchMyPreferences(updates);
127
128 const [url, init] = lastFetchCall();
129 expect(url).toBe("/api/users/me/preferences");
130 expect(init?.method).toBe("PATCH");
131 expect(init?.body).toBe(JSON.stringify(updates));
132 });
133
134 it("patchMyPreferences sends empty palette_color to clear the accent", async () => {
135 const mockUser = { id: "u1", palette_color: null };
136 mockFetchSuccess(mockUser);
137
138 const updates = { palette_color: "" };
139 await patchMyPreferences(updates);
140
141 const [, init] = lastFetchCall();
142 expect(init?.body).toBe(JSON.stringify({ palette_color: "" }));
143 });
144
145 it("patchUserPreferences calls /users/:id/preferences", async () => {
146 const mockUser = { id: "u1", language: "tr" };
147 mockFetchSuccess(mockUser);
148
149 const updates = { language: "tr" as const };
150 const result = await patchUserPreferences("u1", updates);
151 expect(result).toEqual(mockUser);
152
153 const [url, init] = lastFetchCall();
154 expect(url).toBe("/api/users/u1/preferences");
155 expect(init?.method).toBe("PATCH");
156 expect(init?.body).toBe(JSON.stringify(updates));
157 });
158});