Files
heroui-vue/tests/color-picker-protocol.test.ts
T
2026-10-25 19:52:12 +08:00

154 lines
6.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, shallowRef } from "vue";
import Picker from "../packages/vue/src/components/color-picker/ColorPicker.vue";
import Trigger from "../packages/vue/src/components/color-picker/ColorPickerTrigger.vue";
import Panel from "../packages/vue/src/components/color-picker/ColorPickerPopover.vue";
import Field from "../packages/vue/src/components/color-field/ColorField.vue";
import Group from "../packages/vue/src/components/color-field/ColorInputGroup.vue";
import Input from "../packages/vue/src/components/color-field/ColorInputGroupInput.vue";
import { parseColor } from "../packages/vue/src/components/color/value";
const components = { Picker, Trigger, Panel, Field, Group, Input };
const wrappers: ReturnType<typeof mount>[] = [];
beforeEach(() =>
vi.stubGlobal(
"ResizeObserver",
class {
observe() {}
unobserve() {}
disconnect() {}
},
),
);
afterEach(async () => {
wrappers.splice(0).forEach((w) => w.unmount());
await flushPromises();
document.body.innerHTML = "";
vi.unstubAllGlobals();
});
function view(template: string, setup = () => ({})) {
const w = mount(defineComponent({ components, setup, template }), {
attachTo: document.body,
});
wrappers.push(w);
return w;
}
const content =
'<Trigger>Choose</Trigger><Panel aria-label="Color"><Field aria-label="Hex"><Group><Input/></Group></Field></Panel>';
async function open(w: ReturnType<typeof mount>) {
await w.get("button").trigger("click");
await flushPromises();
}
async function edit(value: string) {
const input = document.querySelector<HTMLInputElement>('[aria-label="Hex"]')!;
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Enter",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
return input;
}
describe("formal ColorPicker owner protocol", () => {
it("uses baseline black, exposes accepted slot color and no successful control without a name", () => {
const w = view(
`<form><Picker v-slot="{ color, isOpen }"><Trigger>{{color.toString('hex')}}/{{isOpen}}</Trigger></Picker></form>`,
);
expect(w.get("button").text()).toBe("#000000/false");
expect([...new FormData(w.get("form").element as HTMLFormElement)]).toEqual(
[],
);
expect(w.get("button").attributes("data-slot")).toBe(
"color-picker-trigger",
);
expect(w.get("button").attributes("type")).toBe("button");
});
it("ignores a child clear, including the completion event, while standalone fields remain clearable", async () => {
const w = view(`<Picker default-value="#123456">${content}</Picker>`);
await open(w);
const input = await edit("");
expect(input.value).toBe("#123456");
expect(w.getComponent(Picker).emitted("change")).toBeUndefined();
expect(w.getComponent(Picker).emitted("changeEnd")).toBeUndefined();
});
it("retains parent refusal and retries the same child edit with non-null Color events", async () => {
const w = view(`<Picker model-value="#123456">${content}</Picker>`);
await open(w);
for (let n = 0; n < 2; n++)
expect((await edit("#abc")).value).toBe("#123456");
expect(w.getComponent(Picker).emitted("change")).toHaveLength(2);
expect(w.getComponent(Picker).emitted("change")![0]![0]).toEqual(
parseColor("#abc"),
);
});
it("supports explicit external form association and reset with an open Teleport", async () => {
const w = view(
`<main><form id="external"/><Picker name="color" form="external" default-value="hsba(270,0%,50%,.123456789)">${content}</Picker></main>`,
);
await open(w);
await edit("#abc");
const form = w.get("form").element as HTMLFormElement;
expect(new FormData(form).getAll("color")).toEqual([
"rgba(170, 187, 204, 1)",
]);
form.reset();
await flushPromises();
expect(new FormData(form).getAll("color")).toEqual([
"hsba(270, 0%, 50%, 0.123456789)",
]);
expect(
document.querySelector<HTMLInputElement>('[aria-label="Hex"]')!.value,
).toBe("#808080");
expect(document.querySelectorAll("input[name=color]")).toHaveLength(1);
});
it("cancels native trigger click before activation and preserves attrs on their actual nodes", async () => {
const clicked = vi.fn();
const w = view(
'<Picker id="owner" class="custom-root"><Trigger id="trigger" class="custom-trigger" @click.prevent="clicked">Choose</Trigger><Panel id="panel" aria-label="Color"/></Picker>',
() => ({ clicked }),
);
await open(w);
expect(clicked).toHaveBeenCalledTimes(1);
expect(document.querySelector("[role=dialog]")).toBeNull();
expect(w.get("#owner").classes()).toContain("hv-color-picker");
expect(w.get("#trigger").classes()).toContain("custom-trigger");
});
it("leaves nested Picker owners independent and scopes disabled to its own children", async () => {
const w = view(
`<form><Picker is-disabled name="outer" default-value="#123456"><Trigger>Outer</Trigger><Picker name="inner" default-value="#abcdef"><Trigger>Inner</Trigger></Picker></Picker></form>`,
);
const buttons = w.findAll<HTMLButtonElement>("button");
expect(buttons[0]!.element.disabled).toBe(true);
expect(buttons[1]!.element.disabled).toBe(false);
expect(
new FormData(w.get("form").element as HTMLFormElement).getAll("inner"),
).toEqual(["rgba(171, 205, 239, 1)"]);
});
it("respects read-only but accepts later programmatic values through the same owner", async () => {
const value = shallowRef(parseColor("#123456"));
const w = view(
`<Picker v-model="value" is-read-only>${content}</Picker>`,
() => ({ value }),
);
await open(w);
await edit("#abc");
expect(value.value.equals(parseColor("#123456"))).toBe(true);
value.value = parseColor("#fedcba");
await flushPromises();
expect(
document.querySelector<HTMLInputElement>('[aria-label="Hex"]')!.value,
).toBe("#FEDCBA");
});
it("keeps controlled open refusal independent from the accepted color", async () => {
const w = view(`<Picker :open="false">${content}</Picker>`);
await open(w);
expect(document.querySelector("[role=dialog]")).toBeNull();
expect(w.getComponent(Picker).emitted("update:open")).toEqual([[true]]);
expect(w.getComponent(Picker).emitted("change")).toBeUndefined();
});
});