|
| 1 | +import { screen, waitFor } from "@testing-library/react" |
| 2 | +import userEvent from "@testing-library/user-event" |
| 3 | +import { http, HttpResponse } from "msw" |
| 4 | +import { describe, expect, it, vi } from "vitest" |
| 5 | +import { renderWithFileRoutes } from "@/test/renderers" |
| 6 | +import { server } from "@/test/setup" |
| 7 | + |
| 8 | +vi.mock("sonner", async (importOriginal) => { |
| 9 | + const actual = await importOriginal<typeof import("sonner")>() |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + toast: { |
| 13 | + ...actual.toast, |
| 14 | + success: vi.fn(), |
| 15 | + error: vi.fn(), |
| 16 | + }, |
| 17 | + } |
| 18 | +}) |
| 19 | + |
| 20 | +import { toast } from "sonner" |
| 21 | + |
| 22 | +type AuthMeBody = { |
| 23 | + id: string |
| 24 | + email: string |
| 25 | + display_name: string | null |
| 26 | + avatar_url: string | null |
| 27 | + tos_accepted_at: string | null |
| 28 | +} |
| 29 | + |
| 30 | +const consentsHandler = http.get("*/user/consents", () => |
| 31 | + HttpResponse.json({ current_policy_version: "test", consents: {} }) |
| 32 | +) |
| 33 | + |
| 34 | +function authMeHandler(initial: AuthMeBody) { |
| 35 | + let current = initial |
| 36 | + return { |
| 37 | + setBody(next: AuthMeBody) { |
| 38 | + current = next |
| 39 | + }, |
| 40 | + handler: http.get("*/auth/me", () => HttpResponse.json(current)), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +describe("ProfilePage display name", () => { |
| 45 | + it("pre-fills the input with the current display_name", async () => { |
| 46 | + const me = authMeHandler({ |
| 47 | + id: "u-1", |
| 48 | + email: "player@example.com", |
| 49 | + display_name: "ZeroEmpires", |
| 50 | + avatar_url: null, |
| 51 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 52 | + }) |
| 53 | + server.use(me.handler, consentsHandler) |
| 54 | + |
| 55 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 56 | + |
| 57 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 58 | + await waitFor(() => expect(input.value).toBe("ZeroEmpires")) |
| 59 | + }) |
| 60 | + |
| 61 | + it("renders an empty input when display_name is unset", async () => { |
| 62 | + const me = authMeHandler({ |
| 63 | + id: "u-1", |
| 64 | + email: "player@example.com", |
| 65 | + display_name: null, |
| 66 | + avatar_url: null, |
| 67 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 68 | + }) |
| 69 | + server.use(me.handler, consentsHandler) |
| 70 | + |
| 71 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 72 | + |
| 73 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 74 | + await waitFor(() => expect(input.value).toBe("")) |
| 75 | + }) |
| 76 | + |
| 77 | + it("PATCHes /auth/me with the trimmed value and toasts on success", async () => { |
| 78 | + const me = authMeHandler({ |
| 79 | + id: "u-1", |
| 80 | + email: "player@example.com", |
| 81 | + display_name: null, |
| 82 | + avatar_url: null, |
| 83 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 84 | + }) |
| 85 | + let captured: { display_name?: unknown } | null = null |
| 86 | + server.use( |
| 87 | + me.handler, |
| 88 | + consentsHandler, |
| 89 | + http.patch("*/auth/me", async ({ request }) => { |
| 90 | + captured = (await request.json()) as { display_name?: unknown } |
| 91 | + me.setBody({ |
| 92 | + id: "u-1", |
| 93 | + email: "player@example.com", |
| 94 | + display_name: "ZeroEmpires", |
| 95 | + avatar_url: null, |
| 96 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 97 | + }) |
| 98 | + return HttpResponse.json({ ok: true }) |
| 99 | + }) |
| 100 | + ) |
| 101 | + |
| 102 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 103 | + |
| 104 | + const user = userEvent.setup() |
| 105 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 106 | + await user.type(input, " ZeroEmpires ") |
| 107 | + await user.click(screen.getByRole("button", { name: /save display name/i })) |
| 108 | + |
| 109 | + await waitFor(() => expect(captured).not.toBeNull()) |
| 110 | + expect(captured).toEqual({ display_name: "ZeroEmpires" }) |
| 111 | + await waitFor(() => |
| 112 | + expect(toast.success).toHaveBeenCalledWith("Display name updated.") |
| 113 | + ) |
| 114 | + }) |
| 115 | + |
| 116 | + it("sends an empty string when the user clears the field", async () => { |
| 117 | + const me = authMeHandler({ |
| 118 | + id: "u-1", |
| 119 | + email: "player@example.com", |
| 120 | + display_name: "ZeroEmpires", |
| 121 | + avatar_url: null, |
| 122 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 123 | + }) |
| 124 | + let captured: { display_name?: unknown } | null = null |
| 125 | + server.use( |
| 126 | + me.handler, |
| 127 | + consentsHandler, |
| 128 | + http.patch("*/auth/me", async ({ request }) => { |
| 129 | + captured = (await request.json()) as { display_name?: unknown } |
| 130 | + me.setBody({ |
| 131 | + id: "u-1", |
| 132 | + email: "player@example.com", |
| 133 | + display_name: null, |
| 134 | + avatar_url: null, |
| 135 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 136 | + }) |
| 137 | + return HttpResponse.json({ ok: true }) |
| 138 | + }) |
| 139 | + ) |
| 140 | + |
| 141 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 142 | + |
| 143 | + const user = userEvent.setup() |
| 144 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 145 | + await waitFor(() => expect(input.value).toBe("ZeroEmpires")) |
| 146 | + |
| 147 | + await user.clear(input) |
| 148 | + await user.click( |
| 149 | + screen.getByRole("button", { name: /clear display name/i }) |
| 150 | + ) |
| 151 | + |
| 152 | + await waitFor(() => expect(captured).not.toBeNull()) |
| 153 | + expect(captured).toEqual({ display_name: "" }) |
| 154 | + await waitFor(() => |
| 155 | + expect(toast.success).toHaveBeenCalledWith("Display name cleared.") |
| 156 | + ) |
| 157 | + }) |
| 158 | + |
| 159 | + it("surfaces backend 422 messages as an error toast", async () => { |
| 160 | + const me = authMeHandler({ |
| 161 | + id: "u-1", |
| 162 | + email: "player@example.com", |
| 163 | + display_name: null, |
| 164 | + avatar_url: null, |
| 165 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 166 | + }) |
| 167 | + server.use( |
| 168 | + me.handler, |
| 169 | + consentsHandler, |
| 170 | + http.patch("*/auth/me", () => |
| 171 | + HttpResponse.json( |
| 172 | + { detail: "Display name is too long." }, |
| 173 | + { status: 422 } |
| 174 | + ) |
| 175 | + ) |
| 176 | + ) |
| 177 | + |
| 178 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 179 | + |
| 180 | + const user = userEvent.setup() |
| 181 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 182 | + await user.type(input, "anything") |
| 183 | + await user.click(screen.getByRole("button", { name: /save display name/i })) |
| 184 | + |
| 185 | + await waitFor(() => |
| 186 | + expect(toast.error).toHaveBeenCalledWith("Display name is too long.") |
| 187 | + ) |
| 188 | + }) |
| 189 | + |
| 190 | + it("disables the save button when the trimmed value equals the current name", async () => { |
| 191 | + const me = authMeHandler({ |
| 192 | + id: "u-1", |
| 193 | + email: "player@example.com", |
| 194 | + display_name: "ZeroEmpires", |
| 195 | + avatar_url: null, |
| 196 | + tos_accepted_at: "2026-01-01T00:00:00Z", |
| 197 | + }) |
| 198 | + server.use(me.handler, consentsHandler) |
| 199 | + |
| 200 | + await renderWithFileRoutes(<></>, { initialLocation: "/profile" }) |
| 201 | + |
| 202 | + const input = await screen.findByLabelText<HTMLInputElement>("Display name") |
| 203 | + await waitFor(() => expect(input.value).toBe("ZeroEmpires")) |
| 204 | + |
| 205 | + const button = screen.getByRole("button", { name: /save display name/i }) |
| 206 | + expect(button).toBeDisabled() |
| 207 | + |
| 208 | + const user = userEvent.setup() |
| 209 | + await user.type(input, " ") |
| 210 | + expect(button).toBeDisabled() |
| 211 | + }) |
| 212 | +}) |
0 commit comments