Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions apps/web/__tests__/unit/dashboard-avatar.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
4 changes: 2 additions & 2 deletions apps/web/app/(org)/dashboard/_components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading