167 lines
6.2 KiB
TypeScript
167 lines
6.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { readFileSync } from "node:fs";
|
|
import Basic from "../apps/docs/demos/color-picker-basic.vue";
|
|
import Controlled from "../apps/docs/demos/color-picker-controlled.vue";
|
|
import WithSwatches from "../apps/docs/demos/color-picker-with-swatches.vue";
|
|
import WithFields from "../apps/docs/demos/color-picker-with-fields.vue";
|
|
import WithSliders from "../apps/docs/demos/color-picker-with-sliders.vue";
|
|
import CustomStyles from "../apps/docs/demos/color-picker-custom-styles.vue";
|
|
|
|
const mounted: ReturnType<typeof mount>[] = [];
|
|
beforeEach(() =>
|
|
vi.stubGlobal(
|
|
"ResizeObserver",
|
|
class {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
},
|
|
),
|
|
);
|
|
afterEach(async () => {
|
|
mounted.splice(0).forEach((w) => w.unmount());
|
|
await flushPromises();
|
|
document.body.innerHTML = "";
|
|
vi.unstubAllGlobals();
|
|
vi.restoreAllMocks();
|
|
});
|
|
function render(component: Parameters<typeof mount>[0]) {
|
|
const w = mount(component, { attachTo: document.body });
|
|
mounted.push(w);
|
|
return w;
|
|
}
|
|
async function open(w: ReturnType<typeof mount>) {
|
|
await w.get<HTMLButtonElement>("[data-slot=color-picker-trigger]").trigger("click");
|
|
await flushPromises();
|
|
expect(document.querySelector("[role=dialog]")).not.toBeNull();
|
|
}
|
|
const previews = [
|
|
"basic",
|
|
"controlled",
|
|
"with-swatches",
|
|
"with-fields",
|
|
"with-sliders",
|
|
"custom-styles",
|
|
];
|
|
const items = () =>
|
|
Array.from(
|
|
document.querySelectorAll<HTMLElement>("[data-slot=color-swatch-picker-item]"),
|
|
);
|
|
const labels = () =>
|
|
Array.from(document.querySelectorAll<HTMLElement>("[data-slot=label]")).map(
|
|
(node) => node.textContent?.trim(),
|
|
);
|
|
|
|
describe("ColorPicker documentation demos", () => {
|
|
it("keeps the six pinned previews in the fixed source order", () => {
|
|
const document = readFileSync("apps/docs/components/color-picker.md", "utf8");
|
|
const names = [
|
|
...document.matchAll(/<ComponentPreview name="color-picker-([^"]+)"/g),
|
|
].map((match) => match[1]);
|
|
expect(names).toEqual(previews);
|
|
for (const name of previews) {
|
|
const source = readFileSync(
|
|
`apps/docs/demos/color-picker-${name}.vue`,
|
|
"utf8",
|
|
);
|
|
expect(source).toContain("<template>");
|
|
expect(source).toContain('from "@heroui-vue/components"');
|
|
expect(source).not.toContain("className");
|
|
}
|
|
});
|
|
|
|
it("renders the basic preview with the shared HSB area and hue slider", async () => {
|
|
const w = render(Basic);
|
|
const trigger = w.get<HTMLElement>("[data-slot=color-picker-trigger]");
|
|
expect(trigger.text()).toContain("选择颜色");
|
|
expect(
|
|
w.get<HTMLElement>("[data-slot=color-swatch]").element.style.backgroundColor,
|
|
).toBe("rgb(4, 133, 247)");
|
|
await open(w);
|
|
expect(document.querySelector('[aria-label="取色区域"]')).not.toBeNull();
|
|
expect(labels()).toContain("色相");
|
|
const x = document.querySelector<HTMLInputElement>('[data-axis="x"]')!;
|
|
x.dispatchEvent(
|
|
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true }),
|
|
);
|
|
x.dispatchEvent(
|
|
new KeyboardEvent("keyup", { key: "ArrowRight", bubbles: true, cancelable: true }),
|
|
);
|
|
await flushPromises();
|
|
expect(
|
|
w.get<HTMLElement>("[data-slot=color-swatch]").element.style.backgroundColor,
|
|
).not.toBe("rgb(4, 133, 247)");
|
|
});
|
|
|
|
it("keeps the controlled preview output, presets and random action in sync", async () => {
|
|
const w = render(Controlled);
|
|
expect(w.text()).toContain("已选:");
|
|
expect(w.text()).toContain("#325578");
|
|
await open(w);
|
|
expect(items()).toHaveLength(9);
|
|
expect(document.querySelector('[aria-label="随机颜色"]')).not.toBeNull();
|
|
items()[1]!.dispatchEvent(
|
|
new MouseEvent("mousedown", { bubbles: true, cancelable: true }),
|
|
);
|
|
await flushPromises();
|
|
expect(w.text()).toContain("#F97316");
|
|
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
document
|
|
.querySelector<HTMLButtonElement>('[aria-label="随机颜色"]')!
|
|
.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
|
|
await flushPromises();
|
|
expect(w.text()).toContain("#993333");
|
|
});
|
|
|
|
it("renders the brand swatches preview with the pinned #F43F5E start", async () => {
|
|
const w = render(WithSwatches);
|
|
expect(w.get("[data-slot=color-picker-trigger]").text()).toContain("品牌色");
|
|
expect(
|
|
w.get<HTMLElement>("[data-slot=color-swatch]").element.style.backgroundColor,
|
|
).toBe("rgb(244, 63, 94)");
|
|
await open(w);
|
|
expect(items()).toHaveLength(9);
|
|
expect(labels()).toContain("色相");
|
|
});
|
|
|
|
it("renders the fields preview with one channel field per HSL channel", async () => {
|
|
const w = render(WithFields);
|
|
await open(w);
|
|
const fields = Array.from(
|
|
document.querySelectorAll<HTMLElement>("[data-slot=color-field]"),
|
|
);
|
|
expect(
|
|
fields.map((field) => field.querySelector("input")!.getAttribute("aria-label")),
|
|
).toEqual(["色相", "饱和度", "明度"]);
|
|
expect(document.querySelector('[aria-label="色彩空间"]')).not.toBeNull();
|
|
expect(document.querySelector("[data-slot=color-area]")).not.toBeNull();
|
|
});
|
|
|
|
it("renders the sliders preview with four HSL channels including alpha", async () => {
|
|
const w = render(WithSliders);
|
|
await open(w);
|
|
const sliders = Array.from(
|
|
document.querySelectorAll<HTMLElement>("[data-slot=color-slider]"),
|
|
);
|
|
expect(
|
|
sliders.map((slider) => slider.querySelector("input")!.getAttribute("aria-label")),
|
|
).toEqual(["色相", "饱和度", "明度", "透明度"]);
|
|
expect(document.querySelector('[aria-label="色彩空间"]')).not.toBeNull();
|
|
});
|
|
|
|
it("keeps the custom styles preview classes on their own nodes", async () => {
|
|
const w = render(CustomStyles);
|
|
const trigger = w.get<HTMLElement>("[data-slot=color-picker-trigger]");
|
|
expect(trigger.classes()).toContain("bg-default-soft");
|
|
expect(trigger.text()).toContain("主题色");
|
|
await open(w);
|
|
expect(
|
|
document
|
|
.querySelector("[data-slot=color-picker-popover]")!
|
|
.getAttribute("class"),
|
|
).toContain("bg-surface");
|
|
expect(document.querySelector('[aria-label="颜色区域"]')).not.toBeNull();
|
|
});
|
|
});
|