import { describe, it, expect, vi, afterEach } from "vitest"; import { mount } from "@vue/test-utils"; import { createCommentVNode, createTextVNode, defineComponent, h, nextTick, ref, } from "vue"; import { readFileSync } from "node:fs"; import { Chip, ChipLabel, Kbd, KbdAbbr, KbdContent, Separator, } from "../packages/vue/src"; import { kbdKeysLabelMap, kbdKeysMap } from "../packages/vue/src"; import ChipBasicDemo from "../apps/docs/demos/chip-basic.vue"; afterEach(() => { vi.useRealTimers(); document.body.innerHTML = ""; }); describe("Separator", () => { it("renders a horizontal hr with baseline classes and semantics", () => { const w = mount(Separator); const root = w.get("[data-slot=separator]"); expect(root.element.tagName).toBe("HR"); expect(root.attributes("role")).toBe("separator"); expect(root.attributes("data-orientation")).toBe("horizontal"); expect(root.attributes("aria-orientation")).toBeUndefined(); expect(root.classes()).toContain("hv-separator"); expect(root.classes()).toContain("hv-separator--horizontal"); expect(root.classes()).toContain("hv-separator--default"); }); it("applies vertical orientation and variant classes", () => { const w = mount(Separator, { props: { orientation: "vertical", variant: "secondary" }, }); const root = w.get("[data-slot=separator]"); expect(root.attributes("data-orientation")).toBe("vertical"); expect(root.attributes("aria-orientation")).toBe("vertical"); expect(root.classes()).toContain("hv-separator--vertical"); expect(root.classes()).toContain("hv-separator--secondary"); }); it("supports decorative separators and custom as roots", () => { const decorative = mount(Separator, { props: { decorative: true } }); expect(decorative.get("[data-slot=separator]").attributes("role")).toBe( "none", ); const custom = mount(Separator, { props: { as: "div" } }); expect(custom.get("[data-slot=separator]").element.tagName).toBe("DIV"); }); it("merges attrs onto the root and updates them dynamically", async () => { const extra = ref>({}); const w = mount( defineComponent({ components: { Separator }, setup() { return { extra }; }, template: ``, }), { attachTo: document.body }, ); let root = w.get("[data-slot=separator]"); expect(root.classes()).toContain("hv-separator"); expect(root.classes()).toContain("base-sep"); expect(root.attributes("aria-label")).toBeUndefined(); extra.value = { "aria-label": "section", "data-testid": "sep-1" }; await nextTick(); root = w.get("[data-slot=separator]"); expect(root.attributes("aria-label")).toBe("section"); expect(root.attributes("data-testid")).toBe("sep-1"); extra.value = { "aria-label": "updated" }; await nextTick(); root = w.get("[data-slot=separator]"); expect(root.attributes("aria-label")).toBe("updated"); expect(root.attributes("data-testid")).toBeUndefined(); extra.value = {}; await nextTick(); root = w.get("[data-slot=separator]"); expect(root.attributes("aria-label")).toBeUndefined(); extra.value = { title: "again" }; await nextTick(); root = w.get("[data-slot=separator]"); expect(root.attributes("title")).toBe("again"); w.unmount(); }); it("merges caller class strings, arrays and objects", async () => { const classValue = ref("extra-a"); const w = mount( defineComponent({ components: { Separator }, setup() { return { classValue }; }, template: ``, }), ); let classes = w.get("[data-slot=separator]").classes(); expect(classes).toContain("hv-separator"); expect(classes).toContain("extra-a"); classValue.value = ["extra-b", { "extra-c": true, "extra-a": false }]; await nextTick(); classes = w.get("[data-slot=separator]").classes(); expect(classes).toContain("hv-separator"); expect(classes).toContain("extra-b"); expect(classes).toContain("extra-c"); expect(classes).not.toContain("extra-a"); classValue.value = undefined; await nextTick(); classes = w.get("[data-slot=separator]").classes(); expect(classes).toContain("hv-separator"); expect(classes).not.toContain("extra-b"); w.unmount(); }); it("keeps orientation and variant classes in sync when props change", async () => { const orientation = ref<"horizontal" | "vertical">("horizontal"); const variant = ref<"default" | "secondary" | "tertiary">("default"); const w = mount( defineComponent({ components: { Separator }, setup() { return { orientation, variant }; }, template: ``, }), ); expect(w.get("[data-slot=separator]").classes()).toContain( "hv-separator--horizontal", ); orientation.value = "vertical"; variant.value = "tertiary"; await nextTick(); const classes = w.get("[data-slot=separator]").classes(); expect(classes).toContain("hv-separator--vertical"); expect(classes).toContain("hv-separator--tertiary"); expect(classes).not.toContain("hv-separator--horizontal"); expect(classes).not.toContain("hv-separator--default"); w.unmount(); }); it("supports asChild custom roots with data attributes", () => { const w = mount(Separator, { props: { asChild: true }, slots: { default: () => h("div", { "data-custom": "foo" }) }, }); const root = w.get("[data-slot=separator]"); expect(root.element.tagName).toBe("DIV"); expect(root.attributes("data-custom")).toBe("foo"); expect(root.classes()).toContain("hv-separator"); expect(root.attributes("data-orientation")).toBe("horizontal"); }); it("keeps public prop types constrained", async () => { const { Separator: Sep } = await import("../packages/vue/src"); // @ts-expect-error orientation must be horizontal | vertical const badOrientation: InstanceType["$props"]["orientation"] = "diagonal"; // @ts-expect-error variant must be default | secondary | tertiary const badVariant: InstanceType["$props"]["variant"] = "ghost"; void badOrientation; void badVariant; const ok: InstanceType["$props"] = { orientation: "vertical", variant: "tertiary", decorative: true, }; expect(ok.orientation).toBe("vertical"); }); }); describe("Kbd", () => { it("renders default kbd with abbr mapping and content wrapper", () => { const w = mount(Kbd, { slots: { default: () => [ h(KbdAbbr, { keyValue: "command" }), h(KbdContent, {}, { default: () => "K" }), ], }, }); const root = w.get("kbd"); expect(root.classes()).toContain("hv-kbd"); expect(root.classes()).toContain("hv-kbd--default"); const abbr = w.get("abbr"); expect(abbr.text()).toBe("⌘"); expect(abbr.attributes("title")).toBe("Command"); expect(abbr.classes()).toContain("hv-kbd__abbr"); const content = w.get("span"); expect(content.text()).toBe("K"); expect(content.classes()).toContain("hv-kbd__content"); }); it("maps every documented keyValue to a symbol and title", () => { for (const [key, symbol] of Object.entries(kbdKeysMap)) { const w = mount(Kbd, { slots: { default: () => h(KbdAbbr, { keyValue: key as keyof typeof kbdKeysMap }), }, }); expect(w.get("abbr").text()).toBe(symbol); expect(w.get("abbr").attributes("title")).toBe( kbdKeysLabelMap[key as keyof typeof kbdKeysLabelMap], ); w.unmount(); } }); it("updates variant and keyValue dynamically", async () => { const variant = ref<"default" | "light">("default"); const keyValue = ref<"command" | "shift">("command"); const w = mount( defineComponent({ components: { Kbd, KbdAbbr }, setup() { return { variant, keyValue }; }, template: ``, }), ); expect(w.get("kbd").classes()).toContain("hv-kbd--default"); expect(w.get("abbr").text()).toBe("⌘"); expect(w.get("abbr").attributes("title")).toBe("Command"); variant.value = "light"; keyValue.value = "shift"; await nextTick(); expect(w.get("kbd").classes()).toContain("hv-kbd--light"); expect(w.get("kbd").classes()).not.toContain("hv-kbd--default"); expect(w.get("abbr").text()).toBe("⇧"); expect(w.get("abbr").attributes("title")).toBe("Shift"); w.unmount(); }); it("passes attrs to the right element and updates them dynamically", async () => { const rootExtra = ref>({}); const abbrExtra = ref>({}); const contentExtra = ref>({}); const w = mount( defineComponent({ components: { Kbd, KbdAbbr, KbdContent }, setup() { return { rootExtra, abbrExtra, contentExtra }; }, template: ` Y `, }), { attachTo: document.body }, ); expect(w.get("kbd").attributes("data-testid")).toBeUndefined(); rootExtra.value = { "data-testid": "kbd-root", "aria-label": "shortcut" }; abbrExtra.value = { "data-testid": "kbd-abbr" }; contentExtra.value = { "data-testid": "kbd-content", title: "letter" }; await nextTick(); expect(w.get("kbd").attributes("data-testid")).toBe("kbd-root"); expect(w.get("kbd").attributes("aria-label")).toBe("shortcut"); expect(w.get("abbr").attributes("data-testid")).toBe("kbd-abbr"); expect(w.get("span").attributes("data-testid")).toBe("kbd-content"); expect(w.get("span").attributes("title")).toBe("letter"); rootExtra.value = {}; await nextTick(); expect(w.get("kbd").attributes("data-testid")).toBeUndefined(); expect(w.get("kbd").attributes("aria-label")).toBeUndefined(); w.unmount(); }); it("merges caller class on all three parts", () => { const w = mount(Kbd, { attrs: { class: "kbd-custom" }, slots: { default: () => [ h(KbdAbbr, { keyValue: "tab", class: "abbr-custom" }), h(KbdContent, { class: "content-custom" }, { default: () => "T" }), ], }, }); expect(w.get("kbd").classes()).toContain("hv-kbd"); expect(w.get("kbd").classes()).toContain("kbd-custom"); expect(w.get("abbr").classes()).toContain("hv-kbd__abbr"); expect(w.get("abbr").classes()).toContain("abbr-custom"); expect(w.get("span").classes()).toContain("hv-kbd__content"); expect(w.get("span").classes()).toContain("content-custom"); }); it("keeps public prop types constrained", async () => { const { KbdAbbr: Abbr, Kbd: Root } = await import("../packages/vue/src"); // @ts-expect-error keyValue must be a known KbdKey const badKey: InstanceType["$props"]["keyValue"] = "nope"; // @ts-expect-error variant must be default | light const badVariant: InstanceType["$props"]["variant"] = "solid"; void badKey; void badVariant; const okKey: InstanceType["$props"] = { keyValue: "pageup" }; const okRoot: InstanceType["$props"] = { variant: "light" }; expect(okKey.keyValue).toBe("pageup"); expect(okRoot.variant).toBe("light"); }); }); describe("Chip", () => { it("auto-wraps plain text and number default slots in ChipLabel", () => { const text = mount(Chip, { slots: { default: () => "默认" } }); const label = text.get("[data-slot=chip-label]"); expect(label.text()).toBe("默认"); expect(label.classes()).toContain("hv-chip__label"); expect(text.get("[data-slot=chip]").element.childNodes).toHaveLength(1); const number = mount(Chip, { slots: { default: () => createTextVNode("42") }, }); expect(number.get("[data-slot=chip-label]").text()).toBe("42"); }); it("does not double-wrap explicit ChipLabel or composed content", () => { const explicit = mount(Chip, { slots: { default: () => h(ChipLabel, { class: "custom-label" }, { default: () => "标签" }), }, }); expect(explicit.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(explicit.get("[data-slot=chip-label]").text()).toBe("标签"); expect(explicit.get("[data-slot=chip-label]").classes()).toContain( "custom-label", ); const composed = mount(Chip, { slots: { default: () => [ h("span", { class: "icon" }, "•"), h(ChipLabel, {}, { default: () => "组合" }), ], }, }); expect(composed.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(composed.find(".icon").exists()).toBe(true); }); it("switches between plain text and composed slot content dynamically", async () => { const mode = ref<"text" | "composed">("text"); const w = mount( defineComponent({ components: { Chip, ChipLabel }, setup() { return { mode }; }, template: ` `, }), ); expect(w.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(w.get("[data-slot=chip-label]").text()).toBe("纯文本"); expect(w.find(".dot").exists()).toBe(false); mode.value = "composed"; await nextTick(); expect(w.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(w.get("[data-slot=chip-label]").text()).toBe("组合"); expect(w.find(".dot").exists()).toBe(true); mode.value = "text"; await nextTick(); expect(w.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(w.get("[data-slot=chip-label]").text()).toBe("纯文本"); expect(w.find(".dot").exists()).toBe(false); w.unmount(); }); it("ignores comments and whitespace when deciding auto-wrap", () => { const w = mount(Chip, { slots: { default: () => [ createCommentVNode("cond"), createTextVNode(" "), createTextVNode("可见文本"), ], }, }); expect(w.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(w.get("[data-slot=chip-label]").text()).toBe("可见文本"); const empty = mount(Chip, { slots: { default: () => [createCommentVNode("only-comment")] }, }); expect(empty.findAll("[data-slot=chip-label]")).toHaveLength(0); }); it("applies color, size and variant classes without conflicting overrides", () => { const w = mount(Chip, { props: { color: "accent", size: "lg", variant: "primary" }, slots: { default: () => "强调" }, }); const classes = w.get("[data-slot=chip]").classes(); expect(classes).toContain("hv-chip"); expect(classes).toContain("hv-chip--accent"); expect(classes).toContain("hv-chip--lg"); expect(classes).toContain("hv-chip--primary"); }); it("updates variant classes when props change and merges caller class", async () => { const color = ref<"default" | "danger">("default"); const classValue = ref("chip-custom"); const w = mount( defineComponent({ components: { Chip }, setup() { return { color, classValue }; }, template: `状态`, }), ); let classes = w.get("[data-slot=chip]").classes(); expect(classes).toContain("hv-chip--default"); expect(classes).toContain("chip-custom"); expect(classes).toContain("hv-chip"); color.value = "danger"; classValue.value = ["chip-custom", { "chip-extra": true }]; await nextTick(); classes = w.get("[data-slot=chip]").classes(); expect(classes).toContain("hv-chip--danger"); expect(classes).not.toContain("hv-chip--default"); expect(classes).toContain("chip-custom"); expect(classes).toContain("chip-extra"); expect(classes).toContain("hv-chip"); w.unmount(); }); it("passes attrs to the root and updates them dynamically", async () => { const extra = ref>({}); const w = mount( defineComponent({ components: { Chip }, setup() { return { extra }; }, template: `属性`, }), { attachTo: document.body }, ); const root = () => w.get("[data-slot=chip]"); expect(root().attributes("data-x")).toBeUndefined(); extra.value = { "data-x": "1", "aria-hidden": "true", title: "t1" }; await nextTick(); expect(root().attributes("data-x")).toBe("1"); expect(root().attributes("aria-hidden")).toBe("true"); expect(root().attributes("title")).toBe("t1"); extra.value = { "data-x": "2" }; await nextTick(); expect(root().attributes("data-x")).toBe("2"); expect(root().attributes("title")).toBeUndefined(); extra.value = {}; await nextTick(); expect(root().attributes("data-x")).toBeUndefined(); extra.value = { "data-y": "again" }; await nextTick(); expect(root().attributes("data-y")).toBe("again"); w.unmount(); }); it("wires events through attrs and switches handlers dynamically", async () => { const first = vi.fn(); const second = vi.fn(); const handler = ref<((e: Event) => void) | undefined>(first); const w = mount( defineComponent({ components: { Chip }, setup() { return { handler }; }, template: `事件`, }), { attachTo: document.body }, ); await w.get("[data-slot=chip]").trigger("click"); expect(first).toHaveBeenCalledTimes(1); handler.value = second; await nextTick(); await w.get("[data-slot=chip]").trigger("click"); expect(first).toHaveBeenCalledTimes(1); expect(second).toHaveBeenCalledTimes(1); handler.value = undefined; await nextTick(); await w.get("[data-slot=chip]").trigger("click"); expect(second).toHaveBeenCalledTimes(1); w.unmount(); }); it("replaces the default slot closure and keeps auto-wrap accurate", async () => { const content = ref<"a" | "b" | "composed">("a"); const w = mount( defineComponent({ components: { Chip, ChipLabel }, setup() { return { content }; }, template: ` `, }), ); expect(w.get("[data-slot=chip-label]").text()).toBe("A文本"); content.value = "b"; await nextTick(); expect(w.get("[data-slot=chip-label]").text()).toBe("B文本"); content.value = "composed"; await nextTick(); expect(w.findAll("[data-slot=chip-label]")).toHaveLength(1); expect(w.get("[data-slot=chip-label]").text()).toBe("显式"); w.unmount(); }); it("renders the chip-basic demo with auto-wrapped labels", () => { const w = mount(ChipBasicDemo); const chips = w.findAll("[data-slot=chip]"); expect(chips).toHaveLength(5); for (const chip of chips) { expect(chip.findAll("[data-slot=chip-label]")).toHaveLength(1); } expect(chips[0].classes()).toContain("hv-chip--default"); expect(chips[1].classes()).toContain("hv-chip--accent"); }); it("keeps public prop types constrained", async () => { const { Chip: ChipComp } = await import("../packages/vue/src"); // @ts-expect-error color must be a known chip color const badColor: InstanceType["$props"]["color"] = "neon"; // @ts-expect-error variant must be a known chip variant const badVariant: InstanceType["$props"]["variant"] = "ghost"; // @ts-expect-error size must be sm | md | lg const badSize: InstanceType["$props"]["size"] = "xl"; void badColor; void badVariant; void badSize; const ok: InstanceType["$props"] = { color: "success", variant: "soft", size: "sm", }; expect(ok.color).toBe("success"); }); }); describe("M07–M09 demo inventory", () => { const expected: Record = { separator: [ "basic", "variants", "with-surface", "vertical", "with-content", "render-function", "custom-styles", ], kbd: [ "basic", "variants", "navigation-keys", "inline-usage", "instructional-text", "special-keys", "custom-styles", ], chip: ["basic", "variants", "statuses", "with-icon", "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("