import { describe, bench } from "vitest";
import { Field, Form } from "houseform";
import {
cleanup,
findByTestId,
fireEvent,
render,
} from "@testing-library/react";
import { Formik, FastField as FormikFastField } from "formik";
import { FieldProps } from "formik/dist/Field";
import { Controller, useForm } from "react-hook-form";
import React, { useState } from "react";
const arr = Array.from({ length: 1000 }, (_, i) => i);
function HouseFormReRenderBenchmark() {
const [count, setCount] = useState(0);
return (
Count: {count}
);
}
function FormikReRenderBenchmark() {
const [count, setCount] = useState(0);
return (
);
}
function ReactHookFormHeadlessReRenderBenchmark() {
const [count, setCount] = useState(0);
const { control } = useForm({
defaultValues: {
num: arr,
},
});
return (
);
}
function ReactHookFormReRenderBenchmark() {
const [count, setCount] = useState(0);
const { register } = useForm({
defaultValues: {
num: arr,
},
});
return (
Count: {count}
{arr.map((num, i) => (
))}
);
}
describe("Renders unrelated data in large forms", () => {
bench(
"HouseForm",
async function (this) {
const { findByTestId, findByText } = render(
);
await findByTestId("999");
await findByText("Count: 0");
fireEvent.click(await findByText("Add"));
await findByText("Count: 1");
await findByTestId("999");
},
{
setup(task) {
task.opts.beforeEach = () => {
cleanup();
};
},
}
);
bench(
"Formik",
async () => {
const { findByTestId, findByText } = render();
await findByTestId("999");
await findByText("Count: 0");
fireEvent.click(await findByText("Add"));
await findByText("Count: 1");
await findByTestId("999");
},
{
setup(task) {
task.opts.beforeEach = () => {
cleanup();
};
},
}
);
bench(
"React Hook Form",
async () => {
const { findByTestId, findByText } = render(
);
await findByTestId("999");
await findByText("Count: 0");
fireEvent.click(await findByText("Add"));
await findByText("Count: 1");
await findByTestId("999");
},
{
setup(task) {
task.opts.beforeEach = () => {
cleanup();
};
},
}
);
bench(
"React Hook Form (Headless)",
async () => {
const { findByTestId, findByText } = render(
);
await findByTestId("999");
await findByText("Count: 0");
fireEvent.click(await findByText("Add"));
await findByText("Count: 1");
await findByTestId("999");
},
{
setup(task) {
task.opts.beforeEach = () => {
cleanup();
};
},
}
);
});