import { afterEach, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, h, ref } from "vue"; import { InputOTP, InputOTPGroup, InputOTPSlot, Label, Description, FieldError, Fieldset, Form, Pagination, PaginationLink, } from "../packages/vue/src"; const wrappers: ReturnType[] = []; afterEach(async () => { wrappers.splice(0).forEach((w) => w.unmount()); await flushPromises(); document.body.innerHTML = ""; }); function fixture(template: string, setup = () => ({})) { const w = mount( defineComponent({ components: { InputOTP, InputOTPGroup, InputOTPSlot, Label, Description, FieldError, Fieldset, Form, Pagination, PaginationLink, }, template, setup, }), { attachTo: document.body }, ); wrappers.push(w); return w; } const slots = ''; const inputs = (w: ReturnType) => w.findAll('input[data-slot="input-otp-input"]'); it("noncontrolled OTP restores its initial visible and submitted value on native reset", async () => { const w = fixture( `
${slots}
`, ); await inputs(w)[0].setValue("9"); await flushPromises(); expect(new FormData(w.get("form").element).get("code")).toBe("92"); w.get("form").element.reset(); await flushPromises(); expect( inputs(w) .map((i) => (i.element as HTMLInputElement).value) .join(""), ).toBe("12"); expect(new FormData(w.get("form").element).get("code")).toBe("12"); }); it("controlled OTP middle deletion keeps visual slots aligned with real inputs", async () => { const value = ref("1234"); const w = fixture( `${slots}`, () => ({ value }), ); await inputs(w)[1].trigger("keydown", { key: "Delete" }); await flushPromises(); expect(inputs(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"]); expect( w.findAll('[data-slot="input-otp-slot"]')[1].attributes("data-filled"), ).toBeUndefined(); }); it("rejecting a controlled update still allows a later identical request", async () => { const update = vi.fn(); const w = fixture( `${slots}`, () => ({ update }), ); await inputs(w)[2].setValue("9"); await flushPromises(); expect( inputs(w) .map((i) => (i.element as HTMLInputElement).value) .join(""), ).toBe("12"); await inputs(w)[2].setValue("9"); await flushPromises(); expect(update).toHaveBeenCalledTimes(2); }); it("OTP readonly blocks editing and deletion", async () => { const w = fixture( `${slots}`, ); expect((inputs(w)[0].element as HTMLInputElement).readOnly).toBe(true); await inputs(w)[0].trigger("keydown", { key: "Delete" }); await flushPromises(); expect( inputs(w) .map((i) => (i.element as HTMLInputElement).value) .join(""), ).toBe("1234"); }); it("OTP consumes Fieldset disabled for real input state and visual slots", async () => { const w = fixture( `
${slots}
`, ); expect((inputs(w)[0].element as HTMLInputElement).disabled).toBe(true); expect( w.get('[data-slot="input-otp-slot"]').attributes("data-disabled"), ).toBeDefined(); }); it("OTP real inputs reference descriptions and errors from their field context", async () => { const w = fixture( `${slots}Help`, ); await flushPromises(); for (const input of inputs(w)) { expect(input.attributes("aria-describedby")?.split(/\s+/)).toContain( "otp-help", ); expect(input.attributes("aria-describedby")?.split(/\s+/)).toContain( "otp-error", ); expect(input.attributes("aria-invalid")).toBe("true"); } }); it("OTP consumes named server errors from the public Form", async () => { const w = fixture( `
${slots}
`, ); await flushPromises(); expect(w.get('[data-slot="input-otp"]').attributes("data-invalid")).toBe( "true", ); expect(w.text()).toContain("Invalid code"); }); it("OTP inputClassName augments the structural input class", () => { const w = fixture( `${slots}`, ); expect(inputs(w)[0].classes()).toContain("custom-input"); expect(inputs(w)[0].classes()).toContain("hv-input-otp__input"); }); it("OTP mask does not reveal plaintext through its visual value layer", () => { const w = fixture( `${slots}`, ); expect( inputs(w).every((input) => input.attributes("type") === "password"), ).toBe(true); expect(w.get('[data-slot="input-otp-slot-value"]').text()).not.toBe("1"); }); it("PaginationLink preserves an explicit aria-current override when not isActive", () => { const w = fixture( 'Next step', ); expect(w.get("button").attributes("aria-current")).toBe("step"); }); it("OTP composed Label names the actual focusable input", async () => { const w = fixture( `${slots}`, ); await flushPromises(); const actual = inputs(w)[0]; const hasReference = actual .attributes("aria-labelledby") ?.split(/\s+/) .includes("otp-label"); const hasNativeLabel = Boolean(actual.attributes("id")) && w.get("label").attributes("for") === actual.attributes("id"); const hasExplicitName = actual .attributes("aria-label") ?.includes("Verification code"); expect(Boolean(hasReference || hasNativeLabel || hasExplicitName)).toBe(true); }); it("OTP dynamically added readonly reaches the real input", async () => { const extra = ref>({}); const w = fixture( `${slots}`, () => ({ extra }), ); expect((inputs(w)[0].element as HTMLInputElement).readOnly).toBe(false); extra.value = { readonly: true }; await flushPromises(); expect((inputs(w)[0].element as HTMLInputElement).readOnly).toBe(true); }); it("OTP edit dismisses the current server error response", async () => { const w = fixture( `
${slots}
`, ); await inputs(w)[0].setValue("9"); await flushPromises(); expect( w.get('[data-slot="input-otp"]').attributes("data-invalid"), ).toBeUndefined(); expect(w.text()).not.toContain("Invalid code"); }); it("OTP does not lose valid characters with a global RegExp filter", async () => { const filter = /\d/g; const w = fixture( `${slots}`, () => ({ filter }), ); for (let i = 0; i < 4; i++) await inputs(w)[i].setValue(String(i + 1)); await flushPromises(); expect( inputs(w) .map((input) => (input.element as HTMLInputElement).value) .join(""), ).toBe("1234"); }); it("OTP Slot keydown.prevent cancels editing before the bottom-layer handler", async () => { const w = fixture( ``, ); await inputs(w)[1].trigger("keydown", { key: "Delete" }); await flushPromises(); expect( inputs(w) .map((input) => (input.element as HTMLInputElement).value) .join(""), ).toBe("1234"); }); it("OTP capture handler arrays honor stopImmediatePropagation", async () => { const stop = (event: Event) => event.stopImmediatePropagation(); const after = vi.fn(); const w = fixture( ``, () => ({ stop, after }), ); await inputs(w)[1].trigger("keydown", { key: "Delete" }); await flushPromises(); expect(after).not.toHaveBeenCalled(); }); it("OTP resizing retains existing empty slot indices", async () => { const length = ref(4); const w = fixture( ``, () => ({ length }), ); await inputs(w)[1].trigger("keydown", { key: "Delete" }); await flushPromises(); length.value = 5; await flushPromises(); expect( inputs(w).map((input) => (input.element as HTMLInputElement).value), ).toEqual(["1", "", "3", "4", ""]); }); it("OTP form and validation examples have unique field IDs on the same page", async () => { const formDemo = await import("../apps/docs/demos/input-otp-form-example.vue"); const validationDemo = await import("../apps/docs/demos/input-otp-with-validation.vue"); const wrapper = mount( defineComponent({ render: () => h("div", [h(formDemo.default), h(validationDemo.default)]), }), { attachTo: document.body }, ); wrappers.push(wrapper); await flushPromises(); const ids = wrapper .findAll("[id]") .map((element) => element.attributes("id")); expect(new Set(ids).size).toBe(ids.length); }); it("OTP primitive fallback name cannot override a composed Label or explicit name", async () => { const w = fixture( `${slots}`, ); await flushPromises(); for (const input of inputs(w)) expect(input.attributes("aria-labelledby")?.split(/\s+/)).toContain( "otp-composed", ); const explicit = fixture( `${slots}`, ); await flushPromises(); expect(inputs(explicit)[0].attributes("aria-label")).toBe("Security code"); });