fix: complete field and OTP public contracts (HD-07)

This commit is contained in:
maoyingjie
2026-10-29 09:30:58 +08:00
parent 27cc62e36f
commit 2d7e3d4e1e
26 changed files with 1067 additions and 100 deletions
+250 -1
View File
@@ -219,7 +219,7 @@ describe("InputOTP value model", () => {
});
describe("InputOTP pattern and states", () => {
it("filters characters with REGEXP_ONLY_CHARS and digits constants", async () => {
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]+$");
@@ -540,3 +540,252 @@ describe("InputOTP types", () => {
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();
}
});