224 lines
7.4 KiB
TypeScript
224 lines
7.4 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { readFileSync } from "node:fs";
|
|
import {
|
|
ColorSwatchPicker,
|
|
ColorSwatchPickerIndicator,
|
|
ColorSwatchPickerItem,
|
|
ColorSwatchPickerSwatch,
|
|
} from "../packages/vue/src";
|
|
import Basic from "../apps/docs/demos/color-swatch-picker-basic.vue";
|
|
import Variants from "../apps/docs/demos/color-swatch-picker-variants.vue";
|
|
import Sizes from "../apps/docs/demos/color-swatch-picker-sizes.vue";
|
|
import Disabled from "../apps/docs/demos/color-swatch-picker-disabled.vue";
|
|
import StackLayout from "../apps/docs/demos/color-swatch-picker-stack-layout.vue";
|
|
import DefaultValue from "../apps/docs/demos/color-swatch-picker-default-value.vue";
|
|
import Controlled from "../apps/docs/demos/color-swatch-picker-controlled.vue";
|
|
import CustomIndicator from "../apps/docs/demos/color-swatch-picker-custom-indicator.vue";
|
|
import RenderFunction from "../apps/docs/demos/color-swatch-picker-render-function.vue";
|
|
import CustomStyles from "../apps/docs/demos/color-swatch-picker-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",
|
|
"variants",
|
|
"sizes",
|
|
"disabled",
|
|
"stack-layout",
|
|
"default-value",
|
|
"controlled",
|
|
"custom-indicator",
|
|
"render-function",
|
|
"custom-styles",
|
|
];
|
|
|
|
const BASELINE = [
|
|
"#F43F5E",
|
|
"#D946EF",
|
|
"#8B5CF6",
|
|
"#3B82F6",
|
|
"#06B6D4",
|
|
"#10B981",
|
|
"#84CC16",
|
|
];
|
|
|
|
const items = (w: ReturnType<typeof mount>) =>
|
|
w.findAll<HTMLElement>("[data-slot=color-swatch-picker-item]");
|
|
|
|
describe("ColorSwatchPicker documentation demos", () => {
|
|
it("keeps the ten pinned previews in the upstream order", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/color-swatch-picker.md",
|
|
"utf8",
|
|
);
|
|
const names = [
|
|
...document.matchAll(
|
|
/<ComponentPreview name="color-swatch-picker-([^"]+)"/g,
|
|
),
|
|
].map((match) => match[1]);
|
|
expect(names).toEqual(previews);
|
|
for (const name of previews) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/color-swatch-picker-${name}.vue`,
|
|
"utf8",
|
|
);
|
|
expect(source).toContain("<template>");
|
|
expect(source).toContain('from "@heroui-vue/components"');
|
|
expect(source).not.toContain("className");
|
|
}
|
|
});
|
|
|
|
it("renders the basic preset palette", () => {
|
|
const w = render(Basic);
|
|
expect(w.findAllComponents(ColorSwatchPicker)).toHaveLength(1);
|
|
expect(w.findAllComponents(ColorSwatchPickerItem)).toHaveLength(7);
|
|
expect(w.findAllComponents(ColorSwatchPickerSwatch)).toHaveLength(7);
|
|
expect(w.findAllComponents(ColorSwatchPickerIndicator)).toHaveLength(7);
|
|
expect(
|
|
items(w).map((item) =>
|
|
(item.find("[data-slot=color-swatch-picker-swatch]").element as HTMLElement)
|
|
.style.backgroundColor,
|
|
),
|
|
).toEqual([
|
|
"rgb(244, 63, 94)",
|
|
"rgb(217, 70, 239)",
|
|
"rgb(139, 92, 246)",
|
|
"rgb(59, 130, 246)",
|
|
"rgb(6, 182, 212)",
|
|
"rgb(16, 185, 129)",
|
|
"rgb(132, 204, 22)",
|
|
]);
|
|
for (const color of BASELINE) {
|
|
expect(items(w).some((item) => item.attributes("aria-label") === color.toUpperCase()))
|
|
.toBe(true);
|
|
}
|
|
});
|
|
|
|
it("maps variants, sizes, layout and disabled states to baseline classes", () => {
|
|
const variants = render(Variants);
|
|
const roots = variants
|
|
.findAll<HTMLElement>("[data-slot=color-swatch-picker]")
|
|
.map((node) => node.classes());
|
|
expect(roots[0]).toContain("hv-color-swatch-picker--circle");
|
|
expect(roots[1]).toContain("hv-color-swatch-picker--square");
|
|
|
|
const sizes = render(Sizes);
|
|
expect(
|
|
sizes
|
|
.findAll<HTMLElement>("[data-slot=color-swatch-picker]")
|
|
.map((node) =>
|
|
["xs", "sm", "md", "lg", "xl"].find((size) =>
|
|
node.classes().includes(`hv-color-swatch-picker--${size}`),
|
|
),
|
|
),
|
|
).toEqual(["xs", "sm", "md", "lg", "xl"]);
|
|
|
|
const stack = render(StackLayout);
|
|
expect(
|
|
stack
|
|
.get<HTMLElement>("[data-slot=color-swatch-picker]")
|
|
.classes(),
|
|
).toContain("hv-color-swatch-picker--stack");
|
|
|
|
const disabled = render(Disabled);
|
|
expect(items(disabled)).toHaveLength(7);
|
|
for (const item of items(disabled)) {
|
|
expect(item.attributes("aria-disabled")).toBe("true");
|
|
expect(item.attributes("data-disabled")).toBe("true");
|
|
}
|
|
});
|
|
|
|
it("keeps the default value, controlled value and real slot state", async () => {
|
|
const initial = render(DefaultValue);
|
|
expect(
|
|
items(initial).map((item) => item.attributes("aria-selected")),
|
|
).toEqual([
|
|
"false",
|
|
"false",
|
|
"true",
|
|
"false",
|
|
"false",
|
|
"false",
|
|
"false",
|
|
]);
|
|
|
|
const controlled = render(Controlled);
|
|
expect(
|
|
controlled.get("p span").text(),
|
|
).toBe("#F43F5E");
|
|
items(controlled)[2]!.trigger("mousedown");
|
|
await controlled.vm.$nextTick();
|
|
expect(controlled.get("p span").text()).toBe("#8B5CF6");
|
|
|
|
const slots = render(RenderFunction);
|
|
const root = slots.get<HTMLElement>("[data-slot=color-swatch-picker]");
|
|
expect(root.attributes("data-custom")).toBe("foo");
|
|
expect(items(slots)[2]!.attributes("aria-selected")).toBe("false");
|
|
expect(
|
|
items(slots)[2]!
|
|
.find("[data-slot=color-swatch-picker-swatch]")
|
|
.attributes("data-picker-layout"),
|
|
).toBe("grid");
|
|
});
|
|
|
|
it("replaces the default checkmark and styles the custom container", () => {
|
|
const custom = render(CustomIndicator);
|
|
expect(
|
|
custom.findAll("[data-slot=color-swatch-picker-checkmark]"),
|
|
).toHaveLength(0);
|
|
expect(custom.findAll("svg")).toHaveLength(7);
|
|
|
|
const styled = render(CustomStyles);
|
|
const root = styled.get<HTMLElement>("[data-slot=color-swatch-picker]");
|
|
expect(root.classes()).toEqual(
|
|
expect.arrayContaining([
|
|
"rounded-2xl",
|
|
"bg-surface",
|
|
"hv-color-swatch-picker--square",
|
|
]),
|
|
);
|
|
expect(items(styled)[2]!.attributes("aria-selected")).toBe("true");
|
|
});
|
|
|
|
it("documents the Vue API, slot states and recorded differences", () => {
|
|
const document = readFileSync(
|
|
"apps/docs/components/color-swatch-picker.md",
|
|
"utf8",
|
|
);
|
|
expect(document).toContain("title: ColorSwatchPicker 颜色色块选择器");
|
|
expect(document).toContain("`modelValue`");
|
|
expect(document).toContain("`defaultValue`");
|
|
expect(document).toContain("`layout`");
|
|
expect(document).toContain("`size`");
|
|
expect(document).toContain("`variant`");
|
|
expect(document).toContain("### Render Props");
|
|
expect(document).toContain("isSelected");
|
|
expect(document).toContain("listbox");
|
|
expect(document).toContain("hexa");
|
|
expect(document).toContain("可访问名称差异");
|
|
expect(document).toContain("感知色名");
|
|
expect(document).toContain("不新增这些字段 API");
|
|
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
|
});
|
|
|
|
it("adds ColorSwatchPicker to the shared navigation", () => {
|
|
const navigation = readFileSync(
|
|
"apps/docs/.vitepress/theme/navigation.ts",
|
|
"utf8",
|
|
);
|
|
expect(navigation).toContain("ColorSwatchPicker 颜色色块选择器");
|
|
expect(navigation).toContain('url: "/components/color-swatch-picker"');
|
|
expect(navigation).toContain('category: "colors"');
|
|
});
|
|
});
|