feat(docs): align documentation shell with HeroUI baseline
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
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";
|
||||
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();
|
||||
}
|
||||
describe("documentation structure", () => {
|
||||
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();
|
||||
});
|
||||
Reference in New Issue
Block a user