|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | +import { UserAvatar } from "./user-avatar"; |
| 4 | + |
| 5 | +describe("UserAvatar", () => { |
| 6 | + it("falls back to the user icon when no image is available", () => { |
| 7 | + const { container } = render(<UserAvatar />); |
| 8 | + const icon = container.querySelector('[data-slot="avatar-fallback"] svg'); |
| 9 | + expect(icon).toBeInTheDocument(); |
| 10 | + }); |
| 11 | + |
| 12 | + it("hides the fallback icon from assistive tech", () => { |
| 13 | + // The control wrapping the avatar carries the accessible name, so the icon |
| 14 | + // must not announce a second one. |
| 15 | + const { container } = render(<UserAvatar />); |
| 16 | + const icon = container.querySelector('[data-slot="avatar-fallback"] svg')!; |
| 17 | + expect(icon).toHaveAttribute("aria-hidden", "true"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("colors the icon from theme tokens rather than fixed values", () => { |
| 21 | + const { container } = render(<UserAvatar />); |
| 22 | + const fallback = container.querySelector('[data-slot="avatar-fallback"]')!; |
| 23 | + expect(fallback).toHaveClass("bg-muted", "text-muted-foreground"); |
| 24 | + // A hardcoded or dark:-prefixed color would defeat the token indirection. |
| 25 | + expect(fallback.className).not.toMatch(/dark:/); |
| 26 | + }); |
| 27 | + |
| 28 | + it("sizes to the 24px header slot by default", () => { |
| 29 | + const { container } = render(<UserAvatar />); |
| 30 | + expect(container.querySelector('[data-slot="avatar"]')).toHaveClass("size-6", "rounded-md"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("accepts a className override", () => { |
| 34 | + const { container } = render(<UserAvatar className="size-10" />); |
| 35 | + const root = container.querySelector('[data-slot="avatar"]')!; |
| 36 | + expect(root).toHaveClass("size-10"); |
| 37 | + expect(root).not.toHaveClass("size-6"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("mounts the image slot only when a src is supplied", () => { |
| 41 | + // jsdom never resolves the image load, so Radix keeps the fallback visible |
| 42 | + // and withholds the <img>. Asserting on the mounted-vs-absent Image child |
| 43 | + // is not possible here; the meaningful check is that passing a src neither |
| 44 | + // crashes nor removes the fallback, so there is never an empty frame. |
| 45 | + const { container } = render(<UserAvatar src="https://example.com/avatar.png" alt="Ada" />); |
| 46 | + expect(container.querySelector('[data-slot="avatar-fallback"] svg')).toBeInTheDocument(); |
| 47 | + }); |
| 48 | +}); |
0 commit comments