feat: establish shared field and form foundation
This commit is contained in:
@@ -0,0 +1,489 @@
|
||||
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("hydrates standard field composition without ID or attribute mismatch", async () => {
|
||||
const { createSSRApp } = await import("vue");
|
||||
const { renderToString } = await import("@vue/server-renderer");
|
||||
const app = { render: () => textField({ name: "ssr", isInvalid: true }) };
|
||||
const html = await renderToString(createSSRApp(app));
|
||||
const container = document.createElement("div");
|
||||
container.innerHTML = html;
|
||||
document.body.append(container);
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {}),
|
||||
error = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const client = createSSRApp(app);
|
||||
client.mount(container);
|
||||
await settle();
|
||||
expect(warn.mock.calls.flat().join(" ")).not.toMatch(/hydration/i);
|
||||
expect(error.mock.calls.flat().join(" ")).not.toMatch(/hydration/i);
|
||||
const control = container.querySelector("input")!;
|
||||
for (const id of control.getAttribute("aria-describedby")!.split(" "))
|
||||
expect(container.querySelector(`[id="${id}"]`)).not.toBeNull();
|
||||
client.unmount();
|
||||
container.remove();
|
||||
warn.mockRestore();
|
||||
error.mockRestore();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
@@ -36,3 +36,38 @@ it("renders the initial components without browser globals", async () => {
|
||||
expect(html).toContain('role="status"');
|
||||
expect(html).toContain('data-slot="textarea"');
|
||||
});
|
||||
|
||||
it("renders complete field associations without browser globals", async () => {
|
||||
const { TextField, Label, Description, FieldError, Form } =
|
||||
await import("../packages/vue/src");
|
||||
const html = await renderToString(
|
||||
createSSRApp({
|
||||
render: () =>
|
||||
h(Form, {}, () => [
|
||||
h(
|
||||
TextField,
|
||||
{ name: "email", defaultValue: "a@example.com", isInvalid: true },
|
||||
() => [
|
||||
h(Label, {}, () => "Email"),
|
||||
h(Input),
|
||||
h(Description, {}, () => "Hint"),
|
||||
h(FieldError, {}, () => "Error"),
|
||||
],
|
||||
),
|
||||
h(TextField, { name: "notes" }, () => [
|
||||
h(Label, {}, () => "Notes"),
|
||||
h(TextArea),
|
||||
h(Description, {}, () => "Other hint"),
|
||||
]),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
const refs = [
|
||||
...html.matchAll(/(?:for|aria-describedby)="([^"]+)"/g),
|
||||
].flatMap((match) => match[1].split(" "));
|
||||
expect(refs.length).toBeGreaterThan(3);
|
||||
for (const id of refs) expect(ids).toContain(id);
|
||||
expect(html).toContain('value="a@example.com"');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user