import { describe, it, expect, vi, afterEach } from "vitest"; import { mount } from "@vue/test-utils"; import { createCommentVNode, defineComponent, h, nextTick, ref } from "vue"; import { readFileSync } from "node:fs"; import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage, CloseButton, Skeleton, } from "../packages/vue/src"; import CloseButtonInteractive from "../apps/docs/demos/close-button-interactive.vue"; import AvatarGroupMaxDemo from "../apps/docs/demos/avatar-group-max.vue"; import AvatarGroupCountDemo from "../apps/docs/demos/avatar-group-count.vue"; import SkeletonSingleShimmer from "../apps/docs/demos/skeleton-single-shimmer.vue"; afterEach(() => { vi.useRealTimers(); document.body.innerHTML = ""; }); describe("CloseButton", () => { it("renders the baseline close icon and Close aria-label", () => { const w = mount(CloseButton); const button = w.get("button"); expect(button.attributes("type")).toBe("button"); expect(button.attributes("aria-label")).toBe("Close"); expect(button.attributes("data-slot")).toBe("close-button"); expect(button.classes()).toContain("hv-close-button"); expect(button.classes()).toContain("hv-close-button--default"); expect(w.find("[data-slot=close-button-icon]").exists()).toBe(true); }); it("lets callers override the aria-label and replace the icon", () => { const w = mount(CloseButton, { attrs: { "aria-label": "关闭" }, slots: { default: () => h("span", { "data-icon": "custom" }, "×") }, }); expect(w.get("button").attributes("aria-label")).toBe("关闭"); expect(w.get("[data-icon=custom]").text()).toBe("×"); expect(w.find("[data-slot=close-button-icon]").exists()).toBe(false); }); it("blocks activation when disabled and restores hover state", async () => { const clicks = vi.fn(); const w = mount(CloseButton, { props: { isDisabled: true }, attrs: { onClick: clicks }, }); const button = w.get("button"); expect(button.attributes("disabled")).toBeDefined(); expect(button.attributes("aria-disabled")).toBe("true"); await button.trigger("click"); expect(clicks).not.toHaveBeenCalled(); await button.trigger("pointerenter"); expect(button.attributes("data-hovered")).toBeUndefined(); }); it("counts clicks in the interactive demo", async () => { const w = mount(CloseButtonInteractive, { attachTo: document.body }); const button = w.get("button"); expect(w.text()).toContain("已点击:0 次"); await button.trigger("click"); await button.trigger("click"); expect(w.text()).toContain("已点击:2 次"); expect(button.attributes("aria-label")).toContain("2 次"); w.unmount(); }); it("marks hover, pressed and focus-visible through data attributes", async () => { const w = mount(CloseButton, { attachTo: document.body }); const button = w.get("button"); await button.trigger("pointerenter"); expect(button.attributes("data-hovered")).toBe("true"); await button.trigger("pointerdown"); expect(button.attributes("data-pressed")).toBe("true"); await button.trigger("pointerleave"); expect(button.attributes("data-hovered")).toBeUndefined(); expect(button.attributes("data-pressed")).toBeUndefined(); (button.element as HTMLButtonElement).focus(); await button.trigger("focus"); expect(button.attributes("data-focused")).toBe("true"); await button.trigger("blur"); expect(button.attributes("data-focused")).toBeUndefined(); w.unmount(); }); }); describe("Avatar", () => { it("applies size, color and variant slots", () => { const w = mount(Avatar, { props: { size: "lg", variant: "soft" }, slots: { default: () => [ h(AvatarImage, { src: "https://example.com/a.png", alt: "A", }), h(AvatarFallback, { color: "accent" }, { default: () => "JD" }), ], }, }); const root = w.get("[data-slot=avatar]"); expect(root.classes()).toContain("hv-avatar"); expect(root.classes()).toContain("hv-avatar--lg"); expect(root.classes()).toContain("hv-avatar--soft"); const image = w.get("[data-slot=avatar-image]"); expect(image.classes()).toContain("hv-avatar__image"); expect(image.attributes("src")).toBe("https://example.com/a.png"); const fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.classes()).toContain("hv-avatar__fallback"); expect(fallback.classes()).toContain("hv-avatar__fallback--accent"); expect(fallback.text()).toBe("JD"); }); it("shows fallback content while the image is unavailable", async () => { const w = mount(Avatar, { slots: { default: () => [ h(AvatarImage, { src: "https://invalid.example/missing.jpg", alt: "missing", }), h(AvatarFallback, {}, { default: () => "NA" }), ], }, attachTo: document.body, }); await nextTick(); // Reka tracks load state separately; jsdom does not fetch images, so the // fallback stays visible until a successful load. expect(w.text()).toContain("NA"); w.unmount(); }); it("forwards class and DOM attrs to the avatar root", () => { const w = mount(Avatar, { attrs: { class: "custom", "data-id": "a1", "aria-label": "约翰的头像" }, slots: { default: () => h(AvatarFallback, {}, { default: () => "J" }) }, }); const root = w.get("[data-slot=avatar]"); expect(root.classes()).toContain("custom"); expect(root.attributes("data-id")).toBe("a1"); expect(root.attributes("aria-label")).toBe("约翰的头像"); }); it("replaces the inherited fallback color when the child overrides it", () => { const w = mount(Avatar, { props: { color: "accent" }, slots: { default: () => h(AvatarFallback, { color: "danger" }, { default: () => "X" }), }, }); const classes = w.get("[data-slot=avatar-fallback]").classes(); expect(classes).toContain("hv-avatar__fallback--danger"); expect(classes).not.toContain("hv-avatar__fallback--accent"); }); it("keeps the inherited fallback color when the child does not override it", () => { const w = mount(Avatar, { props: { color: "accent" }, slots: { default: () => h(AvatarFallback, {}, { default: () => "X" }), }, }); const classes = w.get("[data-slot=avatar-fallback]").classes(); expect(classes).toContain("hv-avatar__fallback--accent"); expect(classes).not.toContain("hv-avatar__fallback--danger"); }); it("merges custom class onto the fallback without dropping color", () => { const w = mount(Avatar, { props: { color: "success" }, slots: { default: () => h( AvatarFallback, { class: "rounded-lg", color: "warning" }, { default: () => "X" }, ), }, }); const classes = w.get("[data-slot=avatar-fallback]").classes(); expect(classes).toContain("rounded-lg"); expect(classes).toContain("hv-avatar__fallback--warning"); expect(classes).not.toContain("hv-avatar__fallback--success"); }); }); describe("AvatarGroup", () => { it("inherits size/color/variant onto direct children and keeps overrides", () => { const w = mount(AvatarGroup, { props: { size: "sm", variant: "soft", color: "accent" }, slots: { default: () => [ h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), h( Avatar, { size: "lg" }, { default: () => h(AvatarFallback, {}, { default: () => "B" }), }, ), ], }, }); const group = w.get("[data-slot=avatar-group]"); expect(group.classes()).toContain("hv-avatar-group"); expect(group.classes()).toContain("hv-avatar-group--clip"); const avatars = w.findAll("[data-slot=avatar]"); expect(avatars).toHaveLength(2); expect(avatars[0].classes()).toContain("hv-avatar--sm"); expect(avatars[0].classes()).toContain("hv-avatar--soft"); expect(avatars[1].classes()).toContain("hv-avatar--lg"); expect(avatars[0].get("[data-slot=avatar-fallback]").classes()).toContain( "hv-avatar__fallback--accent", ); }); it("truncates with max and renders an automatic count", () => { const w = mount(AvatarGroupMaxDemo); const avatars = w.findAll("[data-slot=avatar]"); // 3 visible avatars; the overflow marker uses its own data-slot. expect(avatars).toHaveLength(3); const count = w.get("[data-slot=avatar-group-count]"); expect(count.text()).toBe("+2"); }); it("keeps explicit counts untruncated and suppresses auto count", () => { const w = mount(AvatarGroupCountDemo); const counts = w.findAll("[data-slot=avatar-group-count]"); expect(counts).toHaveLength(1); expect(counts[0].text()).toBe("+9"); const avatars = w.findAll("[data-slot=avatar]"); expect(avatars).toHaveLength(3); }); it("supports grid and ring overlap modifiers", () => { const grid = mount(AvatarGroup, { props: { isGrid: true, overlap: "ring" }, slots: { default: () => h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "X" }), }, ), }, }); const root = grid.get("[data-slot=avatar-group]"); expect(root.classes()).toContain("hv-avatar-group--grid"); // CSS uses :not(--grid) so ring is inert while grid is on, but the class is still emitted. expect(root.classes()).toContain("hv-avatar-group--ring"); const ring = mount(AvatarGroup, { props: { overlap: "ring" }, slots: { default: () => h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "X" }), }, ), }, }); expect(ring.get("[data-slot=avatar-group]").classes()).toContain( "hv-avatar-group--ring", ); expect(ring.get("[data-slot=avatar-group]").classes()).not.toContain( "hv-avatar-group--grid", ); }); it("does not leak group size to nested avatars", () => { const w = mount(AvatarGroup, { props: { size: "sm" }, slots: { default: () => h( Avatar, { size: "lg" }, { default: () => [ h(AvatarFallback, {}, { default: () => "P" }), h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "N" }), }, ), ], }, ), }, }); const avatars = w.findAll("[data-slot=avatar]"); expect(avatars[0].classes()).toContain("hv-avatar--lg"); // Nested avatar is not a direct group child, so it keeps default md. expect(avatars[1].classes()).toContain("hv-avatar--md"); expect(avatars[1].classes()).not.toContain("hv-avatar--sm"); }); it("forwards group attrs and role", () => { const w = mount(AvatarGroup, { attrs: { role: "group", "aria-label": "指派人" }, slots: { default: () => h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), }, }); const root = w.get("[data-slot=avatar-group]"); expect(root.attributes("role")).toBe("group"); expect(root.attributes("aria-label")).toBe("指派人"); }); }); describe("Avatar dynamic attrs and slot updates", () => { it("updates AvatarFallback aria-label, class and style", async () => { const label = ref("before"); const cls = ref("before"); const color = ref("red"); const w = mount( defineComponent({ setup() { return () => h( Avatar, {}, { default: () => h(AvatarFallback, { "aria-label": label.value, class: cls.value, style: { color: color.value }, }), }, ); }, }), { attachTo: document.body }, ); await nextTick(); let fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.attributes("aria-label")).toBe("before"); expect(fallback.classes()).toContain("before"); expect((fallback.element as HTMLElement).style.color).toBe("red"); label.value = "after"; cls.value = "after"; color.value = "blue"; await nextTick(); fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.attributes("aria-label")).toBe("after"); expect(fallback.classes()).toContain("after"); expect(fallback.classes()).not.toContain("before"); expect((fallback.element as HTMLElement).style.color).toBe("blue"); w.unmount(); }); it("updates AvatarGroupCount aria-label and class", async () => { const label = ref("before"); const cls = ref("before"); const w = mount( defineComponent({ setup() { return () => h( AvatarGroup, {}, { default: () => [ h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), h( AvatarGroupCount, { "aria-label": label.value, class: cls.value }, { default: () => "+1" }, ), ], }, ); }, }), { attachTo: document.body }, ); await nextTick(); let count = w.get("[data-slot=avatar-group-count]"); expect(count.attributes("aria-label")).toBe("before"); expect(count.classes()).toContain("before"); label.value = "after"; cls.value = "after"; await nextTick(); count = w.get("[data-slot=avatar-group-count]"); expect(count.attributes("aria-label")).toBe("after"); expect(count.classes()).toContain("after"); expect(count.classes()).not.toContain("before"); w.unmount(); }); it("reflects replaced slot members after the parent updates", async () => { const items = ref(["A"]); const Host = defineComponent({ setup() { return () => { const current = items.value; return h( AvatarGroup, { max: 2 }, { default: () => current.map((name) => h( Avatar, { key: name }, { default: () => h(AvatarFallback, {}, { default: () => name }), }, ), ), }, ); }; }, }); const w = mount(Host, { attachTo: document.body }); await nextTick(); expect(w.text()).toContain("A"); items.value = ["B", "C"]; await nextTick(); const text = w.text(); expect(text).toContain("B"); expect(text).toContain("C"); expect(text).not.toContain("A"); w.unmount(); }); it("ignores v-if comment nodes when applying max and overflow count", async () => { const showGhost = ref(true); const w = mount( defineComponent({ setup() { return () => h( AvatarGroup, { max: 1 }, { default: () => [ showGhost.value ? createCommentVNode("v-if") : h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "X" }), }, ), h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "REAL" }), }, ), ], }, ); }, }), { attachTo: document.body }, ); await nextTick(); // Comment must not consume the max slot: the real avatar is visible, no +N. expect(w.text()).toContain("REAL"); expect(w.find("[data-slot=avatar-group-count]").exists()).toBe(false); // Two real members: first stays visible, second becomes +1. showGhost.value = false; await nextTick(); expect(w.text()).toContain("X"); expect(w.get("[data-slot=avatar-group-count]").text()).toBe("+1"); w.unmount(); }); it("skips whitespace text nodes and keeps explicit counts with conditionals", async () => { const w = mount(AvatarGroup, { props: { max: 1 }, slots: { default: () => [ " ", createCommentVNode("v-if"), h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "B" }), }, ), h(AvatarGroupCount, {}, { default: () => "+9" }), ], }, attachTo: document.body, }); await nextTick(); expect(w.text()).toContain("A"); expect(w.text()).not.toContain("B"); expect(w.get("[data-slot=avatar-group-count]").text()).toBe("+9"); w.unmount(); }); it("passes through attrs added after mount on AvatarFallback", async () => { const extra = ref>({}); const w = mount( defineComponent({ setup() { return () => h( Avatar, {}, { default: () => h(AvatarFallback, extra.value), }, ); }, }), { attachTo: document.body }, ); await nextTick(); let fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.attributes("aria-label")).toBeUndefined(); expect(fallback.attributes("title")).toBeUndefined(); extra.value = { "aria-label": "new", title: "new" }; await nextTick(); fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.attributes("aria-label")).toBe("new"); expect(fallback.attributes("title")).toBe("new"); extra.value = {}; await nextTick(); fallback = w.get("[data-slot=avatar-fallback]"); expect(fallback.attributes("aria-label")).toBeUndefined(); extra.value = { "aria-label": "again" }; await nextTick(); expect(w.get("[data-slot=avatar-fallback]").attributes("aria-label")).toBe( "again", ); w.unmount(); }); it("passes through attrs added after mount on AvatarGroupCount", async () => { const extra = ref>({}); const w = mount( defineComponent({ setup() { return () => h( AvatarGroup, {}, { default: () => [ h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), h(AvatarGroupCount, extra.value, { default: () => "+1" }), ], }, ); }, }), { attachTo: document.body }, ); await nextTick(); expect( w.get("[data-slot=avatar-group-count]").attributes("aria-label"), ).toBeUndefined(); extra.value = { "aria-label": "new", title: "new" }; await nextTick(); const count = w.get("[data-slot=avatar-group-count]"); expect(count.attributes("aria-label")).toBe("new"); expect(count.attributes("title")).toBe("new"); w.unmount(); }); it("merges caller class on AvatarGroup root", () => { const w = mount(AvatarGroup, { attrs: { class: "gap-4 custom-group" }, slots: { default: () => h( Avatar, {}, { default: () => h(AvatarFallback, {}, { default: () => "A" }), }, ), }, }); const classes = w.get("[data-slot=avatar-group]").classes(); expect(classes).toContain("hv-avatar-group"); expect(classes).toContain("gap-4"); expect(classes).toContain("custom-group"); }); it("keeps AvatarGroup public prop types constrained", async () => { const { AvatarGroup: Group } = await import("../packages/vue/src"); // Compile-time contract: illegal variant values must not type-check. // @ts-expect-error size must be sm | md | lg const badSize: InstanceType["$props"]["size"] = 123; // @ts-expect-error color must be a known theme name const badColor: InstanceType["$props"]["color"] = "nope"; // @ts-expect-error overlap must be clip | ring const badOverlap: InstanceType["$props"]["overlap"] = "invalid"; void badSize; void badColor; void badOverlap; // Legal values remain assignable. const ok: InstanceType["$props"] = { size: "lg", color: "accent", overlap: "ring", isGrid: true, }; expect(ok.size).toBe("lg"); }); }); describe("Skeleton", () => { it("defaults to shimmer and accepts explicit animation types", () => { const basic = mount(Skeleton); expect(basic.get("[data-slot=skeleton]").classes()).toContain( "hv-skeleton", ); expect(basic.get("[data-slot=skeleton]").classes()).toContain( "hv-skeleton--shimmer", ); const pulse = mount(Skeleton, { props: { animationType: "pulse" } }); expect(pulse.get("[data-slot=skeleton]").classes()).toContain( "hv-skeleton--pulse", ); const none = mount(Skeleton, { props: { animationType: "none" } }); expect(none.get("[data-slot=skeleton]").classes()).toContain( "hv-skeleton--none", ); }); it("reads --skeleton-animation on mount when the prop is omitted", async () => { document.documentElement.style.setProperty("--skeleton-animation", "pulse"); const w = mount(Skeleton, { attachTo: document.body }); await nextTick(); expect(w.get("[data-slot=skeleton]").classes()).toContain( "hv-skeleton--pulse", ); document.documentElement.style.removeProperty("--skeleton-animation"); w.unmount(); }); it("applies the synchronized shimmer class on the single-shimmer demo parent", () => { const w = mount(SkeletonSingleShimmer); const parent = w.find(".hv-skeleton--shimmer"); expect(parent.exists()).toBe(true); expect(parent.findAll("[data-slot=skeleton]")).toHaveLength(3); for (const node of parent.findAll("[data-slot=skeleton]")) { expect(node.classes()).toContain("hv-skeleton--none"); } }); }); describe("M04–M06 demo inventory", () => { const expected: Record = { "close-button": [ "default", "interactive", "with-custom-icon", "custom-styles", ], avatar: [ "basic", "sizes", "colors", "variants", "fallback", "custom-image-component", "custom-styles", ], "avatar-group": [ "basic", "max", "count", "sizes", "grid", "overlap", "custom-styles", ], skeleton: [ "basic", "text-content", "user-profile", "list", "grid", "single-shimmer", "animation-types", "custom-styles", ], }; it("includes every preview from the pinned upstream documents", () => { for (const [docName, names] of Object.entries(expected)) { const document = readFileSync( `apps/docs/components/${docName}.md`, "utf8", ); const previewNames = [ ...document.matchAll( new RegExp(` match[1]); expect(previewNames).toEqual(names); for (const name of names) { const source = readFileSync( `apps/docs/demos/${docName}-${name}.vue`, "utf8", ); expect(source).toContain("