import { describe, it, expect, afterEach } from "vitest";
import { mount } from "@vue/test-utils";
import {
createCommentVNode,
createTextVNode,
defineComponent,
h,
inject,
nextTick,
ref,
} from "vue";
import { readFileSync } from "node:fs";
import {
Alert,
AlertContent,
AlertDescription,
AlertIndicator,
AlertTitle,
Badge,
BadgeAnchor,
BadgeLabel,
surfaceContextKey,
} from "../packages/vue/src";
import AlertBasicDemo from "../apps/docs/demos/alert-basic.vue";
import BadgeBasicDemo from "../apps/docs/demos/badge-basic.vue";
afterEach(() => {
document.body.innerHTML = "";
});
describe("Badge", () => {
it("renders default placement/size/variant and wraps plain text", () => {
const w = mount(Badge, { slots: { default: () => "5" } });
const root = w.get("[data-slot=badge]");
expect(root.classes()).toContain("hv-badge");
expect(root.classes()).toContain("hv-badge--default");
expect(root.classes()).toContain("hv-badge--top-right");
expect(root.classes()).toContain("hv-badge--md");
expect(root.classes()).toContain("hv-badge--primary");
const label = w.get("[data-slot=badge-label]");
expect(label.text()).toBe("5");
expect(label.classes()).toContain("hv-badge__label");
expect(root.element.childNodes).toHaveLength(1);
});
it("wraps number zero and text, not explicit label or composed content", () => {
const zero = mount(Badge, {
slots: { default: () => createTextVNode("0") },
});
expect(zero.get("[data-slot=badge-label]").text()).toBe("0");
const explicit = mount(Badge, {
slots: {
default: () =>
h(BadgeLabel, { class: "custom" }, { default: () => "标签" }),
},
});
expect(explicit.findAll("[data-slot=badge-label]")).toHaveLength(1);
expect(explicit.get("[data-slot=badge-label]").classes()).toContain(
"custom",
);
const composed = mount(Badge, {
slots: {
default: () => [
h("span", { class: "icon" }, "•"),
h(BadgeLabel, {}, { default: () => "新" }),
],
},
});
expect(composed.findAll("[data-slot=badge-label]")).toHaveLength(1);
expect(composed.find(".icon").exists()).toBe(true);
});
it("switches between text and composed slot content dynamically", async () => {
const mode = ref<"text" | "composed">("text");
const w = mount(
defineComponent({
components: { Badge, BadgeLabel },
setup: () => ({ mode }),
template: `
99+
•
新
`,
}),
);
expect(w.get("[data-slot=badge-label]").text()).toBe("99+");
mode.value = "composed";
await nextTick();
expect(w.findAll("[data-slot=badge-label]")).toHaveLength(1);
expect(w.find(".dot").exists()).toBe(true);
mode.value = "text";
await nextTick();
expect(w.get("[data-slot=badge-label]").text()).toBe("99+");
expect(w.find(".dot").exists()).toBe(false);
w.unmount();
});
it("ignores comments and whitespace when deciding auto-wrap", () => {
const w = mount(Badge, {
slots: {
default: () => [
createCommentVNode("c"),
createTextVNode(" "),
createTextVNode("3"),
],
},
});
expect(w.findAll("[data-slot=badge-label]")).toHaveLength(1);
expect(w.get("[data-slot=badge-label]").text()).toBe("3");
const empty = mount(Badge, {
slots: { default: () => [createCommentVNode("only")] },
});
expect(empty.findAll("[data-slot=badge-label]")).toHaveLength(0);
});
it("applies placement, color, size and variant classes", () => {
const w = mount(Badge, {
props: {
color: "danger",
placement: "bottom-left",
size: "lg",
variant: "soft",
},
slots: { default: () => "9" },
});
const classes = w.get("[data-slot=badge]").classes();
expect(classes).toContain("hv-badge--danger");
expect(classes).toContain("hv-badge--bottom-left");
expect(classes).toContain("hv-badge--lg");
expect(classes).toContain("hv-badge--soft");
});
it("anchors children without becoming a link", () => {
const w = mount(BadgeAnchor, {
slots: {
default: () => [
h("img", { alt: "a" }),
h(Badge, {}, { default: () => "1" }),
],
},
});
const root = w.get("[data-slot=badge-anchor]");
expect(root.element.tagName).toBe("SPAN");
expect(root.element.closest("a")).toBeNull();
expect(root.classes()).toContain("hv-badge-anchor");
expect(w.find("img").exists()).toBe(true);
expect(w.find("[data-slot=badge]").exists()).toBe(true);
});
it("keeps badge in the basic demo overlapping its avatar anchor", () => {
const w = mount(BadgeBasicDemo, { attachTo: document.body });
const anchors = w.findAll("[data-slot=badge-anchor]");
expect(anchors).toHaveLength(3);
for (const anchor of anchors) {
expect(anchor.find("[data-slot=avatar]").exists()).toBe(true);
expect(anchor.find("[data-slot=badge]").exists()).toBe(true);
}
w.unmount();
});
it("updates attrs and class dynamically and merges caller class", async () => {
const extra = ref>({});
const classValue = ref("badge-extra");
const w = mount(
defineComponent({
components: { Badge },
setup: () => ({ extra, classValue }),
template: `A`,
}),
{ attachTo: document.body },
);
const root = () => w.get("[data-slot=badge]");
expect(root().classes()).toContain("hv-badge");
expect(root().classes()).toContain("badge-extra");
expect(root().attributes("title")).toBeUndefined();
extra.value = { title: "t1", "data-x": "1" };
classValue.value = ["badge-extra", { "badge-more": true }];
await nextTick();
expect(root().attributes("title")).toBe("t1");
expect(root().attributes("data-x")).toBe("1");
expect(root().classes()).toContain("badge-more");
extra.value = { title: "t2" };
await nextTick();
expect(root().attributes("title")).toBe("t2");
expect(root().attributes("data-x")).toBeUndefined();
extra.value = {};
await nextTick();
expect(root().attributes("title")).toBeUndefined();
extra.value = { "aria-label": "count" };
await nextTick();
expect(root().attributes("aria-label")).toBe("count");
expect(root().classes()).toContain("hv-badge");
w.unmount();
});
it("keeps public prop types constrained", async () => {
const { Badge: BadgeComp } = await import("../packages/vue/src");
// @ts-expect-error color must be a known badge color
const badColor: InstanceType["$props"]["color"] = "neon";
// @ts-expect-error placement must be a known corner
const badPlacement: InstanceType["$props"]["placement"] =
"center";
// @ts-expect-error variant must be primary | secondary | soft
const badVariant: InstanceType["$props"]["variant"] =
"ghost";
// @ts-expect-error size must be sm | md | lg
const badSize: InstanceType["$props"]["size"] = "xl";
void badColor;
void badPlacement;
void badVariant;
void badSize;
const ok: InstanceType["$props"] = {
color: "accent",
placement: "bottom-left",
variant: "soft",
size: "sm",
};
expect(ok.placement).toBe("bottom-left");
});
});
describe("Alert", () => {
function mountAlert(
props: Record = {},
slots: Record = {},
) {
return mount(Alert, {
props,
slots: {
default: () => [
h(AlertIndicator),
h(
AlertContent,
{},
{
default: () => [
h(AlertTitle, {}, { default: () => "标题" }),
h(AlertDescription, {}, { default: () => "说明" }),
],
},
),
],
...slots,
},
});
}
it("renders default status with baseline parts and default icon", () => {
const w = mountAlert();
expect(w.get("[data-slot=alert-root]").classes()).toContain("hv-alert");
expect(w.get("[data-slot=alert-root]").classes()).toContain(
"hv-alert--default",
);
expect(w.get("[data-slot=alert-indicator]").classes()).toContain(
"hv-alert__indicator",
);
expect(w.get("[data-slot=alert-content]").classes()).toContain(
"hv-alert__content",
);
expect(w.get("[data-slot=alert-title]").element.tagName).toBe("P");
expect(w.get("[data-slot=alert-title]").text()).toBe("标题");
expect(w.get("[data-slot=alert-description]").element.tagName).toBe("SPAN");
expect(w.get("[data-slot=alert-description]").text()).toBe("说明");
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(true);
expect(
w.get("[data-slot=alert-default-icon]").attributes("aria-hidden"),
).toBe("true");
});
it("updates the default icon path with status", () => {
const paths: Record = {};
for (const status of [
"default",
"accent",
"success",
"warning",
"danger",
] as const) {
const w = mountAlert({ status });
paths[status] = w
.get("[data-slot=alert-default-icon] path")
.attributes("d");
expect(w.get("[data-slot=alert-root]").classes()).toContain(
`hv-alert--${status}`,
);
w.unmount();
}
expect(paths.default).toBe(paths.accent);
expect(new Set(Object.values(paths)).size).toBe(4);
});
it("replaces the default icon with custom indicator content", () => {
const w = mount(Alert, {
slots: {
default: () =>
h(
AlertIndicator,
{},
{ default: () => h("span", { class: "spin" }, "…") },
),
},
});
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(false);
expect(w.find(".spin").exists()).toBe(true);
});
it("merges caller class on AlertIndicator and keeps base class", () => {
const w = mount(Alert, {
slots: {
default: () =>
h(
AlertIndicator,
{ class: ["relative", { "text-warning": true }] },
{ default: () => h("span", "x") },
),
},
});
const classes = w.get("[data-slot=alert-indicator]").classes();
expect(classes).toContain("hv-alert__indicator");
expect(classes).toContain("relative");
expect(classes).toContain("text-warning");
});
it("falls back to the default icon for empty or comment-only indicator slots", async () => {
const mode = ref<"empty" | "custom" | "comments">("empty");
const w = mount(
defineComponent({
components: { Alert, AlertIndicator },
setup: () => ({ mode }),
template: `
自定义
`,
}),
);
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(true);
expect(
w.find("[data-slot=alert-default-icon] path").attributes("d"),
).toContain("13.5 8");
mode.value = "custom";
await nextTick();
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(false);
expect(w.text()).toContain("自定义");
mode.value = "comments";
await nextTick();
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(true);
mode.value = "custom";
await nextTick();
expect(w.find("[data-slot=alert-default-icon]").exists()).toBe(false);
w.unmount();
});
it("resets nested SurfaceContext to default", () => {
const seen = ref();
const Probe = defineComponent({
setup() {
const ctx = inject(surfaceContextKey, undefined);
seen.value = ctx?.variant.value;
return () => h("span", "probe");
},
});
const Outer = defineComponent({
components: { Alert, Probe },
template: `
`,
});
// Simulate an outer surface providing secondary before Alert.
const Wrapped = defineComponent({
setup(_, { slots }) {
// provide happens in child tree via a local surface-like parent
return () => h("div", slots.default?.());
},
});
mount(
defineComponent({
components: { Alert, Probe, Wrapped },
setup() {
return {};
},
template: `
`,
}),
);
expect(seen.value).toBe("default");
});
it("keeps the six combinations in the alert-basic demo", () => {
const w = mount(AlertBasicDemo, { attachTo: document.body });
const roots = w.findAll("[data-slot=alert-root]");
expect(roots).toHaveLength(6);
expect(w.text()).toContain("新功能已上线");
expect(w.text()).toContain("有可用更新");
expect(w.text()).toContain("无法连接到服务器");
expect(w.text()).toContain("检查网络连接");
expect(w.text()).toContain("个人资料已更新");
expect(w.text()).toContain("正在处理你的请求");
expect(w.text()).toContain("计划维护");
expect(w.findAll("[data-slot=alert-default-icon]").length).toBeGreaterThan(
0,
);
expect(w.find("[data-slot=spinner]").exists() || w.text()).toBeTruthy();
w.unmount();
});
it("updates attrs, class and status dynamically", async () => {
const status = ref<"default" | "danger">("default");
const extra = ref>({});
const classValue = ref("alert-custom");
const w = mount(
defineComponent({
components: { Alert },
setup: () => ({ status, extra, classValue }),
template: `内容`,
}),
{ attachTo: document.body },
);
const root = () => w.get("[data-slot=alert-root]");
expect(root().classes()).toContain("hv-alert--default");
expect(root().classes()).toContain("alert-custom");
status.value = "danger";
extra.value = { "aria-live": "polite", "data-x": "1" };
classValue.value = ["alert-custom", { "alert-more": true }];
await nextTick();
expect(root().classes()).toContain("hv-alert--danger");
expect(root().classes()).not.toContain("hv-alert--default");
expect(root().classes()).toContain("alert-more");
expect(root().attributes("aria-live")).toBe("polite");
extra.value = {};
await nextTick();
expect(root().attributes("aria-live")).toBeUndefined();
expect(root().attributes("data-x")).toBeUndefined();
extra.value = { role: "status" };
await nextTick();
expect(root().attributes("role")).toBe("status");
w.unmount();
});
it("keeps public prop types constrained", async () => {
const { Alert: AlertComp } = await import("../packages/vue/src");
// @ts-expect-error status must be a known alert status
const badStatus: InstanceType["$props"]["status"] =
"info";
void badStatus;
const ok: InstanceType["$props"] = { status: "warning" };
expect(ok.status).toBe("warning");
});
});
describe("M10–M11 demo inventory", () => {
const expected: Record = {
badge: [
"basic",
"variants",
"sizes",
"colors",
"placements",
"dot",
"with-content",
"custom-styles",
],
alert: ["basic", "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("");
}
}
});
it("documents Vue class binding instead of React className", () => {
for (const docName of ["badge", "alert"]) {
const document = readFileSync(
`apps/docs/components/${docName}.md`,
"utf8",
);
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
expect(document).not.toMatch(/\|\s*`children`\s*\|/);
expect(document).toContain("`class`");
expect(document).toContain("Vue class 绑定");
expect(document).toMatch(/string\s*\\\|\s*Array\s*\\\|\s*Object/);
}
for (const name of ["badge-basic", "alert-basic", "alert-custom-styles"]) {
const source = readFileSync(`apps/docs/demos/${name}.vue`, "utf8");
expect(source).not.toContain("className");
}
});
it("uses Tabler icons and library components in demos", () => {
const withContent = readFileSync(
"apps/docs/demos/badge-with-content.vue",
"utf8",
);
expect(withContent).toContain("@tabler/icons-vue");
expect(withContent).not.toContain("