import { describe, it, expect, vi } from "vitest"; import { mount } from "@vue/test-utils"; import { h, inject, defineComponent, nextTick, ref } from "vue"; import { Surface, surfaceContextKey, Link, LinkIcon, Spinner, TextArea, Button, Input, } from "../packages/vue/src"; describe("Surface", () => { it("applies the default variant and surface data-slot", () => { const w = mount(Surface, { props: { variant: "secondary" }, slots: { default: "内容" }, }); expect(w.classes()).toContain("hv-surface"); expect(w.classes()).toContain("hv-surface--secondary"); expect(w.attributes("data-slot")).toBe("surface"); }); it("provides the current variant to descendants", () => { let seen: unknown; const Child = defineComponent({ setup() { seen = inject(surfaceContextKey)?.variant.value; return () => h("span", "child"); }, }); mount(Surface, { props: { variant: "tertiary" }, slots: { default: () => h(Child) }, }); expect(seen).toBe("tertiary"); }); it("forwards class and DOM attrs to the root", () => { const w = mount(Surface, { props: { variant: "transparent" }, attrs: { class: "custom", "data-id": "s1" }, slots: { default: "x" }, }); expect(w.classes()).toContain("hv-surface--transparent"); expect(w.classes()).toContain("custom"); expect(w.attributes("data-id")).toBe("s1"); }); }); describe("Link", () => { it("renders an anchor with link classes and default icon mark", () => { const w = mount(Link, { props: { href: "https://example.com", target: "_blank" }, slots: { default: () => ["立即行动", h(LinkIcon)], }, }); const a = w.get("a"); expect(a.attributes("href")).toBe("https://example.com"); expect(a.attributes("target")).toBe("_blank"); expect(a.attributes("data-slot")).toBe("link"); expect(a.classes()).toContain("hv-link"); const icon = w.get("[data-slot=link-icon]"); expect(icon.attributes("data-default-icon")).toBe("true"); expect(icon.find("[data-slot=link-default-icon]").exists()).toBe(true); }); it("omits download unless requested", () => { const base = mount(Link, { props: { href: "#", download: false }, slots: { default: "x" }, }); expect(base.get("a").attributes("download")).toBeUndefined(); const named = mount(Link, { props: { href: "#", download: "report.pdf" }, slots: { default: "x" }, }); expect(named.get("a").attributes("download")).toBe("report.pdf"); }); it("disables activation and marks aria-disabled", async () => { let clicks = 0; let keydowns = 0; let captureClicks = 0; const w = mount(Link, { props: { href: "#", isDisabled: true }, attrs: { onClick: () => clicks++, onClickCapture: () => captureClicks++, onKeydown: () => keydowns++, }, slots: { default: "禁用链接" }, }); const a = w.get("a"); expect(a.attributes("aria-disabled")).toBe("true"); expect(a.attributes("href")).toBeUndefined(); await a.trigger("click"); expect(clicks).toBe(0); expect(captureClicks).toBe(0); await a.trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); await a.trigger("keydown", { key: " " }); expect(keydowns).toBe(0); }); it("still delivers click and keydown to the caller when enabled", async () => { let clicks = 0; let keydowns = 0; const w = mount(Link, { props: { href: "#", isDisabled: false }, attrs: { onClick: () => clicks++, onKeydown: () => keydowns++, }, slots: { default: "启用链接" }, }); await w.get("a").trigger("click"); await w .get("a") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(clicks).toBe(1); expect(keydowns).toBe(1); }); it("invokes merged listener arrays once per handler", async () => { const calls: string[] = []; const w = mount(Link, { props: { href: "#" }, attrs: { onClick: [() => calls.push("a"), () => calls.push("b")], onKeydown: [() => calls.push("k1"), () => calls.push("k2")], }, slots: { default: "数组监听" }, }); await w.get("a").trigger("click"); await w .get("a") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(calls).toEqual(["a", "b", "k1", "k2"]); }); it("suppresses merged listener arrays when disabled", async () => { let hits = 0; const w = mount(Link, { props: { href: "#", isDisabled: true }, attrs: { onClick: [() => hits++, () => hits++], onClickCapture: [() => hits++], onKeydown: [() => hits++, () => hits++], }, slots: { default: "禁用数组" }, }); await w.get("a").trigger("click"); await w .get("a") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(hits).toBe(0); }); it("autoFocus focuses the real DOM anchor", async () => { const w = mount(Link, { props: { href: "#", autoFocus: true }, attachTo: document.body, slots: { default: "聚焦我" }, }); expect(document.activeElement).toBe(w.get("a").element); expect((w.vm as { element?: HTMLElement }).element).toBe( w.get("a").element, ); w.unmount(); }); it("gives asChild custom roots link semantics and Enter activation", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClick: () => clicks++ }, attachTo: document.body, slots: { default: () => h("span", { "data-custom": "foo" }, "自定义链接"), }, }); const span = w.get("span[data-custom=foo]"); expect(span.attributes("role")).toBe("link"); expect(span.attributes("tabindex")).toBe("0"); expect(span.attributes("href")).toBe("#"); await span.trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13, }); expect(clicks).toBe(1); const spanEl = span.element as HTMLElement; spanEl.focus(); expect(document.activeElement).toBe(spanEl); spanEl.blur(); (w.vm as unknown as { focus: () => void }).focus(); expect(document.activeElement).toBe(spanEl); w.unmount(); }); it("activates once per full Enter press (keydown + keyup)", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClick: () => clicks++ }, slots: { default: () => h("span", { "data-custom": "enter-once" }, "一次激活"), }, }); const span = w.get("span[data-custom=enter-once]"); await span.trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); await span.trigger("keyup", { key: "Enter", code: "Enter", keyCode: 13 }); expect(clicks).toBe(1); w.unmount(); }); it("allows caller preventDefault to cancel keyboard activation", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClick: () => clicks++, onKeydown: (e: Event) => e.preventDefault(), }, slots: { default: () => h("span", { "data-custom": "cancel-kb" }, "可取消"), }, }); await w .get("span[data-custom=cancel-kb]") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(clicks).toBe(0); w.unmount(); }); it("allows caller capture preventDefault to cancel keyboard activation", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClick: () => clicks++, onKeydownCapture: (e: Event) => e.preventDefault(), }, slots: { default: () => h("span", { "data-custom": "cancel-cap" }, "捕获取消"), }, }); await w .get("span[data-custom=cancel-cap]") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(clicks).toBe(0); w.unmount(); }); it("dispatches a real click to slot, link and ancestor on Enter", async () => { const order: string[] = []; const w = mount( { components: { Link }, setup() { return { order }; }, template: `
传播
`, }, { attachTo: document.body }, ); const span = w.get("span[data-custom=propagates]"); await span.trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(order).toContain("link"); expect(order).toContain("slot"); expect(order).toContain("ancestor"); w.unmount(); }); it("sets target and currentTarget on keyboard-activated click", async () => { let target: EventTarget | null = null; let current: EventTarget | null = null; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClick: (e: Event) => { target = e.target; current = e.currentTarget; }, }, attachTo: document.body, slots: { default: () => h("span", { "data-custom": "evt-target" }, "目标"), }, }); const span = w.get("span[data-custom=evt-target]"); await span.trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13, }); expect(target).toBe(span.element); expect(current).toBe(span.element); w.unmount(); }); it("does not skip bubble when click capture only preventDefaults", async () => { const order: string[] = []; const w = mount(Link, { props: { href: "#", asChild: true }, attrs: { onClickCapture: (e: Event) => { order.push("cap"); e.preventDefault(); }, onClick: () => order.push("bubble"), }, attachTo: document.body, slots: { default: () => h("span", { "data-custom": "cap-pd" }, "取消默认"), }, }); await w .get("span[data-custom=cap-pd]") .trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13 }); expect(order).toEqual(["cap", "bubble"]); w.unmount(); }); it("does not double-activate native anchors on Enter", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#" }, attrs: { onClick: () => clicks++ }, slots: { default: "原生锚点" }, }); await w.get("a").trigger("keydown", { key: "Enter", code: "Enter", keyCode: 13, }); expect(clicks).toBe(0); }); it("stops later array handlers after stopImmediatePropagation", async () => { const calls: string[] = []; const w = mount(Link, { props: { href: "#" }, attrs: { onClick: [ (e: Event) => { calls.push("first"); e.stopImmediatePropagation(); }, () => calls.push("second"), ], }, slots: { default: "中断数组" }, }); await w.get("a").trigger("click"); expect(calls).toEqual(["first"]); }); it("stops later capture array handlers after stopImmediatePropagation", async () => { const calls: string[] = []; const w = mount(Link, { props: { href: "#" }, attrs: { onClickCapture: [ (e: Event) => { calls.push("cap-first"); e.stopImmediatePropagation(); }, () => calls.push("cap-second"), ], }, slots: { default: "捕获中断" }, }); await w.get("a").trigger("click"); expect(calls).toEqual(["cap-first"]); }); it("autoFocus works on asChild custom roots", async () => { const w = mount(Link, { props: { href: "#", asChild: true, autoFocus: true }, attachTo: document.body, slots: { default: () => h("span", { "data-custom": "focus-me" }, "聚焦"), }, }); expect(document.activeElement).toBe( w.get("span[data-custom=focus-me]").element, ); w.unmount(); }); it("allows activation when enabled and keeps custom icon content", async () => { let clicks = 0; const w = mount(Link, { props: { href: "#" }, attrs: { onClick: () => clicks++ }, slots: { default: () => [ "前往页面", h(LinkIcon, { class: "size-3" }, () => h("svg", { "data-custom-icon": "true" }), ), ], }, }); const icon = w.get("[data-slot=link-icon]"); expect(icon.attributes("data-default-icon")).toBeUndefined(); expect(icon.find("[data-custom-icon]").exists()).toBe(true); await w.get("a").trigger("click"); expect(clicks).toBe(1); }); it("merges asChild onto a custom element", () => { const w = mount(Link, { props: { href: "#", asChild: true }, slots: { default: () => h("span", { "data-custom": "foo" }, ["立即行动", h(LinkIcon)]), }, }); const span = w.get("span[data-custom=foo]"); expect(span.attributes("href")).toBe("#"); expect(span.attributes("data-slot")).toBe("link"); expect(span.classes()).toContain("hv-link"); expect(span.find("[data-slot=link-icon]").exists()).toBe(true); }); }); describe("Spinner", () => { it("exposes loading semantics and size/color variants", () => { const w = mount(Spinner, { props: { size: "sm", color: "danger" } }); expect(w.attributes("role")).toBe("status"); expect(w.attributes("aria-label")).toBe("Loading"); expect(w.attributes("data-slot")).toBe("spinner"); expect(w.classes()).toContain("hv-spinner--sm"); expect(w.classes()).toContain("hv-spinner--danger"); const svg = w.get("svg[data-slot=spinner-icon]"); expect(svg.attributes("aria-hidden")).toBe("true"); expect(svg.attributes("viewBox")).toBe("0 0 24 24"); }); it("allows aria overrides and keeps 16px footprint inside Button", async () => { const custom = mount(Spinner, { attrs: { "aria-label": "处理中", role: "presentation" }, }); expect(custom.attributes("aria-label")).toBe("处理中"); expect(custom.attributes("role")).toBe("presentation"); const button = mount(Button, { props: { loading: true }, slots: { default: "上传中…" }, }); const spinner = button.get("[data-slot=spinner]"); expect(spinner.classes()).toContain("hv-spinner--sm"); expect(spinner.attributes("aria-hidden")).toBe("true"); expect(button.attributes("aria-busy")).toBe("true"); }); it("keeps gradient ids unique across instances", () => { const w = mount({ render: () => [h(Spinner), h(Spinner)], }); const ids = w.findAll("linearGradient").map((g) => g.attributes("id")); expect(ids.length).toBe(4); expect(new Set(ids).size).toBe(4); }); }); describe("TextArea", () => { it("emits values without mutating a controlled value", async () => { const w = mount(TextArea, { props: { modelValue: "原值", variant: "secondary" }, attrs: { "aria-label": "备注", name: "note" }, }); expect(w.classes()).toContain("hv-textarea--secondary"); await w.get("textarea").setValue("新值"); expect(w.emitted("update:modelValue")).toEqual([["新值"]]); await w.setProps({ modelValue: "父级值" }); expect(w.element.value).toBe("父级值"); }); it("supports uncontrolled values and native form reset", async () => { const form = document.createElement("form"); document.body.append(form); const w = mount(TextArea, { props: { defaultValue: "初始" }, attachTo: form, }); await w.get("textarea").setValue("编辑"); expect(w.element.value).toBe("编辑"); form.reset(); await nextTick(); expect(w.element.value).toBe("初始"); w.unmount(); form.remove(); }); it("maps invalid state to documented CSS hooks", () => { const w = mount(TextArea, { props: { invalid: true, fullWidth: true } }); expect(w.classes()).toContain("hv-textarea--full-width"); expect(w.attributes("data-invalid")).toBe("true"); expect(w.attributes("aria-invalid")).toBe("true"); }); }); describe("Surface form demo integration", () => { it("renders secondary Input and TextArea inside a Surface", () => { const w = mount({ render: () => h(Surface, { variant: "default" }, () => [ h(Input, { placeholder: "使用次要变体的输入框", variant: "secondary", }), h(TextArea, { placeholder: "使用次要变体的文本域", variant: "secondary", }), ]), }); expect(w.get("[data-slot=surface]").attributes("data-slot")).toBe( "surface", ); expect(w.get("[data-slot=input]").classes()).toContain( "hv-input--secondary", ); expect(w.get("[data-slot=textarea]").classes()).toContain( "hv-textarea--secondary", ); }); }); it("preserves explicit role and tabindex on a native Link used as a search option", () => { const w = mount(Link, { props: { href: "/components/button" }, attrs: { role: "option", tabindex: -1, "aria-selected": "true" }, }); expect(w.attributes("role")).toBe("option"); expect(w.attributes("tabindex")).toBe("-1"); expect(w.attributes("aria-selected")).toBe("true"); w.unmount(); }); it("omits the implicit same-window target so host routers can intercept navigation", () => { const w = mount(Link, { props: { href: "/components/button" } }); expect(w.attributes("target")).toBeUndefined(); w.unmount(); const explicit = mount(Link, { props: { href: "/components/button", target: "_self" }, }); expect(explicit.attributes("target")).toBe("_self"); explicit.unmount(); }); it.each([false, true])( "removes disabled navigation attributes and restores them dynamically (asChild=%s)", async (asChild) => { const click = vi.fn(); const childCapture = vi.fn(); const childRef = ref(); const w = mount(Link, { props: { asChild, href: "#parent", target: "_blank", download: "parent.txt", isDisabled: true, }, attrs: { class: "consumer", onClick: click, "data-test": "kept" }, slots: { default: () => asChild ? h( "a", { href: "#child", target: "_self", download: "child.txt", ref: childRef, tabIndex: 0, onClickCapture: childCapture, }, "link", ) : "link", }, attachTo: document.body, }); try { const root = w.get("a"); expect(root.attributes("href")).toBeUndefined(); expect(root.attributes("target")).toBeUndefined(); expect(root.attributes("download")).toBeUndefined(); expect(root.attributes("aria-disabled")).toBe("true"); expect(root.attributes("tabindex")).toBe("-1"); expect(root.classes()).toContain("consumer"); expect(root.attributes("data-test")).toBe("kept"); if (asChild) expect(childRef.value).toBe(root.element); const event = new MouseEvent("click", { bubbles: true, cancelable: true, }); root.element.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); const middle = new MouseEvent("auxclick", { bubbles: true, cancelable: true, button: 1, }); root.element.dispatchEvent(middle); expect(middle.defaultPrevented).toBe(true); expect(click).not.toHaveBeenCalled(); expect(childCapture).not.toHaveBeenCalled(); await w.setProps({ isDisabled: false }); expect(root.attributes("href")).toBe(asChild ? "#child" : "#parent"); expect(root.attributes("download")).toBe( asChild ? "child.txt" : "parent.txt", ); await root.trigger("click"); expect(click).toHaveBeenCalledTimes(1); if (asChild) expect(childCapture).toHaveBeenCalledTimes(1); await w.setProps({ isDisabled: true }); expect(root.attributes("href")).toBeUndefined(); } finally { w.unmount(); } }, ); it("preserves forwarding child refs and handlers while respecting cancellation", async () => { const child = vi.fn(), parent = vi.fn(); const forwardedRef = ref(); const Forwarded = defineComponent({ inheritAttrs: false, setup(_, { attrs }) { return () => h("a", attrs, "forwarded"); }, }); const w = mount(Link, { props: { asChild: true, isDisabled: true }, attrs: { onClick: parent, onClickCapture: (e: Event) => e.preventDefault(), }, slots: { default: () => h(Forwarded, { href: "#forwarded", ref: forwardedRef, onClick: child }), }, attachTo: document.body, }); try { expect(w.get("a").attributes("href")).toBeUndefined(); await w.get("a").trigger("click"); expect(child).not.toHaveBeenCalled(); await w.setProps({ isDisabled: false }); expect(forwardedRef.value.$el).toBe(w.get("a").element); const event = new MouseEvent("click", { bubbles: true, cancelable: true }); w.get("a").element.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); expect(parent).toHaveBeenCalledTimes(1); expect(child).toHaveBeenCalledTimes(1); } finally { w.unmount(); } });