feat: add Surface, Link and Spinner components
This commit is contained in:
@@ -34,6 +34,16 @@ const inventory = {
|
||||
"controlled",
|
||||
"custom-styles",
|
||||
],
|
||||
link: [
|
||||
"basic",
|
||||
"icon-placement",
|
||||
"underline-and-offset",
|
||||
"custom-icon",
|
||||
"render-function",
|
||||
"custom-styles",
|
||||
],
|
||||
spinner: ["basic", "colors", "sizes", "speed", "custom-styles"],
|
||||
surface: ["basic", "variants", "with-form-components", "custom-styles"],
|
||||
switch: [
|
||||
"basic",
|
||||
"sizes",
|
||||
|
||||
+19
-1
@@ -2,7 +2,17 @@
|
||||
import { it, expect } from "vitest";
|
||||
import { createSSRApp, h } from "vue";
|
||||
import { renderToString } from "@vue/server-renderer";
|
||||
import { Button, Card, Input, Switch } from "../packages/vue/src";
|
||||
import {
|
||||
Button,
|
||||
Card,
|
||||
Input,
|
||||
Switch,
|
||||
Surface,
|
||||
Link,
|
||||
LinkIcon,
|
||||
Spinner,
|
||||
TextArea,
|
||||
} from "../packages/vue/src";
|
||||
it("renders the initial components without browser globals", async () => {
|
||||
const html = await renderToString(
|
||||
createSSRApp({
|
||||
@@ -11,10 +21,18 @@ it("renders the initial components without browser globals", async () => {
|
||||
h(Button, () => "保存"),
|
||||
h(Input, { modelValue: "名称" }),
|
||||
h(Switch, { checked: true, "aria-label": "通知" }),
|
||||
h(Surface, { variant: "secondary" }, () => h("span", "表面")),
|
||||
h(Link, { href: "#" }, () => ["链接", h(LinkIcon)]),
|
||||
h(Spinner),
|
||||
h(TextArea, { modelValue: "备注" }),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
expect(html).toContain("保存");
|
||||
expect(html).toContain('value="名称"');
|
||||
expect(html).toContain('role="switch"');
|
||||
expect(html).toContain("hv-surface--secondary");
|
||||
expect(html).toContain('data-slot="link"');
|
||||
expect(html).toContain('role="status"');
|
||||
expect(html).toContain('data-slot="textarea"');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,540 @@
|
||||
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: `
|
||||
<div data-ancestor @click="order.push('ancestor')" @click.capture="order.push('ancestor-cap')">
|
||||
<Link href="#" as-child @click="order.push('link')">
|
||||
<span data-custom="propagates" @click="order.push('slot')">传播</span>
|
||||
</Link>
|
||||
</div>
|
||||
`,
|
||||
},
|
||||
{ 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",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user