179 lines
7.8 KiB
TypeScript
179 lines
7.8 KiB
TypeScript
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<typeof mount>[] = [];
|
|
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<typeof mount>) =>
|
|
w.get<HTMLInputElement>("[data-slot=color-input-group-input]");
|
|
const body =
|
|
"<Label>颜色</Label><ColorFieldGroup><ColorFieldPrefix>#</ColorFieldPrefix><ColorFieldInput/><ColorFieldSuffix>色</ColorFieldSuffix></ColorFieldGroup><Description>帮助</Description><FieldError/>";
|
|
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(
|
|
`<ColorField aria-label="颜色" aria-labelledby="external-label" aria-describedby="external-description" :aria-details="details">${body}</ColorField>`,
|
|
() => ({ 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(
|
|
`<ColorField as="section" default-value="#123456" :style-fn="s => ({ opacity: s.isFocused ? '.5' : '1' })" v-slot="s"><ColorFieldGroup :style-fn="g => ({ marginTop: g.isFocusWithin ? '2px' : '0px' })"><ColorFieldInput /></ColorFieldGroup><output>{{s.isFocused}}/{{s.isFocusWithin}}</output></ColorField>`,
|
|
);
|
|
control(w).element.focus();
|
|
await flushPromises();
|
|
const root = w.get<HTMLElement>("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<HTMLElement>("[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(`<ColorField default-value="#123456">${body}</ColorField>`);
|
|
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(
|
|
`<form><ColorField name="color" model-value="#123456">${body}</ColorField></form>`,
|
|
);
|
|
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(
|
|
`<ColorField :model-value="null" value="#abcdef" default-value="#123456">${body}</ColorField>`,
|
|
);
|
|
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(
|
|
`<ColorField default-value="hsl(200,50%,50%)"><ColorFieldGroup><ColorFieldInput /></ColorFieldGroup><ColorField v-bind="owns ? { modelValue: '#abcdef' } : {}"><ColorFieldGroup><ColorFieldInput /></ColorFieldGroup></ColorField></ColorField>`,
|
|
() => ({ owns }),
|
|
);
|
|
const inputs = w.findAll<HTMLInputElement>("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(
|
|
`<ColorField default-value="hsba(270,0%,50%,.123456789)">${body}</ColorField>`,
|
|
);
|
|
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(
|
|
'<ColorInputGroup variant="secondary"><ColorInputGroupInput v-model="value" name="plain" /></ColorInputGroup>',
|
|
() => ({ value }),
|
|
);
|
|
await control(w).setValue("anything");
|
|
expect(value.value).toBe("anything");
|
|
expect(w.get("[data-slot=color-input-group]").attributes("role")).toBe(
|
|
"group",
|
|
);
|
|
});
|
|
});
|