Files
heroui-vue/tests/handbook-docs.test.ts

254 lines
8.3 KiB
TypeScript

import { afterEach, expect, it } from "vitest";
import { readFileSync, existsSync } from "node:fs";
import { mount, flushPromises } from "@vue/test-utils";
import {
guideGroups,
adjacentPages,
} from "../apps/docs/.vitepress/theme/navigation";
import DarkMode from "../apps/docs/demos/handbook-dark-mode.vue";
import Theme from "../apps/docs/demos/handbook-theme.vue";
import States from "../apps/docs/demos/handbook-states.vue";
import Composition from "../apps/docs/demos/handbook-composition.vue";
import Motion from "../apps/docs/demos/handbook-animation.vue";
import DocsColorSection from "../apps/docs/.vitepress/theme/DocsColorSection.vue";
import { colorValue } from "../apps/docs/.vitepress/theme/color-value";
it("copies computed colors in the original OKLCH format without losing alpha", () => {
expect(colorValue("oklab(0.5 0.1 0.1 / 0.2)")).toBe(
"oklch(0.5 0.1414 45 / 0.2)",
);
expect(colorValue("rgba(0, 0, 0, 0.5)")).toBe("oklch(0 0 0 / 0.5)");
expect(colorValue("rgb(255, 255, 255)")).toBe("oklch(1 0 0)");
expect(colorValue("rgb(255, 0, 0)")).toBe("oklch(0.628 0.2577 29.2339)");
expect(colorValue("oklch(99.11% 0 0)")).toBe("oklch(0.9911 0 0)");
});
import {
colorSections,
type SectionId,
} from "../apps/docs/.vitepress/theme/color-sections";
it("renders each original color layout without dropping groups or adding primitive theme duplicates", () => {
const counts = [10, 6, 10, 10, 10, 10, 8, 6, 8, 6, 8, 4];
Object.keys(colorSections).forEach((id, i) => {
const wrapper = mount(DocsColorSection, {
props: { section: id as SectionId },
});
wrappers.push(wrapper);
expect(wrapper.findAll("button.docs-swatch"), id).toHaveLength(counts[i]);
if (["accent", "success", "warning", "danger"].includes(id)) {
expect(wrapper.findAll(".docs-color-soft"), id).toHaveLength(2);
expect(
wrapper
.findAll('.docs-color-soft [data-color-label="Foreground"]')
.map((w) => w.attributes("data-color-expression")),
).toEqual([`var(--${id})`, `var(--${id})`]);
}
if (id === "field")
expect(
wrapper
.findAll('[data-color-label="Hover"]')
.map((w) => w.attributes("data-color-expression")),
).toEqual(
Array(2).fill(
"color-mix(in oklab, var(--field-background) 90%, var(--field-foreground) 10%)",
),
);
if (id === "foreground")
expect(
wrapper
.findAll('[data-theme="light"] [data-color-label]')
.map((w) => w.attributes("data-color-label")),
).toEqual(["前景", "弱化", "分段", "遮罩", "链接"]);
});
});
it("ships self-contained follow-up cards with real prerequisite and test paths", () => {
const ids = [
"HB-PACKAGE",
"HB-THEME",
"HB-DISPLAY",
"HB-FIELDS",
"HB-DATES",
"HB-COLORS",
"HB-COLLECTIONS",
"HB-OVERLAYS",
"HB-COMPOSITION",
];
const queue = readFileSync("docs/tasks/HB-QUEUE.md", "utf8");
for (const id of ids) {
expect(queue).toContain(`(${id}.md)`);
const raw = readFileSync(`docs/tasks/${id}.md`, "utf8");
const metadata = JSON.parse(
raw.match(/^<!-- task-metadata\n([\s\S]*?)\n-->/)![1],
) as { requires: string[]; checks: string[] };
for (const file of metadata.requires)
expect(existsSync(file), `${id}: ${file}`).toBe(true);
for (const command of metadata.checks)
for (const file of command
.split(" ")
.filter((token) => token.startsWith("tests/")))
expect(existsSync(file), `${id}: ${file}`).toBe(true);
expect(raw).toContain(`docs/tasks/results/${id}.md`);
}
});
const wrappers: ReturnType<typeof mount>[] = [];
function demo(component: Parameters<typeof mount>[0]) {
const wrapper = mount(component, { attachTo: document.body });
wrappers.push(wrapper);
return wrapper;
}
afterEach(async () => {
wrappers.splice(0).forEach((w) => w.unmount());
await flushPromises();
document.body.innerHTML = "";
});
const chapters = {
colors: [
"强调色",
"默认(中性色)",
"成功",
"警告",
"危险",
"前景色",
"背景色",
"表面色",
"表单字段",
"分隔线",
"其他",
"基础色",
"如何使用颜色",
"默认主题",
"自定义颜色",
],
themes: [
"工作原理",
"快速开始",
"自定义组件",
"导入策略",
"添加自定义颜色",
"变量参考",
"滚动条",
"资源",
],
"dark-mode": [
"在 Next.js 中使用 next-themes",
"使用自定义主题名称",
"在 React 中使用 useTheme",
"同时为两种主题设置样式",
],
styling: [
"基础样式",
"滚动条",
"基于状态的样式",
"渲染属性",
"BEM 类名",
"创建包装组件",
"CSS-in-JS 集成",
"响应式设计",
"CSS 模块",
"组件类名参考",
"下一步",
],
animation: [
"内置动画",
"CSS 动画",
"Framer Motion",
"渲染属性",
"无障碍",
"性能技巧",
"下一步",
],
composition: [
"与框架无关的样式",
"多态样式",
"直接应用类名",
"使用变体函数",
"复合组件",
"混合使用变体函数",
"自定义组件",
"自定义变体",
"自定义 DOM 元素",
"框架集成",
"下一步",
],
};
it("keeps all 56 pinned Handbook topics in source order and every preview executable", () => {
for (const [page, headings] of Object.entries(chapters)) {
const raw = readFileSync(`apps/docs/${page}.md`, "utf8");
const prose = raw.replace(/```[\s\S]*?```/g, "");
expect([...prose.matchAll(/^## (.+)$/gm)].map((m) => m[1])).toEqual(
headings,
);
for (const [, name] of raw.matchAll(/<ComponentPreview name="([^"]+)"/g))
expect(existsSync(`apps/docs/demos/${name}.vue`), name).toBe(true);
}
const handbook = guideGroups.find((group) => group.title === "手册")!;
expect(handbook.items.map((item) => item.url)).toEqual([
"/colors",
"/themes",
"/dark-mode",
"/styling",
"/animation",
"/composition",
"/fields",
]);
expect(adjacentPages("/guide").next?.url).toBe("/colors");
expect(adjacentPages("/themes").next?.url).toBe("/dark-mode");
});
it("keeps theme preview local and applies a chosen mode to real components", async () => {
const original = document.documentElement.className;
const wrapper = demo(DarkMode);
await wrapper.get("select").setValue("dark");
expect(wrapper.get("section").classes()).toContain("dark");
expect(wrapper.get("section input").attributes("aria-label")).toBe(
"深色模式预览",
);
await wrapper.get("select").setValue("light");
expect(wrapper.get("section").classes()).toContain("light");
expect(document.documentElement.className).toBe(original);
});
it("mounts the theme Popover inside the selected local theme container", async () => {
const wrapper = demo(Theme);
await wrapper.get("select").setValue("dark");
await wrapper.get("button").trigger("click");
await flushPromises();
expect(
wrapper.find("section.dark [data-slot=popover-content]").exists(),
).toBe(true);
});
it("demonstrates actual slot state and pending semantics rather than static labels", async () => {
const wrapper = demo(States);
const button = wrapper.get(".handbook-state-button");
await button.trigger("pointerdown");
expect(button.text()).toBe("按下中");
await button.trigger("pointerup");
expect(button.text()).not.toBe("按下中");
await wrapper.get('input[type="checkbox"]').setValue(true);
expect(wrapper.findAll("button").at(2)?.attributes("aria-busy")).toBe("true");
});
it("preserves one click while extending the composed native root", async () => {
const wrapper = demo(Composition);
await wrapper.get("button").trigger("click");
expect(wrapper.get("button").text()).toContain("1");
expect(wrapper.get("button").classes()).toContain("hv-button");
expect(wrapper.get("a").element.tagName).toBe("A");
});
it("puts local motion controls on the actual Portal ancestor", async () => {
const wrapper = demo(Motion);
await wrapper.get("input").setValue(true);
expect(wrapper.get("section").attributes("data-reduce-motion")).toBe("true");
await wrapper.findAll("button").at(-1)!.trigger("click");
await flushPromises();
expect(wrapper.find('section [data-slot="popover-content"]').exists()).toBe(
true,
);
});