feat: migrate public ColorSlider with docs and tests
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { ColorSlider, parseColor } from "../packages/vue/src";
|
||||
import Basic from "../apps/docs/demos/color-slider-basic.vue";
|
||||
import Disabled from "../apps/docs/demos/color-slider-disabled.vue";
|
||||
import Vertical from "../apps/docs/demos/color-slider-vertical.vue";
|
||||
import Controlled from "../apps/docs/demos/color-slider-controlled.vue";
|
||||
import Channels from "../apps/docs/demos/color-slider-channels.vue";
|
||||
import AlphaChannel from "../apps/docs/demos/color-slider-alpha-channel.vue";
|
||||
import RgbChannels from "../apps/docs/demos/color-slider-rgb-channels.vue";
|
||||
import RenderFunction from "../apps/docs/demos/color-slider-render-function.vue";
|
||||
import CustomStyles from "../apps/docs/demos/color-slider-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",
|
||||
"disabled",
|
||||
"vertical",
|
||||
"controlled",
|
||||
"channels",
|
||||
"alpha-channel",
|
||||
"rgb-channels",
|
||||
"render-function",
|
||||
"custom-styles",
|
||||
];
|
||||
const roots = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll<HTMLElement>("[data-slot=color-slider]");
|
||||
const root = (w: ReturnType<typeof mount>) => roots(w)[0]!;
|
||||
const outputs = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll("[data-slot=color-slider-output]").map((node) => node.text());
|
||||
const inputs = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll<HTMLInputElement>(
|
||||
"[data-slot=color-slider-thumb] input[type=range]",
|
||||
);
|
||||
const thumbs = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll<HTMLElement>("[data-slot=color-slider-thumb]");
|
||||
const tracks = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll<HTMLElement>("[data-slot=color-slider-track]");
|
||||
|
||||
describe("ColorSlider documentation demos", () => {
|
||||
it("keeps the nine pinned previews in the upstream order", () => {
|
||||
const document = readFileSync(
|
||||
"apps/docs/components/color-slider.md",
|
||||
"utf8",
|
||||
);
|
||||
const names = [
|
||||
...document.matchAll(/<ComponentPreview name="color-slider-([^"]+)"/g),
|
||||
].map((match) => match[1]);
|
||||
expect(names).toEqual(previews);
|
||||
for (const name of previews) {
|
||||
const source = readFileSync(
|
||||
`apps/docs/demos/color-slider-${name}.vue`,
|
||||
"utf8",
|
||||
);
|
||||
expect(source).toContain("<template>");
|
||||
expect(source).toContain('from "@heroui-vue/components"');
|
||||
expect(source).not.toContain("className");
|
||||
}
|
||||
});
|
||||
|
||||
it("renders the basic example with Label, Output, Track and Thumb", () => {
|
||||
const w = render(Basic);
|
||||
expect(roots(w)).toHaveLength(1);
|
||||
expect(root(w).classes()).toContain("hv-color-slider");
|
||||
expect(root(w).attributes("data-orientation")).toBe("horizontal");
|
||||
expect(outputs(w)).toEqual(["0°"]);
|
||||
expect(inputs(w)).toHaveLength(1);
|
||||
expect(inputs(w)[0]!.element.value).toBe("0");
|
||||
expect(inputs(w)[0]!.attributes("aria-valuetext")).toBe("0°");
|
||||
expect(w.get("label[data-slot=label]").text()).toBe("色相");
|
||||
expect(inputs(w)[0]!.attributes("id")).toBe(
|
||||
w.get("label[data-slot=label]").attributes("for"),
|
||||
);
|
||||
expect(thumbs(w)[0]!.element.style.left).toBe("0%");
|
||||
expect(
|
||||
tracks(w)[0]!.element.style.getPropertyValue("--track-background"),
|
||||
).toContain("linear-gradient");
|
||||
expect(
|
||||
tracks(w)[0]!.element.style.getPropertyValue("--track-start-color"),
|
||||
).toBe("rgb(255, 0, 0)");
|
||||
});
|
||||
|
||||
it("renders the disabled example without editable input", async () => {
|
||||
const w = render(Disabled);
|
||||
expect(root(w).attributes("data-disabled")).toBe("true");
|
||||
expect(thumbs(w)[0]!.attributes("data-disabled")).toBe("true");
|
||||
expect(inputs(w)[0]!.element.disabled).toBe(true);
|
||||
expect(thumbs(w)[0]!.element.style.backgroundColor).toBe("");
|
||||
await inputs(w)[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
await inputs(w)[0]!.trigger("keyup", { key: "ArrowRight" });
|
||||
expect(inputs(w)[0]!.element.value).toBe("200");
|
||||
expect(w.findComponent(ColorSlider).emitted("change")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("renders three vertical sliders with localized accessible names", () => {
|
||||
const w = render(Vertical);
|
||||
expect(roots(w)).toHaveLength(3);
|
||||
for (const node of roots(w)) {
|
||||
expect(node.attributes("data-orientation")).toBe("vertical");
|
||||
}
|
||||
expect(inputs(w).map((input) => input.attributes("aria-label"))).toEqual([
|
||||
"色相",
|
||||
"饱和度",
|
||||
"明度",
|
||||
]);
|
||||
expect(
|
||||
inputs(w).map((input) => input.attributes("aria-orientation")),
|
||||
).toEqual(["vertical", "vertical", "vertical"]);
|
||||
expect(thumbs(w).map((thumb) => thumb.element.style.top)).toEqual([
|
||||
"100%",
|
||||
"0%",
|
||||
"50%",
|
||||
]);
|
||||
expect(
|
||||
tracks(w).map((track) =>
|
||||
track.element.style.getPropertyValue("--track-background"),
|
||||
),
|
||||
).toEqual([
|
||||
expect.stringContaining("to top"),
|
||||
expect.stringContaining("to top"),
|
||||
expect.stringContaining("to top"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the controlled slider and swatch in sync", async () => {
|
||||
const w = render(Controlled);
|
||||
const swatch = w.get<HTMLElement>("[data-slot=color-swatch]");
|
||||
expect(swatch.element.style.backgroundColor).toBe(
|
||||
parseColor("hsl(200, 100%, 50%)").toString("css"),
|
||||
);
|
||||
expect(w.text()).toContain("hsl(200, 100%, 50%)");
|
||||
expect(outputs(w)).toEqual(["200°"]);
|
||||
await inputs(w)[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
await inputs(w)[0]!.trigger("keyup", { key: "ArrowRight" });
|
||||
await flushPromises();
|
||||
expect(inputs(w)[0]!.element.value).toBe("201");
|
||||
expect(outputs(w)).toEqual(["201°"]);
|
||||
expect(w.text()).toContain("hsl(201, 100%, 50%)");
|
||||
expect(swatch.element.style.backgroundColor).toBe(
|
||||
parseColor("hsl(201, 100%, 50%)").toString("css"),
|
||||
);
|
||||
});
|
||||
|
||||
it("shares one color across the HSL channel sliders", async () => {
|
||||
const w = render(Channels);
|
||||
expect(roots(w)).toHaveLength(3);
|
||||
expect(outputs(w)).toEqual(["0°", "100%", "50%"]);
|
||||
expect(inputs(w).map((input) => input.element.value)).toEqual([
|
||||
"0",
|
||||
"100",
|
||||
"50",
|
||||
]);
|
||||
await inputs(w)[1]!.trigger("keydown", { key: "ArrowDown" });
|
||||
await inputs(w)[1]!.trigger("keyup", { key: "ArrowDown" });
|
||||
await flushPromises();
|
||||
expect(outputs(w)).toEqual(["0°", "99%", "50%"]);
|
||||
expect(w.text()).toContain("hsl(0, 99%, 50%)");
|
||||
expect(inputs(w)[1]!.element.value).toBe("99");
|
||||
const swatch = w.get<HTMLElement>("[data-slot=color-swatch]");
|
||||
expect(swatch.element.style.backgroundColor).toBe(
|
||||
parseColor("hsl(0, 99%, 50%)").toString("css"),
|
||||
);
|
||||
});
|
||||
|
||||
it("renders the alpha channel with a checkerboard and percent output", () => {
|
||||
const w = render(AlphaChannel);
|
||||
expect(inputs(w)[0]!.attributes("min")).toBe("0");
|
||||
expect(inputs(w)[0]!.attributes("max")).toBe("1");
|
||||
expect(inputs(w)[0]!.element.value).toBe("0.5");
|
||||
expect(outputs(w)).toEqual(["50%"]);
|
||||
expect(
|
||||
tracks(w)[0]!.element.style.getPropertyValue("--track-background"),
|
||||
).toContain("repeating-conic-gradient");
|
||||
expect(
|
||||
tracks(w)[0]!.element.style.getPropertyValue("--track-start-color"),
|
||||
).toBe("rgba(255, 0, 0, 0)");
|
||||
expect(
|
||||
tracks(w)[0]!.element.style.getPropertyValue("--track-end-color"),
|
||||
).toBe("rgb(255, 0, 0)");
|
||||
});
|
||||
|
||||
it("shares one RGB color across red, green and blue sliders", async () => {
|
||||
const w = render(RgbChannels);
|
||||
expect(outputs(w)).toEqual(["255", "100", "50"]);
|
||||
expect(inputs(w).map((input) => input.attributes("max"))).toEqual([
|
||||
"255",
|
||||
"255",
|
||||
"255",
|
||||
]);
|
||||
await inputs(w)[0]!.trigger("keydown", { key: "ArrowLeft" });
|
||||
await inputs(w)[0]!.trigger("keyup", { key: "ArrowLeft" });
|
||||
await flushPromises();
|
||||
expect(outputs(w)).toEqual(["254", "100", "50"]);
|
||||
expect(w.text()).toContain("rgb(254, 100, 50)");
|
||||
const swatch = w.get<HTMLElement>("[data-slot=color-swatch]");
|
||||
expect(swatch.element.style.backgroundColor).toBe(
|
||||
parseColor("rgb(254, 100, 50)").toString("css"),
|
||||
);
|
||||
});
|
||||
|
||||
it("maps the render-function example to asChild and keeps interaction", async () => {
|
||||
const w = render(RenderFunction);
|
||||
const node = root(w);
|
||||
expect(node.attributes("data-custom")).toBe("foo");
|
||||
expect(node.element.tagName).toBe("DIV");
|
||||
expect(node.classes()).toContain("hv-color-slider");
|
||||
expect(outputs(w)).toEqual(["0°"]);
|
||||
await inputs(w)[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
await inputs(w)[0]!.trigger("keyup", { key: "ArrowRight" });
|
||||
await flushPromises();
|
||||
expect(outputs(w)).toEqual(["1°"]);
|
||||
});
|
||||
|
||||
it("merges the custom style example classes onto real parts", () => {
|
||||
const w = render(CustomStyles);
|
||||
expect(root(w).classes()).toContain("hv-color-slider");
|
||||
const output = w.get("[data-slot=color-slider-output]");
|
||||
for (const name of [
|
||||
"text-muted",
|
||||
"tabular-nums",
|
||||
"hv-color-slider__output",
|
||||
]) {
|
||||
expect(output.classes()).toContain(name);
|
||||
}
|
||||
const track = tracks(w)[0]!;
|
||||
for (const name of [
|
||||
"h-4!",
|
||||
"rounded-none!",
|
||||
"ring-1",
|
||||
"ring-border",
|
||||
"hv-color-slider__track",
|
||||
]) {
|
||||
expect(track.classes()).toContain(name);
|
||||
}
|
||||
const thumb = thumbs(w)[0]!;
|
||||
for (const name of [
|
||||
"size-4",
|
||||
"rounded-sm!",
|
||||
"border-2",
|
||||
"border-background",
|
||||
"shadow-md",
|
||||
"hv-color-slider__thumb",
|
||||
]) {
|
||||
expect(thumb.classes()).toContain(name);
|
||||
}
|
||||
const label = w.get("label[data-slot=label]");
|
||||
expect(label.text()).toBe("色相");
|
||||
expect(label.classes()).toContain("font-medium");
|
||||
});
|
||||
|
||||
it("documents the Vue API, channels and related state", () => {
|
||||
const document = readFileSync(
|
||||
"apps/docs/components/color-slider.md",
|
||||
"utf8",
|
||||
);
|
||||
expect(document).toContain("title: ColorSlider 颜色滑块");
|
||||
expect(document).toContain("ColorSliderOutput");
|
||||
expect(document).toContain("ColorSliderTrack");
|
||||
expect(document).toContain("ColorSliderThumb");
|
||||
expect(document).toContain("`channel`");
|
||||
expect(document).toContain("`colorSpace`");
|
||||
expect(document).toContain("`orientation`");
|
||||
expect(document).toContain("`name`");
|
||||
expect(document).toContain("`form`");
|
||||
expect(document).toContain("`asChild`");
|
||||
expect(document).toContain("`styleFn`");
|
||||
expect(document).toContain("`isReadOnly`");
|
||||
expect(document).toContain("ColorSliderThumbRenderProps");
|
||||
expect(document).toContain("changeEnd");
|
||||
expect(document).toContain("`modelValue`/`value` 或 `defaultValue`");
|
||||
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
||||
expect(document).not.toContain("### ColorSlider.Output");
|
||||
expect(document).toContain("ColorField");
|
||||
expect(document).toContain("仍为私有颜色协议");
|
||||
});
|
||||
|
||||
it("adds ColorSlider to the shared navigation between Area and Swatch", () => {
|
||||
const navigation = readFileSync(
|
||||
"apps/docs/.vitepress/theme/navigation.ts",
|
||||
"utf8",
|
||||
);
|
||||
const area = navigation.indexOf("ColorArea 颜色区域");
|
||||
const slider = navigation.indexOf("ColorSlider 颜色滑块");
|
||||
const swatch = navigation.indexOf("ColorSwatch 颜色色块");
|
||||
expect(area).toBeGreaterThan(-1);
|
||||
expect(slider).toBeGreaterThan(area);
|
||||
expect(swatch).toBeGreaterThan(slider);
|
||||
expect(navigation).toContain('url: "/components/color-slider"');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user