8f51b0e097
- 包根补齐固定上游 82 对 Xxx/XxxRoot 同对象别名(55 个缺失 Root 别名 + Dropdown 反向别名)与 59 个 RootProps/Menu/Dropdown 类型别名;不杜撰上游没有的名称(如 CheckboxGroupRoot、ToastRootProps) - tests/package 新增 api-root-aliases 契约:完整别名清单、82 对身份等价断言与 strict NodeNext/bundler 类型正反例 - progress-bar custom-value 演示回填公共 NumberField/Select,保持固定源文案/布局/范围/四格式与边界处理 - 修复 NumberFieldInput keydown 无条件 restore 还原同任务键入字符的缺陷,并入 number-field 回归 - 文档导航 dropdown 归位固定 meta.json 顺序并补顺序断言;修正“原生临时控件”陈述
407 lines
14 KiB
TypeScript
407 lines
14 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { readFileSync, readdirSync } from "node:fs";
|
|
import { mount, flushPromises, DOMWrapper } from "@vue/test-utils";
|
|
import { nextTick } from "vue";
|
|
import {
|
|
componentItems,
|
|
componentGroups,
|
|
guideGroups,
|
|
docsPath,
|
|
docsSection,
|
|
adjacentPages,
|
|
} from "../apps/docs/.vitepress/theme/navigation";
|
|
import { searchIndex } from "virtual:docs-data";
|
|
import { searchDocs } from "../apps/docs/.vitepress/theme/search";
|
|
const host = vi.hoisted(() => ({
|
|
route: { path: "/components/button" },
|
|
page: { headers: [] as { title: string; slug: string; children: [] }[] },
|
|
go: vi.fn(),
|
|
}));
|
|
vi.mock("vitepress", async () => {
|
|
const { reactive, ref } = await import("vue");
|
|
const route = reactive(host.route);
|
|
host.route = route;
|
|
const data = {
|
|
site: ref({ base: "/" }),
|
|
frontmatter: ref({ title: "Button 按钮", description: "按钮说明" }),
|
|
page: ref(host.page),
|
|
isDark: ref(false),
|
|
};
|
|
host.page = data.page.value;
|
|
const router = {
|
|
go: host.go.mockImplementation(async (value: string) => {
|
|
route.path = value;
|
|
}),
|
|
};
|
|
return {
|
|
withBase: (value: string) => value,
|
|
useRoute: () => route,
|
|
useData: () => data,
|
|
onContentUpdated: () => {},
|
|
useRouter: () => router,
|
|
};
|
|
});
|
|
import Layout from "../apps/docs/.vitepress/theme/Layout.vue";
|
|
import DocSearch from "../apps/docs/.vitepress/theme/DocSearch.vue";
|
|
import Demo from "../apps/docs/.vitepress/theme/Demo.vue";
|
|
import DocsComponents from "../apps/docs/.vitepress/theme/DocsComponents.vue";
|
|
const wrappers: ReturnType<typeof mount>[] = [];
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
host.go.mockImplementation(async (value: string) => {
|
|
host.route.path = value;
|
|
});
|
|
host.page.headers = [];
|
|
const storage = new Map<string, string>();
|
|
vi.stubGlobal("localStorage", {
|
|
getItem: (key: string) => storage.get(key) ?? null,
|
|
setItem: (key: string, value: string) => storage.set(key, String(value)),
|
|
removeItem: (key: string) => storage.delete(key),
|
|
clear: () => storage.clear(),
|
|
key: (index: number) => [...storage.keys()][index] ?? null,
|
|
get length() {
|
|
return storage.size;
|
|
},
|
|
});
|
|
Object.defineProperty(HTMLElement.prototype, "scrollIntoView", {
|
|
value: vi.fn(),
|
|
configurable: true,
|
|
writable: true,
|
|
});
|
|
});
|
|
afterEach(() => {
|
|
wrappers.splice(0).forEach((w) => w.unmount());
|
|
document.body.innerHTML = "";
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
function fixture(component: never, options = {}) {
|
|
const w = mount(component, { attachTo: document.body, ...options });
|
|
wrappers.push(w);
|
|
return w;
|
|
}
|
|
function keyboard(element: Element, key: string, extras = {}) {
|
|
element.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key,
|
|
bubbles: true,
|
|
cancelable: true,
|
|
...extras,
|
|
}),
|
|
);
|
|
}
|
|
function input() {
|
|
return document.querySelector<HTMLInputElement>(
|
|
'[role="combobox"][aria-controls="doc-search-results"]',
|
|
)!;
|
|
}
|
|
async function search(text: string) {
|
|
input().value = text;
|
|
input().dispatchEvent(new Event("input", { bubbles: true }));
|
|
await flushPromises();
|
|
}
|
|
|
|
it("keeps component navigation usable when a themed thumbnail fails", async () => {
|
|
const w = fixture(DocsComponents as never, {
|
|
props: { category: "buttons" },
|
|
});
|
|
const image = w.get("img.component-thumbnail-light");
|
|
expect(image.attributes("src")).toContain("light-button.png");
|
|
await image.trigger("error");
|
|
expect(w.get(".component-thumbnail-fallback").text()).toContain("Button");
|
|
expect(w.get('a[href="/components/button"]').text()).toBe("Button 按钮");
|
|
expect(w.findAll("img.component-thumbnail-dark")).toHaveLength(5);
|
|
});
|
|
|
|
it("uses a native version trigger, button-styled source anchor and detached theme controls", async () => {
|
|
host.route.path = "/components/button";
|
|
const w = fixture(Layout as never, { global: { stubs: { Content: true } } });
|
|
const version = w.get('[aria-label="版本信息"]');
|
|
expect(version.element.tagName).toBe("BUTTON");
|
|
const source = w.get('[aria-label="项目源码"]');
|
|
expect(source.element.tagName).toBe("A");
|
|
expect(source.attributes("href")).toBe(
|
|
"https://git.myjdemo.cn/maoyingjie/heroui-vue",
|
|
);
|
|
expect(source.classes()).toContain("hv-button--tertiary");
|
|
expect(w.get(".docs-theme-wide").classes()).toContain(
|
|
"hv-toggle-button-group--detached",
|
|
);
|
|
await version.trigger("click");
|
|
await flushPromises();
|
|
expect(document.querySelector(".version-panel")?.textContent).toContain(
|
|
"社区独立实现",
|
|
);
|
|
});
|
|
describe("documentation structure", () => {
|
|
it("keeps component pages in the pinned HeroUI meta.json order", () => {
|
|
// Upstream apps/docs/content/docs/cn/react/components/meta.json at
|
|
// ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960, plus the two real Vue pages
|
|
// (empty-state, header) at their alphabetical positions.
|
|
const pinned = [
|
|
"accordion",
|
|
"alert",
|
|
"alert-dialog",
|
|
"autocomplete",
|
|
"avatar",
|
|
"avatar-group",
|
|
"badge",
|
|
"breadcrumbs",
|
|
"button",
|
|
"button-group",
|
|
"calendar",
|
|
"card",
|
|
"checkbox",
|
|
"checkbox-group",
|
|
"chip",
|
|
"close-button",
|
|
"color-area",
|
|
"color-field",
|
|
"color-picker",
|
|
"color-slider",
|
|
"color-swatch",
|
|
"color-swatch-picker",
|
|
"combo-box",
|
|
"date-field",
|
|
"date-picker",
|
|
"date-range-picker",
|
|
"description",
|
|
"disclosure",
|
|
"disclosure-group",
|
|
"drawer",
|
|
"dropdown",
|
|
"empty-state",
|
|
"error-message",
|
|
"field-error",
|
|
"fieldset",
|
|
"form",
|
|
"header",
|
|
"input",
|
|
"input-group",
|
|
"input-otp",
|
|
"kbd",
|
|
"label",
|
|
"link",
|
|
"list-box",
|
|
"meter",
|
|
"modal",
|
|
"number-field",
|
|
"pagination",
|
|
"popover",
|
|
"progress-bar",
|
|
"progress-circle",
|
|
"radio-group",
|
|
"range-calendar",
|
|
"scroll-shadow",
|
|
"search-field",
|
|
"select",
|
|
"separator",
|
|
"skeleton",
|
|
"slider",
|
|
"spinner",
|
|
"surface",
|
|
"switch",
|
|
"table",
|
|
"tabs",
|
|
"tag-group",
|
|
"textarea",
|
|
"text-field",
|
|
"time-field",
|
|
"toast",
|
|
"toggle-button",
|
|
"toggle-button-group",
|
|
"toolbar",
|
|
"tooltip",
|
|
"typography",
|
|
];
|
|
expect(componentItems.map((item) => item.url)).toEqual(
|
|
pinned.map((slug) => "/components/" + slug),
|
|
);
|
|
});
|
|
it("makes every existing component page reachable exactly once in navigation and its category overview", () => {
|
|
const pages = readdirSync("apps/docs/components")
|
|
.filter((name) => name.endsWith(".md"))
|
|
.map((name) => "/components/" + name.slice(0, -3))
|
|
.sort();
|
|
expect(componentItems.map((item) => item.url).sort()).toEqual(pages);
|
|
expect(new Set(componentItems.map((item) => item.url)).size).toBe(
|
|
componentItems.length,
|
|
);
|
|
const overview = readFileSync("apps/docs/components.md", "utf8");
|
|
for (const item of componentItems)
|
|
expect(overview).toContain(`category="${item.category}"`);
|
|
for (const group of [...guideGroups, ...componentGroups])
|
|
for (const item of group.items)
|
|
expect(readFileSync("apps/docs" + item.url + ".md", "utf8")).toContain(
|
|
"title:",
|
|
);
|
|
});
|
|
it("normalizes root, html, hashes and deployed base paths without confusing guide and component routes", () => {
|
|
expect(docsPath("/docs/", "/docs/")).toBe("/introduction");
|
|
expect(docsPath("/docs", "/docs/")).toBe("/introduction");
|
|
expect(docsPath("/docs/components/button.html#api", "/docs/")).toBe(
|
|
"/components/button",
|
|
);
|
|
expect(docsSection("/components.html")).toBe("components");
|
|
expect(docsSection("/components-other")).toBe("guide");
|
|
expect(adjacentPages("/components/button").next?.url).toBe(
|
|
"/components/button-group",
|
|
);
|
|
expect(adjacentPages("/components/textarea").next?.url).toBe(
|
|
"/components/text-field",
|
|
);
|
|
expect(adjacentPages("/introduction").previous).toBeUndefined();
|
|
expect(adjacentPages("/not-found").next).toBeUndefined();
|
|
});
|
|
it("searches the actual fields and overview pages as well as component APIs", () => {
|
|
expect(searchIndex.some((entry) => entry.url === "/fields")).toBe(true);
|
|
expect(searchIndex.some((entry) => entry.url === "/components")).toBe(true);
|
|
expect(
|
|
searchDocs(searchIndex, "isPending").some((entry) =>
|
|
entry.url.startsWith("/components/button#"),
|
|
),
|
|
).toBe(true);
|
|
});
|
|
it("switches sidebar and previous/next from the current route, independently of optional component frontmatter", async () => {
|
|
const route = host.route;
|
|
route.path = "/components/button";
|
|
const w = fixture(Layout as never, {
|
|
global: {
|
|
stubs: {
|
|
Content: { template: '<div><h2 id="button">Button</h2></div>' },
|
|
},
|
|
},
|
|
});
|
|
await flushPromises();
|
|
expect(
|
|
w.get('.docs-sidebar [aria-current="page"]').attributes("href"),
|
|
).toBe("/components/button");
|
|
expect(w.findAll(".docs-sidebar a").length).toBe(componentItems.length + 1);
|
|
expect(w.get(".page-navigation .page-next").attributes("href")).toBe(
|
|
"/components/button-group",
|
|
);
|
|
route.path = "/guide";
|
|
await nextTick();
|
|
await flushPromises();
|
|
expect(w.findAll(".docs-sidebar a")).toHaveLength(4);
|
|
expect(
|
|
w.get('.docs-sidebar [aria-current="page"]').attributes("href"),
|
|
).toBe("/guide");
|
|
expect(w.get(".page-navigation .page-next").attributes("href")).toBe(
|
|
"/themes",
|
|
);
|
|
});
|
|
});
|
|
describe("documentation search using library fields and Modal", () => {
|
|
it("focuses input, preserves option ARIA and skips IME navigation", async () => {
|
|
const w = fixture(DocSearch as never);
|
|
await w.get('button[aria-label="搜索文档"]').trigger("click");
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(input());
|
|
expect(input().getAttribute("aria-activedescendant")).toBeNull();
|
|
await search("isPending");
|
|
const option = document.getElementById("search-result-0")!;
|
|
expect(option.getAttribute("role")).toBe("option");
|
|
expect(option.getAttribute("tabindex")).toBe("-1");
|
|
keyboard(input(), "ArrowDown", { isComposing: true });
|
|
await nextTick();
|
|
expect(input().getAttribute("aria-activedescendant")).toBe(
|
|
"search-result-0",
|
|
);
|
|
keyboard(input(), "ArrowDown");
|
|
await nextTick();
|
|
expect(input().getAttribute("aria-activedescendant")).toBe(
|
|
"search-result-1",
|
|
);
|
|
keyboard(input(), "Escape");
|
|
await flushPromises();
|
|
expect(document.querySelector('[data-slot="modal-dialog"]')).toBeNull();
|
|
});
|
|
it("restores trigger focus after Escape and opens with the keyboard shortcut", async () => {
|
|
const w = fixture(DocSearch as never);
|
|
const trigger = w.get('button[aria-label="搜索文档"]')
|
|
.element as HTMLButtonElement;
|
|
trigger.focus();
|
|
keyboard(document.body, "k", { ctrlKey: true });
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(input());
|
|
await search("no-such-component-xyz");
|
|
expect(document.querySelector('[role="status"]')?.textContent).toContain(
|
|
"没有找到",
|
|
);
|
|
keyboard(input(), "Escape");
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(trigger);
|
|
});
|
|
});
|
|
it("keeps preview expand/copy controls as library buttons and expands full same-SFC source", async () => {
|
|
const w = fixture(Demo as never, { props: { name: "button-basic" } });
|
|
await flushPromises();
|
|
const expand = w.get("button[aria-controls]");
|
|
expect(expand.classes()).toContain("hv-button");
|
|
expect(expand.attributes("aria-expanded")).toBe("false");
|
|
expect(w.get(".code-section").attributes("data-expanded")).toBe("false");
|
|
await expand.trigger("click");
|
|
await flushPromises();
|
|
expect(expand.attributes("aria-expanded")).toBe("true");
|
|
expect(w.get(".highlighted-source").text()).toContain("<Button");
|
|
});
|
|
|
|
it("navigates search results with Enter and keeps focus on the destination instead of the closed trigger", async () => {
|
|
const target = document.createElement("h2");
|
|
target.id = "button";
|
|
target.textContent = "Button";
|
|
document.body.append(target);
|
|
const w = fixture(DocSearch as never);
|
|
await w.get('button[aria-label="搜索文档"]').trigger("click");
|
|
await flushPromises();
|
|
await search("isPending");
|
|
keyboard(input(), "Enter");
|
|
await flushPromises();
|
|
expect(host.go).toHaveBeenCalledWith("/components/button#button");
|
|
expect(document.activeElement).toBe(target);
|
|
expect(document.querySelector('[data-slot="modal-dialog"]')).toBeNull();
|
|
});
|
|
it("uses the same current section in the mobile Drawer and releases it on close", async () => {
|
|
host.route.path = "/components/button";
|
|
const w = fixture(Layout as never, { global: { stubs: { Content: true } } });
|
|
await flushPromises();
|
|
await w.get(".mobile-menu").trigger("click");
|
|
await flushPromises();
|
|
const dialog = document.querySelector('[data-slot="drawer-dialog"]')!;
|
|
expect(dialog).not.toBeNull();
|
|
expect(
|
|
dialog
|
|
.querySelector('[aria-label="移动端文档导航"] [aria-current="page"]')
|
|
?.getAttribute("href"),
|
|
).toBe("/components/button");
|
|
(
|
|
dialog.querySelector('[aria-label="关闭文档目录"]') as HTMLButtonElement
|
|
).click();
|
|
await flushPromises();
|
|
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
|
});
|
|
it("closes the mobile TOC before navigating and focuses the requested heading", async () => {
|
|
host.route.path = "/components/button";
|
|
host.page.headers = [{ title: "Section", slug: "section", children: [] }];
|
|
const w = fixture(Layout as never, {
|
|
global: {
|
|
stubs: {
|
|
Content: { template: '<div><h2 id="section">Section</h2></div>' },
|
|
},
|
|
},
|
|
});
|
|
await flushPromises();
|
|
const toggle = w
|
|
.findAll("button")
|
|
.find((button) => button.text().includes("本页内容"))!;
|
|
await toggle.trigger("click");
|
|
await flushPromises();
|
|
const link = document.querySelector(".mobile-toc-content a")!;
|
|
await new DOMWrapper(link).trigger("click");
|
|
await flushPromises();
|
|
expect(host.go).toHaveBeenCalledWith("/components/button#section");
|
|
expect(document.activeElement).toBe(document.getElementById("section"));
|
|
expect(document.querySelector(".mobile-toc-content")).toBeNull();
|
|
});
|