feat: migrate Pagination Header and InputOTP with field and event regressions
This commit is contained in:
@@ -0,0 +1,563 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { createSSRApp, defineComponent, h, nextTick, ref } from "vue";
|
||||
import { renderToString } from "@vue/server-renderer";
|
||||
import {
|
||||
InputOTP,
|
||||
InputOTPGroup,
|
||||
InputOTPRoot,
|
||||
InputOTPSeparator,
|
||||
InputOTPSlot,
|
||||
REGEXP_ONLY_CHARS,
|
||||
REGEXP_ONLY_DIGITS,
|
||||
REGEXP_ONLY_DIGITS_AND_CHARS,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
function mountOtp(
|
||||
opts: {
|
||||
maxLength?: number;
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
pattern?: string | RegExp;
|
||||
isDisabled?: boolean;
|
||||
isInvalid?: boolean;
|
||||
validationErrors?: string[];
|
||||
name?: string;
|
||||
required?: boolean;
|
||||
variant?: "primary" | "secondary";
|
||||
inputClassName?: string;
|
||||
otp?: boolean;
|
||||
onComplete?: (v: string) => void;
|
||||
} = {},
|
||||
) {
|
||||
const value = ref(opts.modelValue ?? opts.defaultValue ?? "");
|
||||
const controlled = opts.modelValue !== undefined;
|
||||
const updates: string[] = [];
|
||||
const completes: string[] = [];
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => ({ value }),
|
||||
render: () =>
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: opts.maxLength ?? 6,
|
||||
modelValue: controlled ? value.value : undefined,
|
||||
defaultValue: opts.defaultValue,
|
||||
pattern: opts.pattern,
|
||||
isDisabled: opts.isDisabled,
|
||||
isInvalid: opts.isInvalid,
|
||||
validationErrors: opts.validationErrors,
|
||||
name: opts.name,
|
||||
required: opts.required,
|
||||
variant: opts.variant,
|
||||
inputClassName: opts.inputClassName,
|
||||
otp: opts.otp,
|
||||
"onUpdate:modelValue": (v: string) => {
|
||||
value.value = v;
|
||||
updates.push(v);
|
||||
},
|
||||
onComplete: (v: string) => {
|
||||
completes.push(v);
|
||||
opts.onComplete?.(v);
|
||||
},
|
||||
},
|
||||
() => [
|
||||
h(InputOTPGroup, () =>
|
||||
Array.from({ length: opts.maxLength ?? 6 }, (_, i) =>
|
||||
h(InputOTPSlot, { index: i }),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
return { w, value, updates, completes, controlled };
|
||||
}
|
||||
|
||||
function slotInputs(w: ReturnType<typeof mount>) {
|
||||
return w.findAll("input[data-slot=input-otp-input]");
|
||||
}
|
||||
|
||||
describe("InputOTP structure", () => {
|
||||
it("exports InputOTP as Root alias and renders slots/groups", () => {
|
||||
expect(InputOTP).toBe(InputOTPRoot);
|
||||
const { w } = mountOtp({ maxLength: 4, variant: "secondary" });
|
||||
const root = w.get('[data-slot="input-otp"]');
|
||||
expect(root.classes().join(" ")).toContain("hv-input-otp");
|
||||
expect(root.classes().join(" ")).toContain("hv-input-otp--secondary");
|
||||
expect(w.findAll('[data-slot="input-otp-slot"]')).toHaveLength(4);
|
||||
expect(slotInputs(w)).toHaveLength(4);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("renders separator and applies inputClassName to real inputs only", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(InputOTP, { maxLength: 2, inputClassName: "custom-input" }, () => [
|
||||
h(InputOTPGroup, () => h(InputOTPSlot, { index: 0 })),
|
||||
h(InputOTPSeparator, { class: "custom-sep" }),
|
||||
h(InputOTPGroup, () => h(InputOTPSlot, { index: 1 })),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
expect(w.get('[data-slot="input-otp-separator"]').classes()).toContain(
|
||||
"custom-sep",
|
||||
);
|
||||
for (const input of slotInputs(w)) {
|
||||
expect(input.classes()).toContain("custom-input");
|
||||
}
|
||||
expect(w.get('[data-slot="input-otp"]').classes()).not.toContain(
|
||||
"custom-input",
|
||||
);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP value model", () => {
|
||||
it("supports uncontrolled defaultValue and typed updates as strings", async () => {
|
||||
const { w, value, updates } = mountOtp({ defaultValue: "12" });
|
||||
await nextTick();
|
||||
const slots = w.findAll('[data-slot="input-otp-slot"]');
|
||||
expect(slots[0].text()).toContain("1");
|
||||
expect(slots[1].text()).toContain("2");
|
||||
expect(slots[2].text()).not.toContain("3");
|
||||
|
||||
await slotInputs(w)[2].setValue("3");
|
||||
await nextTick();
|
||||
expect(value.value).toBe("123");
|
||||
expect(updates.at(-1)).toBe("123");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("controlled parent rejects update and restores actual input", async () => {
|
||||
const value = ref("12");
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => ({ value }),
|
||||
render: () =>
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: 4,
|
||||
modelValue: value.value,
|
||||
// Parent always keeps "12"
|
||||
"onUpdate:modelValue": () => {},
|
||||
},
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
const list = slotInputs(w);
|
||||
await list[2].setValue("9");
|
||||
await nextTick();
|
||||
// Parent rejected; display should still be 12
|
||||
expect(value.value).toBe("12");
|
||||
const slots = w.findAll('[data-slot="input-otp-slot"]');
|
||||
expect(slots[0].text()).toContain("1");
|
||||
expect(slots[2].text()).not.toContain("9");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("keeps middle empty slot without left-shifting later characters", async () => {
|
||||
const { w } = mountOtp({ defaultValue: "123456" });
|
||||
await nextTick();
|
||||
await slotInputs(w)[1].setValue("");
|
||||
await nextTick();
|
||||
const slots = w.findAll('[data-slot="input-otp-slot"]');
|
||||
expect(slots[1].text()).not.toContain("2");
|
||||
expect(slots[2].text()).toContain("3");
|
||||
expect(slots[3].text()).toContain("4");
|
||||
expect(slots[5].text()).toContain("6");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("clears value and restores on retype", async () => {
|
||||
const { w, value } = mountOtp({ defaultValue: "12" });
|
||||
await nextTick();
|
||||
await slotInputs(w)[0].setValue("");
|
||||
await slotInputs(w)[1].setValue("");
|
||||
await nextTick();
|
||||
expect(value.value).toBe("");
|
||||
await slotInputs(w)[0].setValue("9");
|
||||
await nextTick();
|
||||
expect(value.value).toBe("9");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("emits complete on first fill and not on same-value rewrite", async () => {
|
||||
const completes: string[] = [];
|
||||
const { w, value } = mountOtp({
|
||||
maxLength: 3,
|
||||
onComplete: (v: string) => completes.push(v),
|
||||
});
|
||||
const list = slotInputs(w);
|
||||
await list[0].setValue("1");
|
||||
await list[1].setValue("2");
|
||||
await list[2].setValue("3");
|
||||
await nextTick();
|
||||
expect(completes.at(-1)).toBe("123");
|
||||
const count = completes.length;
|
||||
// Re-set same value should not grow complete
|
||||
await list[2].setValue("3");
|
||||
await nextTick();
|
||||
expect(completes.length).toBe(count);
|
||||
expect(value.value).toBe("123");
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP pattern and states", () => {
|
||||
it("filters characters with REGEXP_ONLY_CHARS and digits constants", async () => {
|
||||
expect(REGEXP_ONLY_DIGITS).toBe("^\\d+$");
|
||||
expect(REGEXP_ONLY_CHARS).toBe("^[a-zA-Z]+$");
|
||||
expect(REGEXP_ONLY_DIGITS_AND_CHARS).toBe("^[a-zA-Z0-9]+$");
|
||||
|
||||
const { w, value } = mountOtp({
|
||||
maxLength: 4,
|
||||
pattern: REGEXP_ONLY_DIGITS,
|
||||
});
|
||||
await slotInputs(w)[0].setValue("a");
|
||||
await nextTick();
|
||||
expect(value.value).toBe("");
|
||||
await slotInputs(w)[0].setValue("7");
|
||||
await nextTick();
|
||||
expect(value.value).toBe("7");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("marks disabled, invalid and filled data attributes", async () => {
|
||||
const { w } = mountOtp({
|
||||
defaultValue: "1",
|
||||
isDisabled: true,
|
||||
isInvalid: true,
|
||||
});
|
||||
await nextTick();
|
||||
expect(
|
||||
w.get('[data-slot="input-otp"]').attributes("data-disabled"),
|
||||
).toBeDefined();
|
||||
expect(
|
||||
w.get('[data-slot="input-otp"]').attributes("data-invalid"),
|
||||
).toBeDefined();
|
||||
const slots = w.findAll('[data-slot="input-otp-slot"]');
|
||||
expect(slots[0].attributes("data-filled")).toBeDefined();
|
||||
expect(slots[0].attributes("data-disabled")).toBeDefined();
|
||||
expect(slots[0].attributes("data-invalid")).toBeDefined();
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("shows validation errors via field context", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h("div", () => [
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: 4,
|
||||
isInvalid: true,
|
||||
validationErrors: ["验证码无效"],
|
||||
name: "code",
|
||||
},
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
// FieldError is optional; invalid state on root is enough for this check
|
||||
expect(
|
||||
w.get('[data-slot="input-otp"]').attributes("data-invalid"),
|
||||
).toBeDefined();
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP form and label", () => {
|
||||
it("submits a single form value and omits when disabled", async () => {
|
||||
const { w } = mountOtp({
|
||||
name: "code",
|
||||
defaultValue: "123456",
|
||||
});
|
||||
await nextTick();
|
||||
const form = document.createElement("form");
|
||||
form.appendChild(w.element);
|
||||
document.body.appendChild(form);
|
||||
const data = new FormData(form);
|
||||
expect(data.get("code")).toBe("123456");
|
||||
form.remove();
|
||||
|
||||
const d = mountOtp({
|
||||
name: "code",
|
||||
defaultValue: "12",
|
||||
isDisabled: true,
|
||||
});
|
||||
await nextTick();
|
||||
const form2 = document.createElement("form");
|
||||
form2.appendChild(d.w.element);
|
||||
document.body.appendChild(form2);
|
||||
const data2 = new FormData(form2);
|
||||
expect(data2.get("code")).toBeNull();
|
||||
form2.remove();
|
||||
d.w.unmount();
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("resets uncontrolled value to defaultValue and keeps controlled parent value", async () => {
|
||||
const uncontrolled = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h("form", null, [
|
||||
h(
|
||||
InputOTP,
|
||||
{ maxLength: 4, defaultValue: "12", name: "code" },
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
const uInputs = slotInputs(uncontrolled);
|
||||
await uInputs[0].setValue("9");
|
||||
await nextTick();
|
||||
expect(new FormData(uncontrolled.get("form").element).get("code")).toBe(
|
||||
"92",
|
||||
);
|
||||
uncontrolled.get("form").element.reset();
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(uncontrolled)
|
||||
.map((i) => (i.element as HTMLInputElement).value)
|
||||
.join(""),
|
||||
).toBe("12");
|
||||
expect(new FormData(uncontrolled.get("form").element).get("code")).toBe(
|
||||
"12",
|
||||
);
|
||||
|
||||
// Cancelable reset keeps the edited value.
|
||||
await slotInputs(uncontrolled)[0].setValue("9");
|
||||
await nextTick();
|
||||
const form = uncontrolled.get("form").element as HTMLFormElement;
|
||||
form.addEventListener(
|
||||
"reset",
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
},
|
||||
true,
|
||||
);
|
||||
form.reset();
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(uncontrolled)
|
||||
.map((i) => (i.element as HTMLInputElement).value)
|
||||
.join(""),
|
||||
).toBe("92");
|
||||
uncontrolled.unmount();
|
||||
|
||||
const value = ref("34");
|
||||
const controlled = mount(
|
||||
defineComponent({
|
||||
setup: () => ({ value }),
|
||||
render: () =>
|
||||
h("form", null, [
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: 4,
|
||||
modelValue: value.value,
|
||||
"onUpdate:modelValue": () => {
|
||||
/* parent rejects */
|
||||
},
|
||||
},
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await slotInputs(controlled)[0].setValue("9");
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(controlled)
|
||||
.map((i) => (i.element as HTMLInputElement).value)
|
||||
.join(""),
|
||||
).toBe("34");
|
||||
controlled.get("form").element.reset();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(controlled)
|
||||
.map((i) => (i.element as HTMLInputElement).value)
|
||||
.join(""),
|
||||
).toBe("34");
|
||||
controlled.unmount();
|
||||
});
|
||||
|
||||
it("restores real inputs after a rejected controlled update and allows retry", async () => {
|
||||
const updates: string[] = [];
|
||||
const value = ref("12");
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => ({ value }),
|
||||
render: () =>
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: 4,
|
||||
modelValue: value.value,
|
||||
"onUpdate:modelValue": (v: string) => {
|
||||
updates.push(v);
|
||||
},
|
||||
},
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await slotInputs(w)[2].setValue("9");
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(w)
|
||||
.map((i) => (i.element as HTMLInputElement).value)
|
||||
.join(""),
|
||||
).toBe("12");
|
||||
await slotInputs(w)[2].setValue("9");
|
||||
await nextTick();
|
||||
expect(updates.filter((u) => u === "129").length).toBe(2);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("middle deletion keeps slot values aligned without left-shift", async () => {
|
||||
const value = ref("1234");
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => ({ value }),
|
||||
render: () =>
|
||||
h(
|
||||
InputOTP,
|
||||
{
|
||||
maxLength: 4,
|
||||
modelValue: value.value,
|
||||
"onUpdate:modelValue": (v: string) => {
|
||||
value.value = v;
|
||||
},
|
||||
},
|
||||
() =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await slotInputs(w)[1].trigger("keydown", { key: "Delete" });
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(
|
||||
slotInputs(w).map((i) => (i.element as HTMLInputElement).value),
|
||||
).toEqual(["1", "", "3", "4"]);
|
||||
expect(
|
||||
w.findAll('[data-slot="input-otp-slot-value"]').map((i) => i.text()),
|
||||
).toEqual(["1", "3", "4"]);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP demos", () => {
|
||||
it("mounts all 11 demos without Vue warnings", async () => {
|
||||
const names = [
|
||||
"basic",
|
||||
"variants",
|
||||
"on-surface",
|
||||
"disabled",
|
||||
"four-digits",
|
||||
"controlled",
|
||||
"on-complete",
|
||||
"form-example",
|
||||
"with-pattern",
|
||||
"with-validation",
|
||||
"custom-styles",
|
||||
];
|
||||
expect(names).toHaveLength(11);
|
||||
const warnings: string[] = [];
|
||||
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
|
||||
warnings.push(args.map(String).join(" "));
|
||||
});
|
||||
for (const name of names) {
|
||||
const mod = await import(`../apps/docs/demos/input-otp-${name}.vue`);
|
||||
const w = mount(mod.default, { attachTo: document.body });
|
||||
await nextTick();
|
||||
expect(w.html().length).toBeGreaterThan(0);
|
||||
w.unmount();
|
||||
}
|
||||
spy.mockRestore();
|
||||
expect(warnings).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP types", () => {
|
||||
it("accepts legal string model and rejects illegal variant", () => {
|
||||
const ok: InstanceType<typeof InputOTPRoot>["$props"] = {
|
||||
modelValue: "123",
|
||||
maxLength: 6,
|
||||
variant: "secondary",
|
||||
};
|
||||
expect(ok.modelValue).toBe("123");
|
||||
const bad: InstanceType<typeof InputOTPRoot>["$props"] = {
|
||||
maxLength: 4,
|
||||
// @ts-expect-error variant must be primary | secondary
|
||||
variant: "ghost",
|
||||
};
|
||||
expect((bad as { variant?: string }).variant).toBe("ghost");
|
||||
});
|
||||
});
|
||||
|
||||
describe("InputOTP SSR", () => {
|
||||
it("renders without touching browser globals", async () => {
|
||||
const html = await renderToString(
|
||||
createSSRApp({
|
||||
render: () =>
|
||||
h(InputOTP, { maxLength: 4, name: "code", defaultValue: "12" }, () =>
|
||||
h(InputOTPGroup, () =>
|
||||
[0, 1, 2, 3].map((i) => h(InputOTPSlot, { index: i })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(html).toContain('data-slot="input-otp"');
|
||||
expect(html).toContain("hv-input-otp");
|
||||
expect(html).toContain('data-slot="input-otp-slot"');
|
||||
expect(html).toContain("1");
|
||||
expect(html).toContain('name="code"');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user