import { describe, it, expect, vi, afterEach } from "vitest"; import { mount } from "@vue/test-utils"; import { defineComponent, h, nextTick, ref } from "vue"; import { Breadcrumbs, BreadcrumbsItem, EmptyState, Fieldset, Typography, TypographyCode, TypographyHeading, TypographyParagraph, TypographyProse, } from "../packages/vue/src"; import type { BreadcrumbsItemProps } from "../packages/vue/src"; afterEach(() => { document.body.innerHTML = ""; }); describe("Typography", () => { it("renders body text as a paragraph with baseline classes", () => { const w = mount(Typography, { slots: { default: "正文" } }); const root = w.get("[data-slot=typography]"); expect(root.element.tagName).toBe("P"); expect(root.classes()).toContain("hv-typography"); expect(root.classes()).toContain("hv-typography--body"); expect(root.classes()).toContain("hv-typography--align-start"); expect(root.classes()).toContain("hv-typography--color-default"); expect(root.attributes("data-type")).toBe("body"); }); it("maps every type to its default semantic element", () => { const cases = [ ["h1", "H1"], ["h2", "H2"], ["h3", "H3"], ["h4", "H4"], ["h5", "H5"], ["h6", "H6"], ["body", "P"], ["body-sm", "P"], ["body-xs", "P"], ["code", "CODE"], ] as const; for (const [type, tag] of cases) { const w = mount(Typography, { props: { type }, slots: { default: type }, }); const root = w.get("[data-slot=typography]"); expect(root.element.tagName).toBe(tag); expect(root.attributes("data-type")).toBe(type); expect(root.classes()).toContain(`hv-typography--${type}`); w.unmount(); } }); it("renders Heading, Paragraph and Code primitives", () => { const heading = mount(TypographyHeading, { props: { level: 3 }, slots: { default: "标题" }, }); expect(heading.element.tagName).toBe("H3"); expect(heading.attributes("data-type")).toBe("h3"); expect(heading.classes()).toContain("hv-typography--h3"); const paragraph = mount(TypographyParagraph, { props: { size: "sm" }, slots: { default: "正文" }, }); expect(paragraph.element.tagName).toBe("P"); expect(paragraph.attributes("data-type")).toBe("body-sm"); expect(paragraph.classes()).toContain("hv-typography--body-sm"); const code = mount(TypographyCode, { slots: { default: "code" } }); expect(code.element.tagName).toBe("CODE"); expect(code.attributes("data-type")).toBe("code"); }); it("supports the as override while keeping visual type classes", () => { const w = mount(Typography, { props: { type: "h1", as: "h2" }, slots: { default: "视觉 H1,语义 H2" }, }); const root = w.get("[data-slot=typography]"); expect(root.element.tagName).toBe("H2"); expect(root.attributes("data-type")).toBe("h1"); expect(root.classes()).toContain("hv-typography--h1"); }); it("applies align, color, weight and truncate variants", () => { const w = mount(Typography, { props: { align: "center", color: "muted", weight: "bold", truncate: true, }, }); const root = w.get("[data-slot=typography]"); expect(root.classes()).toContain("hv-typography--align-center"); expect(root.classes()).toContain("hv-typography--color-muted"); expect(root.classes()).toContain("hv-typography--weight-bold"); expect(root.classes()).toContain("hv-typography--truncate"); }); it("renders Prose with its baseline root and class", () => { const w = mount(TypographyProse, { slots: { default: () => h("p", "段落") }, }); const root = w.get("[data-slot=prose]"); expect(root.element.tagName).toBe("DIV"); expect(root.classes()).toContain("hv-typography-prose"); expect(root.text()).toBe("段落"); }); it("merges dynamic attributes and class values without stale keys", async () => { const extra = ref>({}); const classValue = ref("base-extra"); const w = mount( defineComponent({ components: { Typography }, setup() { return { extra, classValue }; }, template: `文本`, }), ); let root = w.get("[data-slot=typography]"); expect(root.attributes("aria-label")).toBeUndefined(); expect(root.classes()).toContain("base-extra"); extra.value = { "aria-label": "说明", "data-testid": "typo" }; classValue.value = ["array-extra", { "object-extra": true }]; await nextTick(); root = w.get("[data-slot=typography]"); expect(root.attributes("aria-label")).toBe("说明"); expect(root.attributes("data-testid")).toBe("typo"); expect(root.classes()).toContain("array-extra"); expect(root.classes()).toContain("object-extra"); expect(root.classes()).not.toContain("base-extra"); extra.value = {}; classValue.value = undefined; await nextTick(); root = w.get("[data-slot=typography]"); expect(root.attributes("aria-label")).toBeUndefined(); expect(root.classes()).toContain("hv-typography"); extra.value = { title: "再次出现" }; await nextTick(); expect(w.get("[data-slot=typography]").attributes("title")).toBe( "再次出现", ); w.unmount(); }); it("updates type and slot content dynamically", async () => { const type = ref<"body" | "h2">("body"); const text = ref("第一版"); const w = mount( defineComponent({ components: { Typography }, setup() { return { type, text }; }, template: `{{ text }}`, }), ); expect(w.get("[data-slot=typography]").element.tagName).toBe("P"); type.value = "h2"; text.value = "第二版"; await nextTick(); const root = w.get("[data-slot=typography]"); expect(root.element.tagName).toBe("H2"); expect(root.classes()).toContain("hv-typography--h2"); expect(root.text()).toBe("第二版"); w.unmount(); }); it("keeps public typography types constrained", () => { const props: InstanceType["$props"] = { type: "h1", align: "center", color: "muted", weight: "bold", truncate: true, as: "h2", }; // @ts-expect-error type must be one of the documented typography types const badType: InstanceType["$props"]["type"] = "h7"; // @ts-expect-error align must be start | center | end | justify const badAlign: InstanceType["$props"]["align"] = "left"; // @ts-expect-error Heading level must be 1-6 const badLevel: InstanceType["$props"]["level"] = 7; void props; void badType; void badAlign; void badLevel; expect(props.type).toBe("h1"); }); }); describe("EmptyState", () => { it("renders the baseline fallback when no content is provided", () => { const w = mount(EmptyState); const root = w.get("[data-slot=empty-state]"); expect(root.element.tagName).toBe("DIV"); expect(root.classes()).toContain("hv-empty-state"); expect(root.text()).toBe("No results found"); }); it("falls back for empty, whitespace and conditional slots", () => { const empty = mount(EmptyState, { slots: { default: () => "" } }); expect(empty.text()).toBe("No results found"); const whitespace = mount(EmptyState, { slots: { default: () => " " }, }); expect(whitespace.text()).toBe("No results found"); const conditional = mount( defineComponent({ components: { EmptyState }, setup() { return { show: ref(false) }; }, template: `内容`, }), ); expect(conditional.text()).toBe("No results found"); }); it("shows conditional content and restores the fallback dynamically", async () => { const show = ref(false); const w = mount( defineComponent({ components: { EmptyState }, setup() { return { show }; }, template: `没有结果`, }), ); expect(w.text()).toBe("No results found"); show.value = true; await nextTick(); expect(w.text()).toBe("没有结果"); show.value = false; await nextTick(); expect(w.text()).toBe("No results found"); }); it("merges dynamic attrs, class values and custom root elements", async () => { const extra = ref>({}); const w = mount( defineComponent({ components: { EmptyState }, setup() { return { extra }; }, template: `内容`, }), ); let root = w.get("[data-slot=empty-state]"); expect(root.element.tagName).toBe("SECTION"); expect(root.classes()).toContain("hv-empty-state"); expect(root.classes()).toContain("a"); expect(root.classes()).toContain("b"); extra.value = { "aria-label": "空", "data-testid": "empty" }; await nextTick(); root = w.get("[data-slot=empty-state]"); expect(root.attributes("aria-label")).toBe("空"); expect(root.attributes("data-testid")).toBe("empty"); extra.value = {}; await nextTick(); expect(w.get("[data-slot=empty-state]").attributes("aria-label")).toBe( undefined, ); w.unmount(); }); it("the collection example shows the empty slot only when the list has no items", async () => { const demo = (await import("../apps/docs/demos/empty-state-collection.vue")) .default; const w = mount(demo, { attachTo: document.body }); await nextTick(); expect(w.text()).toContain("设计规范"); expect(w.text()).not.toContain("No results found"); const input = w.get("input"); await input.setValue("季度"); await nextTick(); expect(w.text()).toContain("季度报告"); expect(w.text()).not.toContain("设计规范"); await input.setValue("不存在的关键词"); await nextTick(); expect(w.text()).toContain("没有匹配的文件。"); w.unmount(); }); }); describe("Breadcrumbs", () => { const items = () => h(Breadcrumbs, null, { default: () => [ h(BreadcrumbsItem, { href: "#home" }, () => "首页"), h(BreadcrumbsItem, { href: "#products" }, () => "产品"), h(BreadcrumbsItem, null, () => "笔记本电脑"), ], }); it("renders an ordered list with a default accessible name", () => { const w = mount(items()); const root = w.get("[data-slot=breadcrumbs]"); expect(root.element.tagName).toBe("OL"); expect(root.attributes("aria-label")).toBe("Breadcrumbs"); expect(root.classes()).toContain("hv-breadcrumbs"); expect(w.findAll("[data-slot=breadcrumbs-item]")).toHaveLength(3); }); it("marks the last item current and renders separators only between items", () => { const w = mount(items()); const listItems = w.findAll("[data-slot=breadcrumbs-item]"); const links = w.findAll("a"); const current = listItems[2]; expect(links).toHaveLength(2); expect(current.attributes("data-current")).toBe("true"); expect(current.attributes("data-disabled")).toBe("true"); const currentLink = current.get("[role=link]"); expect(currentLink.element.tagName).toBe("SPAN"); expect(currentLink.attributes("aria-current")).toBe("page"); expect(currentLink.attributes("aria-disabled")).toBe("true"); expect(currentLink.attributes("tabindex")).toBe("-1"); expect(currentLink.attributes("data-current")).toBe("true"); expect(currentLink.attributes("href")).toBeUndefined(); expect(currentLink.attributes("target")).toBeUndefined(); const separators = w.findAll("[data-slot=breadcrumbs-separator]"); expect(separators).toHaveLength(2); expect(separators[0].classes()).toContain("hv-breadcrumbs__separator"); expect(separators[0].attributes("aria-hidden")).toBe("true"); expect(separators[0].attributes("role")).toBe("presentation"); }); it("supports a custom accessible name and custom root/item elements", () => { const w = mount(Breadcrumbs, { props: { "aria-label": "当前位置", as: "nav" }, slots: { default: () => h(BreadcrumbsItem, { as: "div" }, () => "当前页面"), }, }); const root = w.get("[data-slot=breadcrumbs]"); expect(root.element.tagName).toBe("NAV"); expect(root.attributes("aria-label")).toBe("当前位置"); expect(w.get("[data-slot=breadcrumbs-item]").element.tagName).toBe("DIV"); }); it("passes link props and item attributes to their documented targets", () => { const w = mount(Breadcrumbs, { slots: { default: () => [ h( BreadcrumbsItem, { href: "https://example.com", target: "_blank", rel: "noreferrer", download: "report.pdf", "data-custom": "bar", }, () => "文档", ), h(BreadcrumbsItem, null, () => "中间"), h(BreadcrumbsItem, null, () => "当前"), ], }, }); const link = w.get("a"); expect(link.attributes("href")).toBe("https://example.com"); expect(link.attributes("target")).toBe("_blank"); expect(link.attributes("rel")).toBe("noreferrer"); expect(link.attributes("download")).toBe("report.pdf"); expect( w.get("[data-slot=breadcrumbs-item]").attributes("data-custom"), ).toBe("bar"); const middle = w.findAll("[data-slot=link]")[1]; expect(middle.element.tagName).toBe("SPAN"); expect(middle.attributes("role")).toBe("link"); expect(middle.attributes("tabindex")).toBe("0"); expect(middle.attributes("href")).toBeUndefined(); expect(middle.attributes("aria-disabled")).toBeUndefined(); }); it("disables all links at the root and blocks click activation", async () => { const clicks = vi.fn(); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup() { return { clicks }; }, template: ` 首页 当前 `, }), { attachTo: document.body }, ); const link = w.get("[data-slot=link]"); expect(link.element.tagName).toBe("SPAN"); expect(link.attributes("href")).toBeUndefined(); expect(link.attributes("target")).toBeUndefined(); expect(link.attributes("aria-disabled")).toBe("true"); expect(link.attributes("tabindex")).toBe("-1"); expect(link.attributes("data-disabled")).toBe("true"); await link.trigger("click"); expect(clicks).not.toHaveBeenCalled(); w.unmount(); }); it("disables a single item without disabling its siblings", async () => { const clicks = vi.fn(); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup() { return { clicks }; }, template: ` 首页 产品 当前 `, }), { attachTo: document.body }, ); const links = w.findAll("[data-slot=link]"); expect(links[0].element.tagName).toBe("SPAN"); expect(links[0].attributes("aria-disabled")).toBe("true"); expect(links[0].attributes("href")).toBeUndefined(); expect(links[1].element.tagName).toBe("A"); expect(links[1].attributes("aria-disabled")).toBeUndefined(); await links[0].trigger("click"); expect(clicks).not.toHaveBeenCalled(); w.unmount(); }); it("clones custom separator content with the baseline class", () => { const w = mount(Breadcrumbs, { slots: { separator: () => h("span", { class: "custom-sep" }, "›"), default: () => [ h(BreadcrumbsItem, { href: "#" }, () => "首页"), h(BreadcrumbsItem, null, () => "当前"), ], }, }); const separator = w.get("[data-slot=breadcrumbs-separator]"); expect(separator.element.tagName).toBe("SPAN"); expect(separator.classes()).toContain("custom-sep"); expect(separator.classes()).toContain("hv-breadcrumbs__separator"); expect(separator.text()).toBe("›"); }); it("moves the current state when conditional items change", async () => { const showLast = ref(false); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup() { return { showLast }; }, template: ` 首页 产品 笔记本电脑 `, }), ); let listItems = w.findAll("[data-slot=breadcrumbs-item]"); expect(listItems).toHaveLength(2); expect(listItems[1].attributes("data-current")).toBe("true"); showLast.value = true; await nextTick(); listItems = w.findAll("[data-slot=breadcrumbs-item]"); expect(listItems).toHaveLength(3); expect(listItems[1].attributes("data-current")).toBeUndefined(); expect(listItems[2].attributes("data-current")).toBe("true"); w.unmount(); }); it("provides scoped item state to the default slot", () => { const w = mount(Breadcrumbs, { slots: { default: () => h(BreadcrumbsItem, null, { default: ({ isCurrent, isDisabled }: any) => `${isCurrent ? "current" : "link"}:${isDisabled ? "disabled" : "enabled"}`, }), }, }); expect(w.text()).toBe("current:disabled"); }); it("merges class values on both the item and its link", async () => { const classValue = ref("first"); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup() { return { classValue }; }, template: ` 首页 当前 `, }), ); let item = w.get("[data-slot=breadcrumbs-item]"); let link = w.get("a"); expect(item.classes()).toContain("first"); expect(link.classes()).toContain("first"); classValue.value = ["second", { third: true }]; await nextTick(); item = w.get("[data-slot=breadcrumbs-item]"); link = w.get("a"); expect(item.classes()).toContain("second"); expect(item.classes()).toContain("third"); expect(link.classes()).toContain("second"); expect(link.classes()).not.toContain("first"); w.unmount(); }); it("keeps public breadcrumbs types constrained", () => { const props: InstanceType["$props"] = { ariaLabel: "当前位置", isDisabled: true, as: "nav", }; const item: BreadcrumbsItemProps = { href: "#", target: "_blank", rel: "noreferrer", download: true, isDisabled: false, as: "li", }; const internalItem: BreadcrumbsItemProps = { // @ts-expect-error separatorSlot is internal Root wiring. separatorSlot: () => null, }; // @ts-expect-error isDisabled must be boolean const badRootDisabled: InstanceType< typeof Breadcrumbs >["$props"]["isDisabled"] = "yes"; // @ts-expect-error href must be a string const badHref: BreadcrumbsItemProps["href"] = 42; void props; void item; void internalItem; void badRootDisabled; void badHref; expect(props.isDisabled).toBe(true); }); it("inherits Fieldset disabled and blocks real link activation", async () => { const clicks = vi.fn(); const w = mount( defineComponent({ components: { Fieldset, Breadcrumbs, BreadcrumbsItem }, setup: () => ({ clicks }), template: `
首页 产品
`, }), { attachTo: document.body }, ); await nextTick(); const link = w.get("[data-slot=link]"); expect(link.element.tagName).toBe("SPAN"); expect(link.attributes("href")).toBeUndefined(); expect(link.attributes("aria-disabled")).toBe("true"); expect(link.attributes("tabindex")).toBe("-1"); await link.trigger("click"); expect(clicks).not.toHaveBeenCalled(); // Keyboard activation must also be blocked. await link.trigger("keydown", { key: "Enter" }); expect(clicks).not.toHaveBeenCalled(); w.unmount(); }); it("re-enables and disables again with ancestor Fieldset", async () => { const disabled = ref(true); const clicks = vi.fn(); const w = mount( defineComponent({ components: { Fieldset, Breadcrumbs, BreadcrumbsItem }, setup: () => ({ disabled, clicks }), template: `
首页 当前
`, }), { attachTo: document.body }, ); await nextTick(); expect(w.get("[data-slot=link]").element.tagName).toBe("SPAN"); disabled.value = false; await nextTick(); await nextTick(); const enabled = w.get("[data-slot=link]"); expect(enabled.element.tagName).toBe("A"); expect(enabled.attributes("href")).toBe("#home"); expect(enabled.attributes("aria-disabled")).toBeUndefined(); await enabled.trigger("click"); expect(clicks).toHaveBeenCalledTimes(1); disabled.value = true; await nextTick(); await nextTick(); const again = w.get("[data-slot=link]"); expect(again.element.tagName).toBe("SPAN"); expect(again.attributes("href")).toBeUndefined(); await again.trigger("click"); expect(clicks).toHaveBeenCalledTimes(1); w.unmount(); }); it("does not let item isDisabled=false escape ancestor Fieldset disable", async () => { const clicks = vi.fn(); const w = mount( defineComponent({ components: { Fieldset, Breadcrumbs, BreadcrumbsItem }, setup: () => ({ clicks }), template: `
首页 当前
`, }), { attachTo: document.body }, ); await nextTick(); const link = w.get("[data-slot=link]"); expect(link.element.tagName).toBe("SPAN"); expect(link.attributes("href")).toBeUndefined(); await link.trigger("click"); expect(clicks).not.toHaveBeenCalled(); w.unmount(); }); it("restores a real link when the current item becomes a middle item", async () => { const showDetail = ref(false); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup: () => ({ showDetail }), template: ` Home Products Detail `, }), { attachTo: document.body }, ); await nextTick(); await nextTick(); let items = w.findAll("[data-slot=breadcrumbs-item]"); expect(items).toHaveLength(2); expect(items[1].find("a").exists()).toBe(false); expect(items[1].get("[data-slot=link]").element.tagName).toBe("SPAN"); showDetail.value = true; await nextTick(); await nextTick(); items = w.findAll("[data-slot=breadcrumbs-item]"); expect(items).toHaveLength(3); const productLink = items[1].find("a"); expect(productLink.exists()).toBe(true); expect(productLink.attributes("href")).toBe("#products"); expect(items[1].get("[data-slot=link]").element.tagName).toBe("A"); expect(items[2].get("[data-slot=link]").element.tagName).toBe("SPAN"); expect(items[2].attributes("data-current")).toBe("true"); showDetail.value = false; await nextTick(); await nextTick(); items = w.findAll("[data-slot=breadcrumbs-item]"); expect(items).toHaveLength(2); expect(items[1].find("a").exists()).toBe(false); expect(items[1].get("[data-slot=link]").element.tagName).toBe("SPAN"); expect(items[1].attributes("data-current")).toBe("true"); w.unmount(); }); it("restores navigation when href appears on a non-current item", async () => { const href = ref(undefined); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup: () => ({ href }), template: ` Home Products Detail `, }), { attachTo: document.body }, ); await nextTick(); await nextTick(); let link = w.findAll("[data-slot=link]")[1]; expect(link.element.tagName).toBe("SPAN"); expect(link.attributes("href")).toBeUndefined(); href.value = "#products"; await nextTick(); await nextTick(); link = w.findAll("[data-slot=link]")[1]; expect(link.element.tagName).toBe("A"); expect(link.attributes("href")).toBe("#products"); href.value = undefined; await nextTick(); await nextTick(); link = w.findAll("[data-slot=link]")[1]; expect(link.element.tagName).toBe("SPAN"); expect(link.attributes("href")).toBeUndefined(); w.unmount(); }); it("keeps unique aria-current and working middle links after reorder", async () => { const showDetail = ref(false); const w = mount( defineComponent({ components: { Breadcrumbs, BreadcrumbsItem }, setup: () => ({ showDetail }), template: ` A B C `, }), { attachTo: document.body }, ); showDetail.value = true; await nextTick(); await nextTick(); const current = w .findAll("[data-slot=link]") .filter((el) => el.attributes("aria-current") === "page"); expect(current).toHaveLength(1); expect(current[0].element.tagName).toBe("SPAN"); const anchors = w.findAll("a"); expect(anchors.map((a) => a.attributes("href"))).toEqual(["#a", "#b"]); for (const a of anchors) { expect(a.attributes("aria-current")).toBeUndefined(); expect(a.attributes("tabindex")).not.toBe("-1"); } w.unmount(); }); });