import * as React from "react";
import {
render,
fireEvent,
type,
wait,
blur,
click,
press,
focus,
} from "reakit-test-utils";
import {
unstable_Form as Form,
unstable_FormCheckbox as FormCheckbox,
unstable_FormGroup as FormGroup,
unstable_FormInput as FormInput,
unstable_FormLabel as FormLabel,
unstable_FormMessage as FormMessage,
unstable_FormRadio as FormRadio,
unstable_FormSubmitButton as FormSubmitButton,
unstable_FormRemoveButton as FormRemoveButton,
unstable_FormPushButton as FormPushButton,
unstable_useFormState as useFormState,
unstable_FormRadioGroup as FormRadioGroup,
unstable_useFormInput as useFormInput,
unstable_useFormPushButton as useFormPushButton,
unstable_useFormRadio as useFormRadio,
unstable_useFormRadioGroup as useFormRadioGroup,
unstable_useFormRemoveButton as useFormRemoveButton,
unstable_FormStateReturn as FormStateReturn,
} from "..";
test("validate on change", async () => {
const onValidate = jest.fn();
const Test = () => {
const form = useFormState({ onValidate });
return (
);
};
const { getByLabelText } = render( );
const input = getByLabelText("input");
expect(onValidate).not.toHaveBeenCalled();
type("a", input);
await wait(() => expect(onValidate).toHaveBeenCalledWith({ input: "a" }));
});
test("don't validate on change if validateOnChange is false", async () => {
const onValidate = jest.fn();
const Test = () => {
const form = useFormState({ onValidate, validateOnChange: false });
return (
);
};
const { getByLabelText } = render( );
const input = getByLabelText("input");
expect(onValidate).not.toHaveBeenCalled();
type("a", input);
await wait(expect(onValidate).not.toHaveBeenCalled);
});
test("validate on blur", async () => {
const onValidate = jest.fn();
const Test = () => {
const form = useFormState({ onValidate });
return (
);
};
const { getByLabelText } = render( );
const input = getByLabelText("input");
expect(onValidate).not.toHaveBeenCalled();
focus(input);
blur();
await wait(() => expect(onValidate).toHaveBeenCalledWith({}));
});
test("don't validate on blur if validateOnBlur is false", async () => {
const onValidate = jest.fn();
const Test = () => {
const form = useFormState({ onValidate, validateOnBlur: false });
return (
);
};
const { getByLabelText } = render( );
const input = getByLabelText("input");
expect(onValidate).not.toHaveBeenCalled();
focus(input);
blur();
await wait(expect(onValidate).not.toHaveBeenCalled);
});
test("display validation error", async () => {
const Test = () => {
const form = useFormState({
values: {
input: "",
},
onValidate: (values) => {
if (!values.input) {
const error = { input: "required" };
return Promise.reject(error);
}
return undefined;
},
});
return (
);
};
const { getByLabelText, getByTestId } = render( );
const input = getByLabelText("input");
const error = getByTestId("error");
expect(error).toBeEmptyDOMElement();
focus(input);
blur();
await wait(() => expect(error).toHaveTextContent("required"));
});
test("display validation message", async () => {
const Test = () => {
const form = useFormState({
values: {
input: "",
},
onValidate: (values) => {
if (values.input) {
return { input: "nice" };
}
return {};
},
});
return (
);
};
const { getByLabelText, getByTestId } = render( );
const input = getByLabelText("input");
const message = getByTestId("message");
expect(message).toBeEmptyDOMElement();
type("a", input);
expect(message).toBeEmptyDOMElement();
focus(input);
blur();
await wait(() => expect(message).toHaveTextContent("nice"));
});
test("display submission error", async () => {
const Test = () => {
const form = useFormState({
values: {
a: {
b: {
c: ["d", "e", "f"],
},
},
},
onSubmit: (values) => {
if (values.a.b.c[1] === "e") {
const error = {
a: {
b: {
c: [null, "error"],
},
},
};
throw error;
}
},
});
return (
);
};
const { getByRole, getByTestId } = render( );
const form = getByRole("form");
const error = getByTestId("error");
expect(error).toBeEmptyDOMElement();
fireEvent.submit(form);
await wait(() => expect(error).toHaveTextContent("error"));
});
test("display submission message", async () => {
const Test = () => {
const form = useFormState({
values: {
a: {
b: {
c: ["d", "e", "f"],
},
},
},
onSubmit: (values) => {
if (values.a.b.c[1] === "e") {
return {
a: {
b: {
c: [null, "nice"],
},
},
};
}
return {};
},
});
return (
);
};
const { getByRole, getByTestId } = render( );
const form = getByRole("form");
const message = getByTestId("message");
expect(message).toBeEmptyDOMElement();
fireEvent.submit(form);
await wait(() => expect(message).toHaveTextContent("nice"));
});
test("display validation message on submit", async () => {
const Test = () => {
const form = useFormState({
values: {
a: {
b: {
c: ["d", "e", "f"],
},
},
},
onValidate: (values) => {
if (values.a.b.c[1] === "e") {
return {
a: {
b: {
c: [null, "nice"],
},
},
};
}
return {};
},
});
return (
);
};
const { getByRole, getByTestId } = render( );
const form = getByRole("form");
const message = getByTestId("message");
expect(message).toBeEmptyDOMElement();
fireEvent.submit(form);
await wait(() => expect(message).toHaveTextContent("nice"));
});
test("display validation error on submit", async () => {
const Test = () => {
const form = useFormState({
values: {
a: {
b: {
c: ["d", "e", "f"],
},
},
},
onValidate: (values) => {
if (values.a.b.c[1] === "e") {
const error = {
a: {
b: {
c: [null, "error"],
},
},
};
throw error;
}
},
});
return (
);
};
const { getByRole, getByTestId } = render( );
const form = getByRole("form");
const error = getByTestId("error");
expect(error).toBeEmptyDOMElement();
fireEvent.submit(form);
await wait(() => expect(error).toHaveTextContent("error"));
});
test("display group error", async () => {
const Test = () => {
const form = useFormState({
values: {
input: ["a"] as Array<"a" | "b" | "c">,
},
onValidate: (values) => {
if (values.input.length <= 1) {
const error = { input: "error" };
throw error;
}
},
});
return (
);
};
const { getByRole, getByTestId, getByLabelText } = render( );
const form = getByRole("form");
const error = getByTestId("error");
const a = getByLabelText("a");
const b = getByLabelText("b");
expect(error).toBeEmptyDOMElement();
fireEvent.submit(form);
await wait(() => expect(error).toHaveTextContent("error"));
click(b);
await wait(expect(error).toBeEmptyDOMElement);
click(a);
await wait(() => expect(error).toHaveTextContent("error"));
});
test("focus the first invalid input on failed submit", async () => {
const Test = () => {
const form = useFormState({
values: {
input1: "",
input2: "",
input3: "",
},
onValidate: (values) => {
if (!values.input2) {
const error = { input2: "error" };
throw error;
}
},
});
return (
);
};
const { getByLabelText, getByTestId } = render( );
const input2 = getByLabelText("input2");
const submit = getByTestId("submit");
expect(input2).not.toHaveFocus();
click(submit);
await wait(expect(input2).toHaveFocus);
});
test("focus the first invalid fieldset on failed submit", async () => {
const Test = () => {
const form = useFormState({
values: {
input1: "",
choice1: "",
choice2: "",
},
onValidate: () => {
const error = { choice1: "error", choice2: "error" };
throw error;
},
});
return (
);
};
const { getByLabelText, getByTestId } = render( );
const choice1 = getByLabelText("choice1");
const submit = getByTestId("submit");
expect(choice1).not.toHaveFocus();
click(submit);
await wait(expect(choice1).toHaveFocus);
});
test("arrow keys control radio buttons", async () => {
const Test = () => {
const form = useFormState({
values: {
input: "a",
},
});
return (
);
};
const { getByLabelText } = render( );
const a = getByLabelText("a") as HTMLInputElement;
const b = getByLabelText("b") as HTMLInputElement;
const c = getByLabelText("c") as HTMLInputElement;
focus(b);
expect(b).toHaveFocus();
expect(b.checked).toBe(false);
press.ArrowDown();
expect(c).toHaveFocus();
expect(c.checked).toBe(true);
press.ArrowDown();
expect(a).toHaveFocus();
expect(a.checked).toBe(true);
press.ArrowUp();
expect(c).toHaveFocus();
expect(c.checked).toBe(true);
press.ArrowLeft();
expect(b).toHaveFocus();
expect(b.checked).toBe(true);
press.ArrowRight();
expect(c).toHaveFocus();
expect(c.checked).toBe(true);
});
test("push/remove button adds/removes entry and moves focus", async () => {
const Test = () => {
const form = useFormState({
baseId: "form",
values: {
people: [] as Array<{ name: string; email: string }>,
},
});
return (
);
};
const { getByTestId, getByPlaceholderText, container } = render( );
const push = getByTestId("push");
expect(container).toMatchInlineSnapshot(`
`);
click(push);
expect(container).toMatchInlineSnapshot(`
`);
await wait(expect(getByPlaceholderText("name0")).toHaveFocus);
click(push);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(push);
await wait(expect(getByPlaceholderText("name2")).toHaveFocus);
const remove0 = getByTestId("remove0");
const remove1 = getByTestId("remove1");
const remove2 = getByTestId("remove2");
click(remove0);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(remove2);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(remove1);
await wait(expect(push).toHaveFocus);
});
test("useFormInput passing name as htmlProps", async () => {
const onValidate = jest.fn();
const Test = () => {
const form = useFormState({ onValidate });
// @ts-ignore
const input = useFormInput(form, { name: "input" });
return (
);
};
const { getByLabelText } = render( );
const input = getByLabelText("input");
expect(onValidate).not.toHaveBeenCalled();
type("a", input);
await wait(() => expect(onValidate).toHaveBeenCalledWith({ input: "a" }));
});
test("useFormPushButton and useFormRemoveButton passing name and value as htmlProps", async () => {
type Values = {
people: Array<{ name: string; email: string }>;
};
const Fragment = ({
i,
...props
}: FormStateReturn & { i: number }) => {
const removeButton = useFormRemoveButton(
// @ts-ignore
{ index: i, ...props },
{ name: "people", "data-testid": `remove${i}` }
);
return (
<>
>
);
};
const Test = () => {
const form = useFormState({
baseId: "form",
values: {
people: [],
},
});
// @ts-ignore
const pushButton = useFormPushButton(form, {
name: "people",
value: { name: "", email: "" },
"data-testid": "push",
});
return (
);
};
const { getByTestId, getByPlaceholderText, container } = render( );
const push = getByTestId("push");
expect(container).toMatchInlineSnapshot(`
`);
click(push);
expect(container).toMatchInlineSnapshot(`
`);
await wait(expect(getByPlaceholderText("name0")).toHaveFocus);
click(push);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(push);
await wait(expect(getByPlaceholderText("name2")).toHaveFocus);
const remove0 = getByTestId("remove0");
const remove1 = getByTestId("remove1");
const remove2 = getByTestId("remove2");
click(remove0);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(remove2);
await wait(expect(getByPlaceholderText("name1")).toHaveFocus);
click(remove1);
await wait(expect(push).toHaveFocus);
});
test("useFormRadio and useFormRadioGroup passing name and value as htmlProps", async () => {
type Values = {
input: string;
};
const CustomFormRadio = ({
value,
...props
}: FormStateReturn & { value: string }) => {
// @ts-ignore
const formRadio = useFormRadio(props, { name: "input", value });
return (
{value}
);
};
const Test = () => {
const form = useFormState({
baseId: "form",
values: {
input: "a",
},
});
// @ts-ignore
const { wrapElement, ...radioGroup } = useFormRadioGroup(form, {
name: "input",
});
const children = (
);
return (
);
};
const { container } = render( );
expect(container).toMatchInlineSnapshot(`
`);
});
test("reset form after removing an item", async () => {
const Test = () => {
const form = useFormState({
baseId: "form",
values: {
names: ["a", "b", "c"],
},
});
return (
);
};
const { getByText, container } = render( );
const remove1 = getByText("remove1");
const reset = getByText("reset");
click(remove1);
click(reset);
expect(container).toMatchInlineSnapshot(`
`);
});