import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, shallowRef } from "vue"; import { ColorField as Field, ColorInputGroup as Group, ColorInputGroupInput as Input, ColorPicker as Picker, ColorPickerPopover as Panel, ColorPickerTrigger as Trigger, parseColor, } from "../packages/vue/src"; const components = { Picker, Trigger, Panel, Field, Group, Input }; const wrappers: ReturnType[] = []; 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 = 'Choose'; async function open(w: ReturnType) { await w.get("button").trigger("click"); await flushPromises(); } async function edit(value: string) { const input = document.querySelector('[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( `
{{color.toString('hex')}}/{{isOpen}}
`, ); 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(`${content}`); 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(`${content}`); 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( `
${content}
`, ); 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('[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( 'Choose', () => ({ 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( `OuterInner`, ); const buttons = w.findAll("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( `${content}`, () => ({ value }), ); await open(w); await edit("#abc"); expect(value.value.equals(parseColor("#123456"))).toBe(true); value.value = parseColor("#fedcba"); await flushPromises(); expect( document.querySelector('[aria-label="Hex"]')!.value, ).toBe("#FEDCBA"); }); it("keeps controlled open refusal independent from the accepted color", async () => { const w = view(`${content}`); 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(); }); });