import { afterEach, beforeEach, describe, it, expect, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, shallowRef } from "vue"; import Picker from "../packages/vue/tests/fixtures/ColorPickerComposition.vue"; import { parseColor, Color } from "../packages/vue/src/components/color/value"; import { serializeColorValue } from "../packages/vue/src/components/color/submission"; 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(); vi.restoreAllMocks(); }); function view(attrs = "", setup = () => ({})) { const w = mount( defineComponent({ components: { Picker }, setup, template: `
`, }), { attachTo: document.body }, ); wrappers.push(w); return w; } const native = (w: ReturnType) => w.get("input[type=hidden]"); const values = (w: ReturnType) => new FormData(w.get("form").element as HTMLFormElement).getAll("color"); async function open(w: ReturnType) { await w.get("button").trigger("click"); await flushPromises(); expect(document.querySelector("[role=dialog]")).not.toBeNull(); } async function key(input: HTMLInputElement, key: string) { input.dispatchEvent( new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }), ); input.dispatchEvent( new KeyboardEvent("keyup", { key, bubbles: true, cancelable: true }), ); await flushPromises(); } describe("private color owner and Popover composition", () => { it("restores a dirty text draft on Escape, then lets Escape close the popup", async () => { const w = view(); await open(w); const text = document.querySelector( '[aria-label="Color hex text"]', )!; text.focus(); text.value = "#abc"; text.dispatchEvent(new Event("input", { bubbles: true })); await flushPromises(); await key(text, "Escape"); expect(document.querySelector("[role=dialog]")).not.toBeNull(); expect(text.value).toBe("#808080"); expect(w.getComponent(Picker).emitted("change")).toBeUndefined(); await key(text, "Escape"); expect(document.querySelector("[role=dialog]")).toBeNull(); expect(document.activeElement).toBe(w.get("button").element); }); it("serializes all spaces without numeric precision loss or an 8-bit alpha round trip", () => { for (const c of [ parseColor("hsba(270,0%,50%,.123456789012345)"), parseColor( "hsla(21.123456789,67.987654321%,45.333333333%,.23456789012345)", ), new Color({ space: "rgb", r: 0.00000001, g: 2.3456789012345, b: 255, alpha: 5e-324, }), ]) expect(parseColor(serializeColorValue(c)).equals(c)).toBe(true); expect(serializeColorValue(null)).toBe(""); }); it("has one named submission while closed and with every popup child mounted", async () => { const w = view(); expect(values(w)).toEqual(["hsba(270, 0%, 50%, 0.123456789)"]); await open(w); expect(document.querySelectorAll("input[name=color]")).toHaveLength(1); expect(document.querySelectorAll("[role=dialog]")).toHaveLength(1); expect(native(w).element.value).toBe("hsba(270, 0%, 50%, 0.123456789)"); }); it("updates hue and alpha through Teleport while preserving gray hue and one owner", async () => { const w = view(); await open(w); const sliders = document.querySelectorAll( "[data-color-slider-input]", ); await key(sliders[0]!, "ArrowUp"); const text = document.querySelector( '[aria-label="Color alpha text"]', )!; text.value = "25"; text.dispatchEvent(new Event("input", { bubbles: true })); await key(text, "Enter"); const c = parseColor(String(values(w)[0])); expect(c.getChannelValue("hue")).toBe(271); expect(c.getChannelValue("alpha")).toBe(0.25); expect(sliders[1]!.value).toBe("0.25"); expect(w.getComponent(Picker).emitted("change")).toHaveLength(2); expect(w.getComponent(Picker).emitted("changeEnd")).toHaveLength(2); }); it("restores accepted color after rejection and retries an identical edit after reopening", async () => { const w = view('model-value="hsba(270,0%,50%,.123456789)"'); for (let i = 0; i < 2; i++) { await open(w); const slider = document.querySelector( "[data-color-slider-input]", )!; await key(slider, "ArrowUp"); expect(slider.value).toBe("270"); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await flushPromises(); } expect(w.getComponent(Picker).emitted("change")).toHaveLength(2); expect(values(w)).toEqual(["hsba(270, 0%, 50%, 0.123456789)"]); }); it("cancels an unfinished accepted close immediately and restores trigger focus", async () => { const w = view(); await open(w); const slider = document.querySelector( "[data-color-slider-input]", )!; slider.focus(); slider.dispatchEvent( new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true, cancelable: true, }), ); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await flushPromises(); expect(document.querySelector("[role=dialog]")).toBeNull(); expect(w.getComponent(Picker).emitted("changeEnd")).toBeUndefined(); expect(document.activeElement).toBe(w.get("button").element); }); it("does not cancel the input transaction when controlled overlay close is rejected", async () => { const w = view(':open="true"'); await flushPromises(); const slider = document.querySelector( "[data-color-slider-input]", )!; slider.dispatchEvent( new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true, cancelable: true, }), ); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await flushPromises(); expect(document.querySelector("[role=dialog]")).not.toBeNull(); slider.dispatchEvent( new KeyboardEvent("keyup", { key: "ArrowUp", bubbles: true, cancelable: true, }), ); await flushPromises(); expect(w.getComponent(Picker).emitted("changeEnd")).toHaveLength(1); }); it("resets one owner while the popup is open and retains controlled accepted values", async () => { const w = view(); await open(w); const slider = document.querySelector( "[data-color-slider-input]", )!; await key(slider, "ArrowUp"); (w.get("form").element as HTMLFormElement).reset(); await flushPromises(); expect(slider.value).toBe("270"); expect(values(w)).toEqual(["hsba(270, 0%, 50%, 0.123456789)"]); expect(w.getComponent(Picker).emitted("change")).toHaveLength(1); }); it("inherits disabled/readOnly and permits later accepted programmatic values", async () => { const off = shallowRef(false), value = shallowRef(parseColor("hsba(270,0%,50%,.123456789)")); const w = view( ':model-value="value" :is-disabled="off" is-read-only', () => ({ off, value }), ); await open(w); const slider = document.querySelector( "[data-color-slider-input]", )!; await key(slider, "ArrowUp"); expect(w.getComponent(Picker).emitted("change")).toBeUndefined(); value.value = parseColor("hsba(90,50%,50%,.125)"); await flushPromises(); expect(slider.value).toBe("90"); off.value = true; await flushPromises(); expect(values(w)).toEqual([]); expect(w.get("button").element.disabled).toBe(true); }); });