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