604 lines
19 KiB
TypeScript
604 lines
19 KiB
TypeScript
import { describe, it, expect, vi } from "vitest";
|
|
import { mount } from "@vue/test-utils";
|
|
import { defineComponent, h, nextTick, ref } from "vue";
|
|
import {
|
|
TextField,
|
|
Form,
|
|
Input,
|
|
TextArea,
|
|
Label,
|
|
Description,
|
|
FieldError,
|
|
Switch,
|
|
SwitchContent,
|
|
SwitchControl,
|
|
SwitchThumb,
|
|
} from "../packages/vue/src";
|
|
|
|
function textField(props = {}, control = Input) {
|
|
return h(TextField, props, () => [
|
|
h(Label, {}, () => "Name"),
|
|
h(control),
|
|
h(Description, {}, () => "Hint"),
|
|
h(FieldError),
|
|
]);
|
|
}
|
|
const settle = async () => {
|
|
await nextTick();
|
|
await nextTick();
|
|
};
|
|
|
|
describe("shared text fields", () => {
|
|
it.each([Input, TextArea])(
|
|
"associates labels descriptions errors and unique IDs",
|
|
async (control) => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h("div", [
|
|
textField({ isInvalid: true }, control),
|
|
textField({}, control),
|
|
]),
|
|
}),
|
|
);
|
|
await settle();
|
|
const fields = w.findAll("[data-slot=textfield]");
|
|
const a = fields[0].get("input,textarea"),
|
|
b = fields[1].get("input,textarea");
|
|
expect(a.attributes("id")).not.toBe(b.attributes("id"));
|
|
expect(fields[0].get("label").attributes("for")).toBe(a.attributes("id"));
|
|
const ids = a.attributes("aria-describedby")!.split(" ");
|
|
for (const id of ids)
|
|
expect(fields[0].find(`[id="${id}"]`).exists()).toBe(true);
|
|
expect(fields[1].find("[data-slot=field-error]").exists()).toBe(false);
|
|
w.unmount();
|
|
},
|
|
);
|
|
it("keeps external description IDs and updates explicit description IDs and unmount", async () => {
|
|
const id = ref("first"),
|
|
show = ref(true);
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(TextField, {}, () => [
|
|
h(Input, { "aria-describedby": "external" }),
|
|
show.value ? h(Description, { id: id.value }, () => "hint") : null,
|
|
]),
|
|
}),
|
|
);
|
|
await settle();
|
|
expect(w.get("input").attributes("aria-describedby")).toContain("first");
|
|
id.value = "next";
|
|
await settle();
|
|
expect(w.get("input").attributes("aria-describedby")).toBe("external next");
|
|
show.value = false;
|
|
await settle();
|
|
expect(w.get("input").attributes("aria-describedby")).toBe("external");
|
|
w.unmount();
|
|
});
|
|
it.each([Input, TextArea])(
|
|
"submits and resets native values, disabled omitted and readonly kept",
|
|
async (control) => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, {}, () => [
|
|
textField({ name: "title", defaultValue: "start" }, control),
|
|
textField(
|
|
{ name: "disabled", defaultValue: "no", isDisabled: true },
|
|
control,
|
|
),
|
|
textField(
|
|
{ name: "read", defaultValue: "yes", isReadOnly: true },
|
|
control,
|
|
),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const input = w.findAll("input,textarea")[0];
|
|
await input.setValue("changed");
|
|
expect(
|
|
Object.fromEntries(
|
|
new FormData(w.get("form").element as HTMLFormElement),
|
|
),
|
|
).toEqual({ title: "changed", read: "yes" });
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((input.element as HTMLInputElement).value).toBe("start");
|
|
w.unmount();
|
|
},
|
|
);
|
|
it("respects canceled reset and root controlled rejection", async () => {
|
|
const value = ref("fixed");
|
|
const update = vi.fn();
|
|
const cancel = ref(true);
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(
|
|
Form,
|
|
{
|
|
onReset: (e: Event) => {
|
|
if (cancel.value) e.preventDefault();
|
|
},
|
|
},
|
|
() => [
|
|
textField({
|
|
modelValue: value.value,
|
|
"onUpdate:modelValue": update,
|
|
name: "x",
|
|
}),
|
|
],
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await w.get("input").setValue("rejected");
|
|
await settle();
|
|
expect((w.get("input").element as HTMLInputElement).value).toBe("fixed");
|
|
expect(update).toHaveBeenCalledWith("rejected");
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((w.get("input").element as HTMLInputElement).value).toBe("fixed");
|
|
w.unmount();
|
|
const canceled = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onReset: (e: Event) => e.preventDefault() }, () => [
|
|
textField({ defaultValue: "a" }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await canceled.get("input").setValue("b");
|
|
(canceled.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect((canceled.get("input").element as HTMLInputElement).value).toBe("b");
|
|
canceled.unmount();
|
|
});
|
|
it("supports a controlled leaf within an uncontrolled TextField", async () => {
|
|
const value = ref("leaf");
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(
|
|
TextField,
|
|
{
|
|
validate: (v: string) => (v === "leaf" ? null : "bad"),
|
|
validationBehavior: "aria",
|
|
},
|
|
() => [h(Input, { modelValue: value.value }), h(FieldError)],
|
|
),
|
|
}),
|
|
);
|
|
await settle();
|
|
expect(w.find("[data-slot=field-error]").exists()).toBe(false);
|
|
await w.get("input").setValue("refused");
|
|
await settle();
|
|
expect((w.get("input").element as HTMLInputElement).value).toBe("leaf");
|
|
expect(w.find("[data-slot=field-error]").exists()).toBe(false);
|
|
w.unmount();
|
|
});
|
|
it("inherits disabled required readonly and variants while preserving explicit native attrs", async () => {
|
|
const w = mount(TextField, {
|
|
props: { isDisabled: true, isRequired: true, variant: "secondary" },
|
|
slots: {
|
|
default: () => [h(Label, {}, () => "X"), h(Input), h(TextArea)],
|
|
},
|
|
});
|
|
expect(w.get("input").attributes("disabled")).toBeDefined();
|
|
expect(w.get("textarea").classes()).toContain("hv-textarea--secondary");
|
|
expect(w.get("label").classes()).toContain("hv-label--required");
|
|
await w.setProps({ isDisabled: false, isReadOnly: true });
|
|
expect(w.get("input").attributes("readonly")).toBeDefined();
|
|
expect(w.get("input").attributes("required")).toBeUndefined();
|
|
w.unmount();
|
|
});
|
|
it("shows native required errors on submit and blocks submit callbacks", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onSubmit: submit }, () => [
|
|
textField({ name: "email", type: "email", isRequired: true }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const form = w.get("form").element as HTMLFormElement;
|
|
const event = new Event("submit", { cancelable: true, bubbles: true });
|
|
form.dispatchEvent(event);
|
|
await settle();
|
|
expect(event.defaultPrevented).toBe(true);
|
|
expect(submit).not.toHaveBeenCalled();
|
|
expect(w.get("input").attributes("aria-invalid")).toBe("true");
|
|
expect(w.get("[data-slot=field-error]").text().length).toBeGreaterThan(0);
|
|
await w.get("input").setValue("a@example.com");
|
|
await settle();
|
|
form.dispatchEvent(
|
|
new Event("submit", { cancelable: true, bubbles: true }),
|
|
);
|
|
expect(submit).toHaveBeenCalledTimes(1);
|
|
w.unmount();
|
|
});
|
|
it("reports aria constraints but allows submission and supports field-level mode override", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(
|
|
Form,
|
|
{
|
|
onSubmit: (e: Event) => {
|
|
e.preventDefault();
|
|
submit();
|
|
},
|
|
},
|
|
() => [
|
|
textField({
|
|
name: "email",
|
|
type: "email",
|
|
isRequired: true,
|
|
validationBehavior: "aria",
|
|
}),
|
|
],
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await settle();
|
|
expect(w.get("input").attributes("aria-invalid")).toBe("true");
|
|
await w.get("form").trigger("submit");
|
|
expect(submit).toHaveBeenCalledTimes(1);
|
|
await w.get("input").setValue("bad-email");
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text().length).toBeGreaterThan(0);
|
|
w.unmount();
|
|
});
|
|
it("custom validation uses actual value; latest server errors replace dismissed errors", async () => {
|
|
const errors = ref<Record<string, string | string[]>>({
|
|
name: ["Taken", "Try again"],
|
|
});
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(Form, { validationErrors: errors.value }, () => [
|
|
textField({
|
|
name: "name",
|
|
validate: (v: string) => (v.length > 3 ? "Too long" : null),
|
|
}),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text()).toContain("Taken");
|
|
expect(
|
|
(w.get("input").element as HTMLInputElement).validationMessage,
|
|
).toContain("Taken");
|
|
await w.get("input").setValue("ok");
|
|
await settle();
|
|
expect(w.find("[data-slot=field-error]").exists()).toBe(false);
|
|
errors.value = { name: "New server error" };
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text()).toContain(
|
|
"New server error",
|
|
);
|
|
await w.get("input").setValue("longer");
|
|
await w.get("form").trigger("submit");
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text()).toContain("Too long");
|
|
w.unmount();
|
|
});
|
|
it("isolates nested Switch parts from outer text field and inherits Form validation mode", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { validationBehavior: "aria" }, () =>
|
|
h(TextField, {}, () => [
|
|
h(Input),
|
|
h(Switch, { name: "notify", isRequired: true }, () => [
|
|
h(SwitchContent, {}, () => [
|
|
h(SwitchControl, {}, () => h(SwitchThumb)),
|
|
h(Label, {}, () => "notify"),
|
|
h(Description, {}, () => "switch hint"),
|
|
h(FieldError),
|
|
]),
|
|
]),
|
|
]),
|
|
),
|
|
}),
|
|
);
|
|
await settle();
|
|
expect(w.get("[role=switch]").attributes("aria-describedby")).toContain(
|
|
w.get("[data-slot=description]").attributes("id"),
|
|
);
|
|
expect(
|
|
w.get("input[data-slot=input]").attributes("aria-describedby"),
|
|
).toBeUndefined();
|
|
expect(w.find("[data-slot=field-error]").exists()).toBe(true);
|
|
w.unmount();
|
|
});
|
|
});
|
|
|
|
it("labels an explicit control id and follows dynamic changes", async () => {
|
|
const id = ref("custom");
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(TextField, {}, () => [
|
|
h(Label, {}, () => "Name"),
|
|
h(Input, { id: id.value }),
|
|
]),
|
|
}),
|
|
);
|
|
await settle();
|
|
expect(w.get("label").attributes("for")).toBe("custom");
|
|
id.value = "changed";
|
|
await settle();
|
|
expect(w.get("label").attributes("for")).toBe("changed");
|
|
w.unmount();
|
|
});
|
|
it("honors formnovalidate and disabled constraints", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onSubmit: submit }, () => [
|
|
textField({ isRequired: true, isDisabled: true }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await settle();
|
|
await w.get("form").trigger("submit");
|
|
expect(submit).toHaveBeenCalledTimes(1);
|
|
w.unmount();
|
|
const skip = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { novalidate: true, onSubmit: submit }, () => [
|
|
textField({ isRequired: true }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await skip.get("form").trigger("submit");
|
|
expect(submit).toHaveBeenCalledTimes(2);
|
|
skip.unmount();
|
|
});
|
|
it("Switch consumes server errors through the shared Form context", async () => {
|
|
const errors = ref<Record<string, string>>({ accept: "Please accept" });
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(Form, { validationErrors: errors.value }, () =>
|
|
h(Switch, { name: "accept" }, () => [
|
|
h(SwitchContent, {}, () => [
|
|
h(SwitchControl, {}, () => h(SwitchThumb)),
|
|
h(Label, {}, () => "Accept"),
|
|
]),
|
|
h(FieldError),
|
|
]),
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text()).toContain("Please accept");
|
|
await w.get("[role=switch]").trigger("click");
|
|
await settle();
|
|
expect(w.find("[data-slot=field-error]").exists()).toBe(false);
|
|
errors.value = { accept: "New" };
|
|
await settle();
|
|
expect(w.get("[data-slot=field-error]").text()).toBe("New");
|
|
w.unmount();
|
|
});
|
|
it("routes root ARIA and native constraints to the control and guards capture submit", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { onSubmitCapture: submit }, () =>
|
|
textField({
|
|
minLength: 3,
|
|
pattern: "[A-Z]+",
|
|
"aria-label": "Account",
|
|
"aria-describedby": "external",
|
|
}),
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
const input = w.get("input");
|
|
expect(input.attributes("aria-label")).toBe("Account");
|
|
expect(input.attributes("aria-describedby")).toContain("external");
|
|
expect(input.attributes("minlength")).toBe("3");
|
|
expect(
|
|
w.get("[data-slot=textfield]").attributes("aria-label"),
|
|
).toBeUndefined();
|
|
await input.setValue("lowercase");
|
|
await w.get("form").trigger("submit");
|
|
expect(submit).not.toHaveBeenCalled();
|
|
w.unmount();
|
|
});
|
|
|
|
it("Form aria defaults also apply to standalone and raw native controls", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { validationBehavior: "aria", onSubmit: submit }, () => [
|
|
h(Input, { required: true, type: "email" }),
|
|
h("input", { required: true }),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await w.get("form").trigger("submit");
|
|
expect(submit).toHaveBeenCalledTimes(1);
|
|
w.unmount();
|
|
});
|
|
|
|
it("native Switch overrides an aria Form and blocks invalid submission", async () => {
|
|
const submit = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { validationBehavior: "aria", onSubmit: submit }, () =>
|
|
h(Switch, {
|
|
validationBehavior: "native",
|
|
name: "accept",
|
|
isRequired: true,
|
|
"aria-label": "Accept",
|
|
}),
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
await settle();
|
|
await w.get("form").trigger("submit");
|
|
expect(submit).not.toHaveBeenCalled();
|
|
expect(w.get("[role=switch]").attributes("aria-invalid")).toBe("true");
|
|
w.unmount();
|
|
});
|
|
|
|
it("TextField autoFocus and focus-within cover moves between child controls", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(
|
|
TextField,
|
|
{ autoFocus: true },
|
|
{
|
|
default: (state: Record<string, unknown>) => [
|
|
h(Input),
|
|
h("button", { type: "button" }, "inside"),
|
|
h("output", String(state.isFocusWithin)),
|
|
],
|
|
},
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
try {
|
|
await settle();
|
|
expect(document.activeElement).toBe(w.get("input").element);
|
|
expect(
|
|
w.get('[data-slot="textfield"]').attributes("data-focus-within"),
|
|
).toBe("true");
|
|
(w.get("button").element as HTMLButtonElement).focus();
|
|
await settle();
|
|
expect(w.get("output").text()).toBe("true");
|
|
(w.get("button").element as HTMLButtonElement).blur();
|
|
await settle();
|
|
expect(w.get("output").text()).toBe("false");
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it("field and Form errors merge in order, dismiss on edit/reset, and a new response reappears", async () => {
|
|
const local = ref(["local"]),
|
|
server = ref({ name: "server" });
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(Form, { validationErrors: server.value }, () =>
|
|
h(TextField, { name: "name", validationErrors: local.value }, () => [
|
|
h(Input),
|
|
h(
|
|
FieldError,
|
|
{},
|
|
{
|
|
default: (s: {
|
|
validationErrors: string[];
|
|
validationDetails?: ValidityState;
|
|
}) =>
|
|
`${s.validationErrors.join("|")}:${s.validationDetails?.customError}`,
|
|
},
|
|
),
|
|
]),
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
try {
|
|
await settle();
|
|
expect(w.get('[role="alert"]').text()).toBe("local|server:true");
|
|
expect((w.get("input").element as HTMLInputElement).checkValidity()).toBe(
|
|
false,
|
|
);
|
|
await w.get("input").setValue("edit");
|
|
await settle();
|
|
expect(w.find('[role="alert"]').exists()).toBe(false);
|
|
local.value = ["new local"];
|
|
server.value = { name: "new server" };
|
|
await settle();
|
|
expect(w.get('[role="alert"]').text()).toBe("new local|new server:true");
|
|
(w.get("form").element as HTMLFormElement).reset();
|
|
await settle();
|
|
expect(w.find('[role="alert"]').exists()).toBe(false);
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
it("TextField FieldError exposes native validity flags for required input", async () => {
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(TextField, { isRequired: true }, () => [
|
|
h(Input),
|
|
h(
|
|
FieldError,
|
|
{},
|
|
{
|
|
default: (s: { validationDetails?: ValidityState }) =>
|
|
String(s.validationDetails?.valueMissing),
|
|
},
|
|
),
|
|
]),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
try {
|
|
await settle();
|
|
(w.get("input").element as HTMLInputElement).checkValidity();
|
|
await settle();
|
|
expect(w.get('[role="alert"]').text()).toBe("true");
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|
|
|
|
it("TextField validity details update when native constraints change without an edit", async () => {
|
|
const pattern = ref("[0-9]+");
|
|
const w = mount(
|
|
defineComponent({
|
|
render: () =>
|
|
h(
|
|
TextField,
|
|
{ defaultValue: "123", pattern: pattern.value },
|
|
{
|
|
default: (state: { validationDetails: ValidityState }) => [
|
|
h(Input),
|
|
h("output", String(state.validationDetails.patternMismatch)),
|
|
],
|
|
},
|
|
),
|
|
}),
|
|
);
|
|
try {
|
|
await settle();
|
|
expect(w.get("output").text()).toBe("false");
|
|
pattern.value = "[a-z]+";
|
|
await settle();
|
|
expect(w.get("output").text()).toBe("true");
|
|
pattern.value = "[0-9]+";
|
|
await settle();
|
|
expect(w.get("output").text()).toBe("false");
|
|
} finally {
|
|
w.unmount();
|
|
}
|
|
});
|