import { describe, it, expect } from "vitest"; import { mount } from "@vue/test-utils"; import { h, inject, defineComponent, nextTick } 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")).toBe("#"); 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: `