160 lines
6.5 KiB
TypeScript
160 lines
6.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { readFileSync } from "node:fs";
|
|
import { ColorField } from "../packages/vue/src";
|
|
import Basic from "../apps/docs/demos/color-field-basic.vue";
|
|
import Variants from "../apps/docs/demos/color-field-variants.vue";
|
|
import OnSurface from "../apps/docs/demos/color-field-on-surface.vue";
|
|
import WithDescription from "../apps/docs/demos/color-field-with-description.vue";
|
|
import Required from "../apps/docs/demos/color-field-required.vue";
|
|
import Disabled from "../apps/docs/demos/color-field-disabled.vue";
|
|
import FullWidth from "../apps/docs/demos/color-field-full-width.vue";
|
|
import Invalid from "../apps/docs/demos/color-field-invalid.vue";
|
|
import ChannelEditing from "../apps/docs/demos/color-field-channel-editing.vue";
|
|
import Controlled from "../apps/docs/demos/color-field-controlled.vue";
|
|
import FormExample from "../apps/docs/demos/color-field-form-example.vue";
|
|
import RenderFunction from "../apps/docs/demos/color-field-render-function.vue";
|
|
import CustomStyles from "../apps/docs/demos/color-field-custom-styles.vue";
|
|
const demos = {
|
|
basic: Basic,
|
|
variants: Variants,
|
|
"on-surface": OnSurface,
|
|
"with-description": WithDescription,
|
|
required: Required,
|
|
disabled: Disabled,
|
|
"full-width": FullWidth,
|
|
invalid: Invalid,
|
|
"channel-editing": ChannelEditing,
|
|
controlled: Controlled,
|
|
"form-example": FormExample,
|
|
"render-function": RenderFunction,
|
|
"custom-styles": CustomStyles,
|
|
};
|
|
const mounted: ReturnType<typeof mount>[] = [];
|
|
afterEach(() => {
|
|
mounted.splice(0).forEach((w) => w.unmount());
|
|
document.body.innerHTML = "";
|
|
vi.useRealTimers();
|
|
vi.restoreAllMocks();
|
|
});
|
|
function render(component: Parameters<typeof mount>[0]) {
|
|
const w = mount(component, { attachTo: document.body });
|
|
mounted.push(w);
|
|
return w;
|
|
}
|
|
const inputs = (w: ReturnType<typeof mount>) =>
|
|
w.findAll<HTMLInputElement>("input[type=text]");
|
|
describe("ColorField documentation", () => {
|
|
it("keeps all thirteen fixed-source previews in page order", () => {
|
|
const doc = readFileSync("apps/docs/components/color-field.md", "utf8");
|
|
expect(
|
|
[...doc.matchAll(/<ComponentPreview name="color-field-([^"]+)"/g)].map(
|
|
(m) => m[1],
|
|
),
|
|
).toEqual(Object.keys(demos));
|
|
});
|
|
for (const [name, demo] of Object.entries(demos))
|
|
it(`renders ${name} through public parts with a label for every input`, () => {
|
|
const w = render(demo);
|
|
expect(inputs(w).length).toBeGreaterThan(0);
|
|
for (const input of inputs(w))
|
|
expect(w.find(`label[for="${input.attributes("id")}"]`).exists()).toBe(
|
|
true,
|
|
);
|
|
expect(
|
|
readFileSync(`apps/docs/demos/color-field-${name}.vue`, "utf8"),
|
|
).not.toContain("packages/vue");
|
|
});
|
|
it("synchronizes basic hex editing and preview on blur", async () => {
|
|
const w = render(Basic);
|
|
expect(inputs(w)[0]!.element.value).toBe("#0485F7");
|
|
await inputs(w)[0]!.setValue("22c55e");
|
|
await inputs(w)[0]!.trigger("blur");
|
|
expect(inputs(w)[0]!.element.value).toBe("#22C55E");
|
|
expect(
|
|
w.get<HTMLElement>("[data-slot=color-swatch]").element.style
|
|
.backgroundColor,
|
|
).toBe("rgb(34, 197, 94)");
|
|
});
|
|
it("renders actual secondary, full width, disabled and invalid states", () => {
|
|
expect(
|
|
render(Variants)
|
|
.find("[data-slot=color-input-group].hv-color-input-group--secondary")
|
|
.exists(),
|
|
).toBe(true);
|
|
expect(
|
|
render(OnSurface)
|
|
.find(".hv-surface .hv-color-input-group--secondary")
|
|
.exists(),
|
|
).toBe(true);
|
|
expect(
|
|
render(FullWidth).findAll(".hv-color-field--full-width"),
|
|
).toHaveLength(2);
|
|
expect(inputs(render(Disabled)).every((i) => i.element.disabled)).toBe(
|
|
true,
|
|
);
|
|
expect(
|
|
inputs(render(Invalid)).every(
|
|
(i) => i.attributes("aria-invalid") === "true",
|
|
),
|
|
).toBe(true);
|
|
expect(inputs(render(Required)).every((i) => i.element.required)).toBe(
|
|
true,
|
|
);
|
|
});
|
|
it("updates all three HSL fields from one accepted color", async () => {
|
|
const w = render(ChannelEditing);
|
|
await inputs(w)[0]!.setValue("90");
|
|
await inputs(w)[0]!.trigger("blur");
|
|
expect(inputs(w)[0]!.element.value).toBe("90°");
|
|
expect(inputs(w)[1]!.element.value).toBe("100%");
|
|
expect(w.text()).toContain("当前:#407F00");
|
|
});
|
|
it("controlled buttons replace and clear both input and preview", async () => {
|
|
const w = render(Controlled);
|
|
await w.findAll("button")[0]!.trigger("click");
|
|
expect(inputs(w)[0]!.element.value).toBe("#EF4444");
|
|
await w.findAll("button")[1]!.trigger("click");
|
|
expect(inputs(w)[0]!.element.value).toBe("#10B981");
|
|
await w.findAll("button")[2]!.trigger("click");
|
|
expect(inputs(w)[0]!.element.value).toBe("");
|
|
expect(w.text()).toContain("(空)");
|
|
});
|
|
it("uses a live scoped slot in the custom root example", async () => {
|
|
const w = render(RenderFunction);
|
|
// Let mounted field metadata and child refs settle before exercising native focus.
|
|
await flushPromises();
|
|
// Vue timestamps bubbled native events and ignores events at or before an
|
|
// ancestor listener's attachment time. Keep focus in a later clock tick,
|
|
// as it would be after a user action, without depending on machine speed.
|
|
vi.useFakeTimers({ toFake: ["Date"] });
|
|
vi.setSystemTime(Date.now() + 1);
|
|
inputs(w)[0]!.element.focus();
|
|
await flushPromises();
|
|
expect(w.get("[data-slot=color-field]").attributes("data-focused")).toBe(
|
|
"true",
|
|
);
|
|
expect(
|
|
w.get("[data-slot=color-input-group]").attributes("data-field-focused"),
|
|
).toBe("true");
|
|
});
|
|
it("submits the accepted form color and restores empty state after saving", async () => {
|
|
vi.useFakeTimers();
|
|
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
const w = render(FormExample);
|
|
expect(w.get<HTMLButtonElement>("button").element.disabled).toBe(true);
|
|
await inputs(w)[0]!.setValue("abc");
|
|
await inputs(w)[0]!.trigger("blur");
|
|
expect(w.get<HTMLButtonElement>("button").element.disabled).toBe(false);
|
|
expect(new FormData(w.get("form").element).get("brand-color")).toBe(
|
|
"#AABBCC",
|
|
);
|
|
await w.get("form").trigger("submit");
|
|
expect(w.text()).toContain("保存中…");
|
|
await vi.advanceTimersByTimeAsync(1500);
|
|
expect(log).toHaveBeenCalledWith("已提交颜色:", { color: "#AABBCC" });
|
|
expect(inputs(w)[0]!.element.value).toBe("");
|
|
expect(w.getComponent(ColorField).emitted("change")).toHaveLength(1);
|
|
});
|
|
});
|