Files
heroui-vue/tests/color-area-demos.test.ts
2026-10-27 17:47:52 +08:00

199 lines
7.9 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { readFileSync } from "node:fs";
import { ColorArea, Select, parseColor } from "../packages/vue/src";
import Basic from "../apps/docs/demos/color-area-basic.vue";
import WithDots from "../apps/docs/demos/color-area-with-dots.vue";
import SpaceAndChannels from "../apps/docs/demos/color-area-space-and-channels.vue";
import Disabled from "../apps/docs/demos/color-area-disabled.vue";
import Controlled from "../apps/docs/demos/color-area-controlled.vue";
import RenderFunction from "../apps/docs/demos/color-area-render-function.vue";
import CustomStyles from "../apps/docs/demos/color-area-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 root = (w: ReturnType<typeof mount>) =>
w.get<HTMLElement>("[data-slot=color-area]");
const thumb = (w: ReturnType<typeof mount>) =>
w.get<HTMLElement>("[data-slot=color-area-thumb]");
const axis = (w: ReturnType<typeof mount>, name: "x" | "y") =>
w.get<HTMLInputElement>(`[data-axis="${name}"]`);
const previews = [
"basic",
"with-dots",
"space-and-channels",
"disabled",
"controlled",
"render-function",
"custom-styles",
];
describe("ColorArea documentation demos", () => {
it("keeps the seven pinned previews in the upstream order", () => {
const document = readFileSync("apps/docs/components/color-area.md", "utf8");
const names = [
...document.matchAll(/<ComponentPreview name="color-area-([^"]+)"/g),
].map((match) => match[1]);
expect(names).toEqual(previews);
for (const name of previews) {
const source = readFileSync(
`apps/docs/demos/color-area-${name}.vue`,
"utf8",
);
expect(source).toContain("<template>");
expect(source).toContain('from "@heroui-vue/components"');
expect(source).not.toContain("className");
}
});
it("renders the basic color area from the baseline default value", () => {
const w = render(Basic);
expect(root(w).classes()).toContain("hv-color-area");
expect(axis(w, "x").element.value).toBe("116");
expect(axis(w, "y").element.value).toBe("52");
expect(axis(w, "x").attributes("aria-label")).toBe("Red");
expect(axis(w, "y").attributes("aria-label")).toBe("Green");
expect(thumb(w).element.style.left).toBe(`${(116 / 255) * 100}%`);
expect(
root(w).element.style.getPropertyValue("--color-area-background"),
).toContain("linear-gradient");
});
it("applies the dots overlay variant", () => {
const w = render(WithDots);
expect(root(w).classes()).toContain("hv-color-area--show-dots");
expect(axis(w, "x").element.value).toBe("200");
expect(axis(w, "y").element.value).toBe("100");
});
it("switches color space and axes in the space-and-channels example", async () => {
const w = render(SpaceAndChannels);
const selects = w.findAllComponents(Select);
expect(selects).toHaveLength(3);
expect(w.findAll("[data-slot=color-area]")).toHaveLength(1);
expect(axis(w, "x").attributes("aria-label")).toBe("Saturation");
expect(axis(w, "y").attributes("aria-label")).toBe("Brightness");
expect(axis(w, "x").element.value).toBe("58");
expect(axis(w, "y").element.value).toBe("93");
expect(w.text()).toContain("hsb(219, 58%, 93%)");
await selects[0]!.vm.$emit("update:modelValue", "rgb");
await flushPromises();
expect(axis(w, "x").attributes("aria-label")).toBe("Blue");
expect(axis(w, "y").attributes("aria-label")).toBe("Green");
expect(w.text()).toMatch(/rgb\(/);
await w.findAllComponents(Select)[1]!.vm.$emit("update:modelValue", "red");
await flushPromises();
expect(axis(w, "x").attributes("aria-label")).toBe("Red");
expect(axis(w, "x").attributes("max")).toBe("255");
});
it("disables both axes and pointer interaction", async () => {
const w = render(Disabled);
expect(root(w).attributes("data-disabled")).toBe("true");
expect(axis(w, "x").element.disabled).toBe(true);
expect(axis(w, "y").element.disabled).toBe(true);
await axis(w, "x").trigger("keydown", { key: "ArrowRight" });
expect(w.findComponent(ColorArea).emitted("change")).toBeUndefined();
});
it("keeps the controlled swatch and hex output in sync", async () => {
const w = render(Controlled);
const swatch = w.get<HTMLElement>("[data-slot=color-swatch]");
expect(swatch.element.style.backgroundColor).toBe(
parseColor("#9B80FF").toString("css"),
);
expect(axis(w, "x").element.value).toBe("155");
expect(axis(w, "y").element.value).toBe("128");
await axis(w, "x").trigger("keydown", { key: "ArrowRight" });
await axis(w, "x").trigger("keyup", { key: "ArrowRight" });
await flushPromises();
expect(w.text()).toContain("#9C80FF");
expect(swatch.element.style.backgroundColor).toBe(
parseColor("#9C80FF").toString("css"),
);
});
it("maps the render-function demo to asChild, as and native attributes", async () => {
const w = render(RenderFunction);
const area = root(w);
expect(area.attributes("data-custom")).toBe("slider");
expect(area.element.tagName).toBe("DIV");
expect(area.classes()).toContain("hv-color-area");
expect(area.attributes("role")).toBe("group");
const handle = thumb(w);
expect(handle.attributes("data-custom")).toBe("thumb");
expect(handle.element.tagName).toBe("SPAN");
expect(handle.classes()).toContain("hv-color-area__thumb");
expect(axis(w, "x").element.value).toBe("116");
await axis(w, "x").trigger("keydown", { key: "ArrowRight" });
await axis(w, "x").trigger("keyup", { key: "ArrowRight" });
await flushPromises();
expect(axis(w, "x").element.value).toBe("117");
});
it("merges the custom style classes onto the real parts", () => {
const w = render(CustomStyles);
const area = root(w);
const handle = thumb(w);
for (const name of ["size-44", "rounded-3xl", "hv-color-area"]) {
expect(area.classes()).toContain(name);
}
for (const name of [
"size-5",
"rounded-full",
"border-4",
"border-white",
"hv-color-area__thumb",
]) {
expect(handle.classes()).toContain(name);
}
});
it("documents the Vue API, adaptation range and related state", () => {
const document = readFileSync("apps/docs/components/color-area.md", "utf8");
expect(document).toContain("title: ColorArea 颜色区域");
expect(document).toContain("ColorAreaThumb");
expect(document).toContain("`colorSpace`");
expect(document).toContain("`xChannel`");
expect(document).toContain("`yChannel`");
expect(document).toContain("`showDots`");
expect(document).toContain("`asChild`");
expect(document).toContain("`styleFn`");
expect(document).toContain("`isReadOnly`");
expect(document).toContain("`xName` / `yName`");
expect(document).toContain("ColorAreaRenderProps");
expect(document).toContain("changeEnd");
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
expect(document).not.toContain("### ColorArea.Thumb");
expect(document).toContain("ColorField");
expect(document).toContain("ColorPicker 颜色选择器");
expect(document).toContain("/components/color-picker");
});
it("adds ColorArea to the shared navigation before ColorSwatch", () => {
const navigation = readFileSync(
"apps/docs/.vitepress/theme/navigation.ts",
"utf8",
);
const area = navigation.indexOf("ColorArea 颜色区域");
const swatch = navigation.indexOf("ColorSwatch 颜色色块");
expect(area).toBeGreaterThan(-1);
expect(swatch).toBeGreaterThan(area);
expect(navigation).toContain('url: "/components/color-area"');
expect(navigation).toContain('category: "colors"');
});
});