298 lines
10 KiB
TypeScript
298 lines
10 KiB
TypeScript
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<typeof mount>[] = [];
|
|
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 =
|
|
'<InputOTPGroup><InputOTPSlot v-for="i in 4" :key="i" :index="i-1"/></InputOTPGroup>';
|
|
const inputs = (w: ReturnType<typeof mount>) =>
|
|
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(
|
|
`<form><InputOTP :max-length="4" default-value="12" name="code">${slots}</InputOTP></form>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" v-model="value">${slots}</InputOTP>`,
|
|
() => ({ 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(
|
|
`<InputOTP :max-length="4" model-value="12" @update:model-value="update">${slots}</InputOTP>`,
|
|
() => ({ 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(
|
|
`<InputOTP :max-length="4" default-value="1234" readonly>${slots}</InputOTP>`,
|
|
);
|
|
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(
|
|
`<Fieldset disabled><InputOTP :max-length="4">${slots}</InputOTP></Fieldset>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" is-invalid :validation-errors="['Invalid']"><Label>Code</Label>${slots}<Description id="otp-help">Help</Description><FieldError id="otp-error"/></InputOTP>`,
|
|
);
|
|
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(
|
|
`<Form :validation-errors="{code: 'Invalid code'}"><InputOTP :max-length="4" name="code">${slots}<FieldError/></InputOTP></Form>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" input-class-name="custom-input">${slots}</InputOTP>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" default-value="1234" mask>${slots}</InputOTP>`,
|
|
);
|
|
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(
|
|
'<Pagination><PaginationLink aria-current="step">Next step</PaginationLink></Pagination>',
|
|
);
|
|
expect(w.get("button").attributes("aria-current")).toBe("step");
|
|
});
|
|
|
|
it("OTP composed Label names the actual focusable input", async () => {
|
|
const w = fixture(
|
|
`<InputOTP :max-length="4"><Label id="otp-label">Verification code</Label>${slots}</InputOTP>`,
|
|
);
|
|
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<Record<string, unknown>>({});
|
|
const w = fixture(
|
|
`<InputOTP :max-length="4" default-value="12" v-bind="extra">${slots}</InputOTP>`,
|
|
() => ({ 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(
|
|
`<Form :validation-errors="{code:'Invalid code'}"><InputOTP :max-length="4" name="code" default-value="12">${slots}<FieldError/></InputOTP></Form>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" :pattern="filter">${slots}</InputOTP>`,
|
|
() => ({ 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(
|
|
`<InputOTP :max-length="4" default-value="1234"><InputOTPGroup><InputOTPSlot v-for="i in 4" :key="i" :index="i-1" @keydown.prevent/></InputOTPGroup></InputOTP>`,
|
|
);
|
|
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(
|
|
`<InputOTP :max-length="4" default-value="1234"><InputOTPGroup><InputOTPSlot v-for="i in 4" :key="i" :index="i-1" :onKeydownCapture="[stop,after]"/></InputOTPGroup></InputOTP>`,
|
|
() => ({ 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(
|
|
`<InputOTP :max-length="length" default-value="1234"><InputOTPGroup><InputOTPSlot v-for="i in length" :key="i" :index="i-1"/></InputOTPGroup></InputOTP>`,
|
|
() => ({ 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(
|
|
`<InputOTP :max-length="4"><Label id="otp-composed">Verification code</Label>${slots}</InputOTP>`,
|
|
);
|
|
await flushPromises();
|
|
for (const input of inputs(w))
|
|
expect(input.attributes("aria-labelledby")?.split(/\s+/)).toContain(
|
|
"otp-composed",
|
|
);
|
|
const explicit = fixture(
|
|
`<InputOTP :max-length="4" aria-label="Security code">${slots}</InputOTP>`,
|
|
);
|
|
await flushPromises();
|
|
expect(inputs(explicit)[0].attributes("aria-label")).toBe("Security code");
|
|
});
|