195 lines
6.8 KiB
TypeScript
195 lines
6.8 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { readFileSync } from "node:fs";
|
|
import { ColorSwatch } from "../packages/vue/src";
|
|
import Basic from "../apps/docs/demos/color-swatch-basic.vue";
|
|
import Sizes from "../apps/docs/demos/color-swatch-sizes.vue";
|
|
import Shapes from "../apps/docs/demos/color-swatch-shapes.vue";
|
|
import Transparency from "../apps/docs/demos/color-swatch-transparency.vue";
|
|
import RenderFunction from "../apps/docs/demos/color-swatch-render-function.vue";
|
|
import Accessibility from "../apps/docs/demos/color-swatch-accessibility.vue";
|
|
import CustomStyles from "../apps/docs/demos/color-swatch-custom-styles.vue";
|
|
|
|
const mounted: ReturnType<typeof mount>[] = [];
|
|
function render(component: Parameters<typeof mount>[0]) {
|
|
const w = mount(component, { attachTo: document.body });
|
|
mounted.push(w);
|
|
return w;
|
|
}
|
|
afterEach(() => {
|
|
mounted.splice(0).forEach((w) => w.unmount());
|
|
document.body.innerHTML = "";
|
|
});
|
|
|
|
const previews = [
|
|
"basic",
|
|
"sizes",
|
|
"shapes",
|
|
"transparency",
|
|
"render-function",
|
|
"accessibility",
|
|
"custom-styles",
|
|
];
|
|
|
|
describe("ColorSwatch documentation demos", () => {
|
|
it("keeps the seven pinned previews in the upstream order", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/color-swatch.md",
|
|
"utf8",
|
|
);
|
|
const names = [
|
|
...document.matchAll(/<ComponentPreview name="color-swatch-([^"]+)"/g),
|
|
].map((match) => match[1]);
|
|
expect(names).toEqual(previews);
|
|
for (const name of previews) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/color-swatch-${name}.vue`,
|
|
"utf8",
|
|
);
|
|
expect(source).toContain("<template>");
|
|
expect(source).toContain('from "@heroui-vue/components"');
|
|
expect(source).not.toContain("className");
|
|
}
|
|
});
|
|
|
|
it("renders the basic color set", () => {
|
|
const w = render(Basic);
|
|
const swatches = w.findAllComponents(ColorSwatch);
|
|
expect(swatches).toHaveLength(5);
|
|
expect(
|
|
swatches.map(
|
|
(swatch) => (swatch.element as HTMLElement).style.backgroundColor,
|
|
),
|
|
).toEqual([
|
|
"rgb(4, 133, 247)",
|
|
"rgb(239, 68, 68)",
|
|
"rgb(245, 158, 11)",
|
|
"rgb(16, 185, 129)",
|
|
"rgb(217, 70, 239)",
|
|
]);
|
|
expect(w.findAll<HTMLElement>("[data-slot=color-swatch]")).toHaveLength(5);
|
|
});
|
|
|
|
it("maps every size and both shapes to baseline classes", () => {
|
|
const sizes = render(Sizes);
|
|
const classes = sizes
|
|
.findAll<HTMLElement>("[data-slot=color-swatch]")
|
|
.map((node) =>
|
|
["xs", "sm", "md", "lg", "xl"].find((size) =>
|
|
node.classes().includes(`hv-color-swatch--${size}`),
|
|
),
|
|
);
|
|
expect(classes).toEqual(["xs", "sm", "md", "lg", "xl"]);
|
|
|
|
const shapes = render(Shapes);
|
|
const shapeNodes = shapes.findAll<HTMLElement>("[data-slot=color-swatch]");
|
|
expect(shapeNodes).toHaveLength(2);
|
|
expect(shapeNodes[0]!.classes()).toContain("hv-color-swatch--circle");
|
|
expect(shapeNodes[0]!.classes()).not.toContain("hv-color-swatch--square");
|
|
expect(shapeNodes[1]!.classes()).toContain("hv-color-swatch--square");
|
|
expect(shapeNodes[1]!.classes()).not.toContain("hv-color-swatch--circle");
|
|
});
|
|
|
|
it("renders transparency alphas on the actual swatch elements", () => {
|
|
const w = render(Transparency);
|
|
const styles = w
|
|
.findAll<HTMLElement>("[data-slot=color-swatch]")
|
|
.map((node) => node.element.style.backgroundColor);
|
|
expect(styles).toEqual([
|
|
"rgb(4, 133, 247)",
|
|
"rgba(4, 133, 247, 0.75)",
|
|
"rgba(4, 133, 247, 0.5)",
|
|
"rgba(4, 133, 247, 0.25)",
|
|
"rgba(4, 133, 247, 0)",
|
|
]);
|
|
expect(
|
|
w
|
|
.findAll<HTMLElement>("[data-slot=color-swatch]")
|
|
.map((node) => node.attributes("aria-label")),
|
|
).toEqual([
|
|
"#0485F7, 100% 不透明度",
|
|
"#0485F7BF, 75% 不透明度",
|
|
"#0485F780, 50% 不透明度",
|
|
"#0485F740, 25% 不透明度",
|
|
"transparent, 0% 不透明度",
|
|
]);
|
|
});
|
|
|
|
it("maps the render-function demo to asChild and native attributes", () => {
|
|
const w = render(RenderFunction);
|
|
const nodes = w.findAll<HTMLElement>("[data-slot=color-swatch]");
|
|
expect(nodes).toHaveLength(5);
|
|
expect(nodes.map((node) => node.attributes("data-custom"))).toEqual([
|
|
"blue",
|
|
"red",
|
|
"amber",
|
|
"green",
|
|
"fuchsia",
|
|
]);
|
|
for (const node of nodes) {
|
|
expect(node.element.tagName).toBe("DIV");
|
|
expect(node.attributes("role")).toBe("img");
|
|
expect(node.classes()).toContain("hv-color-swatch");
|
|
expect(node.element.style.backgroundColor).not.toBe("");
|
|
}
|
|
});
|
|
|
|
it("merges colorName with the contextual aria-label", () => {
|
|
const w = render(Accessibility);
|
|
expect(
|
|
w
|
|
.findAll<HTMLElement>("[data-slot=color-swatch]")
|
|
.map((node) => node.attributes("aria-label")),
|
|
).toEqual(["海洋蓝, 主品牌色", "珊瑚红, 错误状态色", "落日橙, 警告色"]);
|
|
});
|
|
|
|
it("runs both custom-style effects through the real style function", () => {
|
|
const w = render(CustomStyles);
|
|
const nodes = w.findAll<HTMLElement>("[data-slot=color-swatch]");
|
|
expect(nodes).toHaveLength(10);
|
|
expect(nodes[0]!.element.style.boxShadow).toBe("0 0 20px 2px #0485F7");
|
|
expect(nodes[4]!.element.style.boxShadow).toBe("0 0 20px 2px #D946EF");
|
|
expect(nodes[5]!.element.style.background).toBe(
|
|
"linear-gradient(135deg, rgb(4, 133, 247), white)",
|
|
);
|
|
expect(nodes[9]!.element.style.background).toBe(
|
|
"linear-gradient(135deg, rgb(217, 70, 239), white)",
|
|
);
|
|
for (const node of nodes) {
|
|
expect(
|
|
node.element.style.getPropertyValue("--color-swatch-current"),
|
|
).not.toBe("");
|
|
}
|
|
});
|
|
|
|
it("documents the Vue API and style render contract", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/color-swatch.md",
|
|
"utf8",
|
|
);
|
|
expect(document).toContain("title: ColorSwatch 颜色色块");
|
|
expect(document).toContain("`color`");
|
|
expect(document).toContain("`colorName`");
|
|
expect(document).toContain("`shape`");
|
|
expect(document).toContain("`size`");
|
|
expect(document).toContain("`asChild`");
|
|
expect(document).toContain("`class`");
|
|
expect(document).toContain("Vue class 绑定");
|
|
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
|
expect(document).toContain("### Style Render");
|
|
expect(document).toContain("getChannelValue");
|
|
expect(document).toContain("未实现 React Stately");
|
|
expect(document).toContain("ColorSwatchPicker");
|
|
});
|
|
|
|
it("adds ColorSwatch to the shared navigation", () => {
|
|
const navigation = readFileSync(
|
|
"apps/docs/.vitepress/theme/navigation.ts",
|
|
"utf8",
|
|
);
|
|
expect(navigation).toContain("ColorSwatch 颜色色块");
|
|
expect(navigation).toContain('url: "/components/color-swatch"');
|
|
expect(navigation).toContain('category: "colors"');
|
|
});
|
|
});
|