/** * @file Sidebar.account.test.tsx * @description The sidebar's account row (server mode): the signed-in user's * avatar — or initials without one — name and role, linking to Settings → * Account, beside Sign out; local mode shows no row. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, vi } from "vitest"; import { render, screen, within } from "@testing-library/react"; import { MemoryRouter } from "react-router-dom"; const auth = vi.hoisted(() => ({ state: { mode: "local" } as Record, logout: vi.fn(), updateUser: vi.fn(), })); vi.mock("../AuthGate", () => ({ useAuth: () => auth, usePermissions: () => ({ isAdmin: auth.state.mode !== "server", local: auth.state.mode !== "server", seesAll: true, canEditLane: () => true, }), })); import { Sidebar } from "../Sidebar"; const renderSidebar = (collapsed = false) => render( {}} connectionOpen={false} onConnectionOpenChange={() => {}} /> ); describe("Sidebar account row", () => { it("shows the avatar, name and role, linking to Settings → Account", () => { auth.state = { mode: "server", user: { id: 2, username: "mia", role: "developer", displayName: "Mia Tran", avatarAt: "t1" }, }; renderSidebar(); const row = screen.getByTestId("sidebar-account"); expect(row.textContent).toContain("Mia Tran"); expect(row.textContent).toContain("Developer"); expect(within(row).getByTestId("user-avatar-img").getAttribute("src")).toBe( "/api/me/avatar?v=t1" ); expect(within(row).getAllByRole("link")[0]?.getAttribute("href")).toBe("/settings/account"); expect(within(row).getByRole("button", { name: "Sign out" })).toBeTruthy(); }); it("falls back to initials, and keeps only the avatar on the rail", () => { auth.state = { mode: "server", user: { id: 3, username: "leo", role: "admin" } }; renderSidebar(true); const row = screen.getByTestId("sidebar-account"); expect(within(row).getByTestId("user-avatar-initials").textContent).toBe("le"); expect(row.textContent).not.toContain("Admin"); }); it("has no account row in local mode", () => { auth.state = { mode: "local" }; renderSidebar(); expect(screen.queryByTestId("sidebar-account")).toBeNull(); }); });