import { afterEach, describe, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, shallowRef, h } from "vue"; import * as api from "../packages/vue/src"; const mounted: ReturnType[] = []; afterEach(() => { mounted.splice(0).forEach((w) => w.unmount()); document.body.innerHTML = ""; vi.restoreAllMocks(); }); function view(template: string, setup = () => ({})) { const w = mount( defineComponent({ components: { ColorField: api.ColorField, ColorFieldGroup: api.ColorFieldGroup, ColorFieldInput: api.ColorFieldInput, ColorFieldPrefix: api.ColorFieldPrefix, ColorFieldSuffix: api.ColorFieldSuffix, ColorInputGroup: api.ColorInputGroup, ColorInputGroupInput: api.ColorInputGroupInput, Label: api.Label, Description: api.Description, FieldError: api.FieldError, }, setup, template, }), { attachTo: document.body }, ); mounted.push(w); return w; } const control = (w: ReturnType) => w.get("[data-slot=color-input-group-input]"); const body = "#色帮助"; describe("public ColorField", () => { it("exports aliases without creating separate implementations", () => { expect(api.ColorFieldRoot).toBe(api.ColorField); expect(api.ColorInputGroupRoot).toBe(api.ColorFieldGroup); expect(api.ColorInputGroupInput).toBe(api.ColorFieldInput); expect(api.ColorInputGroupPrefix).toBe(api.ColorFieldPrefix); expect(api.ColorInputGroupSuffix).toBe(api.ColorFieldSuffix); expect(api.colorFieldVariants({ fullWidth: true })).toContain( "hv-color-field--full-width", ); expect( api.colorInputGroupVariants({ variant: "secondary" }).base(), ).toContain("hv-color-input-group--secondary"); }); it("routes every ARIA attribute to the input and updates root attrs reactively", async () => { const details = shallowRef("details-a"); const w = view( `${body}`, () => ({ details }), ); expect(control(w).attributes("aria-details")).toBe("details-a"); expect(control(w).attributes("aria-labelledby")).toContain( "external-label", ); expect(control(w).attributes("aria-describedby")).toContain( "external-description", ); expect( w.get("[data-slot=color-field]").attributes("aria-details"), ).toBeUndefined(); details.value = "details-b"; await flushPromises(); expect(control(w).attributes("aria-details")).toBe("details-b"); }); it("updates scoped slots, styles and focus states from the real input", async () => { const w = view( `{{s.isFocused}}/{{s.isFocusWithin}}`, ); control(w).element.focus(); await flushPromises(); const root = w.get("section"); expect(root.attributes("data-focused")).toBe("true"); expect(root.attributes("data-focus-within")).toBe("true"); expect(root.element.style.opacity).toBe("0.5"); expect( w.get("[data-slot=color-input-group]").element.style .marginTop, ).toBe("2px"); expect(w.get("output").text()).toBe("true/true"); control(w).element.blur(); await flushPromises(); expect(w.get("output").text()).toBe("false/false"); expect(root.attributes("data-focus-within")).toBeUndefined(); }); it("exposes the actual input and color, with usable focus()", async () => { const w = view(`${body}`); const field = w.getComponent(api.ColorField); field.vm.focus(); await flushPromises(); expect(document.activeElement).toBe(control(w).element); expect(field.vm.element).toBe(control(w).element); expect(field.vm.color?.toString("hex")).toBe("#123456"); }); it("retries a rejected hex, preserves the native draft until commit and resets accepted DOM", async () => { const w = view( `
${body}
`, ); for (let i = 0; i < 2; i++) { await control(w).setValue("abc"); expect(new FormData(w.get("form").element).get("color")).toBe("abc"); await control(w).trigger("keydown", { key: "Enter" }); expect(control(w).element.value).toBe("#123456"); } expect(w.getComponent(api.ColorField).emitted("change")).toHaveLength(2); await control(w).setValue("#12"); w.get("form").element.reset(); await flushPromises(); expect(control(w).element.value).toBe("#123456"); }); it("modelValue including null takes precedence over value/defaultValue", async () => { const w = view( `${body}`, ); expect(control(w).element.value).toBe(""); await control(w).setValue("abc"); await control(w).trigger("blur"); expect(control(w).element.value).toBe(""); }); it("switches between standalone and inherited color values without leaking drafts", async () => { const owns = shallowRef(false); const w = view( ``, () => ({ owns }), ); const inputs = w.findAll("input[type=text]"); expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value); await inputs[1]!.setValue("#12"); owns.value = true; await flushPromises(); expect(inputs[1]!.element.value).toBe("#ABCDEF"); owns.value = false; await flushPromises(); expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value); }); it("commits composition only after candidate completion and preserves alpha when text is untouched", async () => { const w = view( `${body}`, ); await control(w).trigger("compositionstart"); await control(w).setValue("abc"); await control(w).trigger("keydown", { key: "Enter" }); expect(w.getComponent(api.ColorField).emitted("change")).toBeUndefined(); await control(w).trigger("compositionend"); await flushPromises(); await control(w).trigger("keydown", { key: "Escape" }); await control(w).trigger("blur"); expect( w.getComponent(api.ColorField).vm.color?.getChannelValue("alpha"), ).toBe(0.123456789); }); it("does not run unknown wrappers to discover an input", () => { const setup = vi.fn(() => () => h(api.ColorFieldInput)); const Unknown = defineComponent({ setup }); const w = mount(api.ColorField, { slots: { default: () => h(Unknown) } }); mounted.push(w); expect(setup).toHaveBeenCalledOnce(); expect(control(w).element.tagName).toBe("INPUT"); }); it("supports an independent group as a plain v-model text field", async () => { const value = shallowRef("free text"); const w = view( '', () => ({ value }), ); await control(w).setValue("anything"); expect(value.value).toBe("anything"); expect(w.get("[data-slot=color-input-group]").attributes("role")).toBe( "group", ); }); });