diff --git a/apps/web/__tests__/unit/dashboard-avatar.test.ts b/apps/web/__tests__/unit/dashboard-avatar.test.ts new file mode 100644 index 0000000000..aa8803da2c --- /dev/null +++ b/apps/web/__tests__/unit/dashboard-avatar.test.ts @@ -0,0 +1,42 @@ +// @vitest-environment jsdom + +import { act, createElement } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { Avatar } from "@/app/(org)/dashboard/_components/Avatar"; + +describe("dashboard avatar initial", () => { + let root: Root; + let container: HTMLDivElement; + + beforeEach(() => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + container = document.createElement("div"); + document.body.append(container); + root = createRoot(container); + }); + + afterEach(async () => { + await act(async () => root.unmount()); + container.remove(); + vi.unstubAllGlobals(); + }); + + const renderInitial = async (name: string | null) => { + await act(async () => root.render(createElement(Avatar, { name }))); + return container.querySelector("span")?.textContent; + }; + + it("shows the whole emoji for names starting with an emoji", async () => { + expect(await renderInitial("\u{1F600} Team")).toBe("\u{1F600}"); + }); + + it("ignores leading whitespace", async () => { + expect(await renderInitial(" bob")).toBe("B"); + }); + + it("falls back to A for empty or missing names", async () => { + expect(await renderInitial("")).toBe("A"); + expect(await renderInitial(null)).toBe("A"); + }); +}); diff --git a/apps/web/app/(org)/dashboard/_components/Avatar.tsx b/apps/web/app/(org)/dashboard/_components/Avatar.tsx index b5e869631a..da76a78d7a 100644 --- a/apps/web/app/(org)/dashboard/_components/Avatar.tsx +++ b/apps/web/app/(org)/dashboard/_components/Avatar.tsx @@ -67,8 +67,8 @@ export const Avatar: React.FC<{ className?: string; letterClass?: string; }> = ({ name, className = "", letterClass = "text-xs" }) => { - const initial = name?.[0]?.toUpperCase() || "A"; - const charCode = initial.charCodeAt(0); + const initial = Array.from(name?.trim() ?? "")[0]?.toUpperCase() || "A"; + const charCode = initial.codePointAt(0) ?? 0; const isAlpha = charCode >= 65 && charCode <= 90; const colorIndex = isAlpha ? charCode - 65 : 25; // fallback to last color for non-alpha const bgColor = avatarDefaultPalette[colorIndex];