792 lines
23 KiB
TypeScript
792 lines
23 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { defineComponent, h, nextTick, ref } from "vue";
|
|
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("checks candidates 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");
|
|
});
|
|
});
|
|
|
|
function pasteInto(input: Element, text: string) {
|
|
const event = new Event("paste", { bubbles: true, cancelable: true });
|
|
Object.defineProperty(event, "clipboardData", {
|
|
value: { getData: () => text },
|
|
});
|
|
input.dispatchEvent(event);
|
|
}
|
|
const settleOtp = async () => {
|
|
await nextTick();
|
|
await nextTick();
|
|
await nextTick();
|
|
};
|
|
function enhancedOtp(
|
|
props: Record<string, unknown> = {},
|
|
slots: Record<string, unknown> = {},
|
|
) {
|
|
return mount(InputOTP, {
|
|
props: { maxLength: 4, ...props },
|
|
slots: {
|
|
default: () =>
|
|
h(InputOTPGroup, {}, () =>
|
|
[0, 1, 2, 3].map((index) => h(InputOTPSlot, { index })),
|
|
),
|
|
...slots,
|
|
},
|
|
attachTo: document.body,
|
|
});
|
|
}
|
|
it("OTP autoFocus and textAlign reach actual inputs without leaking root props", async () => {
|
|
const w = enhancedOtp({ autoFocus: true, textAlign: "right" });
|
|
try {
|
|
await settleOtp();
|
|
expect(document.activeElement).toBe(slotInputs(w)[0].element);
|
|
expect((slotInputs(w)[0].element as HTMLInputElement).style.textAlign).toBe(
|
|
"right",
|
|
);
|
|
expect(w.attributes("textalign")).toBeUndefined();
|
|
await w.setProps({ textAlign: "center" });
|
|
expect((slotInputs(w)[3].element as HTMLInputElement).style.textAlign).toBe(
|
|
"center",
|
|
);
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it.each(["isDisabled", "isReadOnly"])(
|
|
"OTP %s prevents paste transformation and edits",
|
|
async (flag) => {
|
|
const transform = vi.fn((text: string) => text.replaceAll("-", ""));
|
|
const w = enhancedOtp({
|
|
[flag]: true,
|
|
defaultValue: "12",
|
|
pasteTransformer: transform,
|
|
autoFocus: true,
|
|
});
|
|
try {
|
|
await settleOtp();
|
|
pasteInto(slotInputs(w)[0].element, "34-56");
|
|
await settleOtp();
|
|
expect(transform).not.toHaveBeenCalled();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("12");
|
|
if (flag === "isDisabled")
|
|
expect(w.element.contains(document.activeElement)).toBe(false);
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
},
|
|
);
|
|
it("OTP transforms paste before whole-value pattern checking and restores rejected DOM values", async () => {
|
|
const w = enhancedOtp({
|
|
pattern: "^12[0-9]*$",
|
|
defaultValue: "12",
|
|
pasteTransformer: (t: string) => t.replaceAll("-", ""),
|
|
});
|
|
try {
|
|
pasteInto(slotInputs(w)[0].element, "12-34");
|
|
await settleOtp();
|
|
expect(
|
|
slotInputs(w).map((i) => (i.element as HTMLInputElement).value),
|
|
).toEqual(["1", "2", "3", "4"]);
|
|
expect(document.activeElement).toBe(slotInputs(w)[3].element);
|
|
expect((slotInputs(w)[3].element as HTMLInputElement).selectionStart).toBe(
|
|
0,
|
|
);
|
|
await slotInputs(w)[2].setValue("8");
|
|
await settleOtp();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("1284");
|
|
await slotInputs(w)[0].setValue("9");
|
|
await settleOtp();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("1284");
|
|
pasteInto(slotInputs(w)[0].element, "90-12");
|
|
await settleOtp();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("1284");
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it("OTP rejects an invalid paste as one edit and honors consumer cancellation", async () => {
|
|
const w = enhancedOtp({ pattern: REGEXP_ONLY_DIGITS, defaultValue: "12" });
|
|
try {
|
|
pasteInto(slotInputs(w)[0].element, "3a45");
|
|
await settleOtp();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("12");
|
|
const transform = vi.fn((t: string) => t);
|
|
await w.setProps({ pasteTransformer: transform });
|
|
const slot = w.element;
|
|
slot.addEventListener("paste", (e: Event) => e.preventDefault(), {
|
|
capture: true,
|
|
});
|
|
pasteInto(slotInputs(w)[0].element, "5678");
|
|
await settleOtp();
|
|
expect(transform).not.toHaveBeenCalled();
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it("OTP complete follows accepted threshold crossings, including external updates, rather than rejected drafts", async () => {
|
|
const w = enhancedOtp({ modelValue: "12" });
|
|
try {
|
|
pasteInto(slotInputs(w)[0].element, "1234");
|
|
await settleOtp();
|
|
pasteInto(slotInputs(w)[0].element, "1234");
|
|
await settleOtp();
|
|
expect(w.emitted("update:modelValue")).toHaveLength(2);
|
|
expect(w.emitted("complete")).toBeUndefined();
|
|
expect(
|
|
slotInputs(w)
|
|
.map((i) => (i.element as HTMLInputElement).value)
|
|
.join(""),
|
|
).toBe("12");
|
|
await w.setProps({ modelValue: "1234" });
|
|
await settleOtp();
|
|
expect(w.emitted("complete")).toEqual([["1234"]]);
|
|
await w.setProps({ modelValue: "9876" });
|
|
await settleOtp();
|
|
pasteInto(slotInputs(w)[0].element, "9876");
|
|
await settleOtp();
|
|
expect(w.emitted("complete")).toHaveLength(1);
|
|
await w.setProps({ modelValue: "" });
|
|
await settleOtp();
|
|
await w.setProps({ modelValue: "4567" });
|
|
await settleOtp();
|
|
expect(w.emitted("complete")).toEqual([["1234"], ["4567"]]);
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it("OTP passes caller ValidityState through shared FieldError without emitting it on the DOM", async () => {
|
|
const { FieldError } = await import("../packages/vue/src");
|
|
const details = {
|
|
badInput: false,
|
|
customError: true,
|
|
patternMismatch: false,
|
|
rangeOverflow: false,
|
|
rangeUnderflow: false,
|
|
stepMismatch: false,
|
|
tooLong: false,
|
|
tooShort: false,
|
|
typeMismatch: false,
|
|
valid: false,
|
|
valueMissing: false,
|
|
};
|
|
const w = enhancedOtp(
|
|
{
|
|
isInvalid: true,
|
|
validationDetails: details,
|
|
validationErrors: ["server"],
|
|
},
|
|
{
|
|
default: () => [
|
|
h(InputOTPGroup, {}, () =>
|
|
[0, 1, 2, 3].map((index) => h(InputOTPSlot, { index })),
|
|
),
|
|
h(
|
|
FieldError,
|
|
{},
|
|
{
|
|
default: (s: { validationDetails?: ValidityState }) =>
|
|
String(s.validationDetails?.customError),
|
|
},
|
|
),
|
|
],
|
|
},
|
|
);
|
|
try {
|
|
await settleOtp();
|
|
expect(w.get('[data-slot="field-error"]').text()).toBe("true");
|
|
expect(w.attributes("validationdetails")).toBeUndefined();
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
|
|
it("OTP native required validation shares FieldError details and clears on an effective edit", async () => {
|
|
const { FieldError } = await import("../packages/vue/src");
|
|
const w = enhancedOtp(
|
|
{ required: true },
|
|
{
|
|
default: () => [
|
|
h(InputOTPGroup, {}, () =>
|
|
[0, 1, 2, 3].map((index) => h(InputOTPSlot, { index })),
|
|
),
|
|
h(
|
|
FieldError,
|
|
{},
|
|
{
|
|
default: (s: { validationDetails?: ValidityState }) =>
|
|
String(s.validationDetails?.valueMissing),
|
|
},
|
|
),
|
|
],
|
|
},
|
|
);
|
|
try {
|
|
await settleOtp();
|
|
const proxy = w.get('input[aria-hidden="true"]')
|
|
.element as HTMLInputElement;
|
|
expect(proxy.checkValidity()).toBe(false);
|
|
await settleOtp();
|
|
expect(w.get('[role="alert"]').text()).toBe("true");
|
|
await slotInputs(w)[0].setValue("1");
|
|
await settleOtp();
|
|
expect(w.find('[role="alert"]').exists()).toBe(false);
|
|
expect(proxy.checkValidity()).toBe(true);
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|