import React from 'react'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { Controller } from '../controller'; import type { Control } from '../types'; import { useFieldArray } from '../useFieldArray'; import { useForm } from '../useForm'; import { FormProvider } from '../useFormContext'; import { useFormState } from '../useFormState'; import deepEqual from '../utils/deepEqual'; import noop from '../utils/noop'; describe('useFormState', () => { it('should render correct form state with isDirty, dirty, touched', () => { let count = 0; const Test = ({ control, }: { control: Control<{ test: string; }>; }) => { const { isDirty, dirtyFields, touchedFields } = useFormState({ control, }); return ( <>
{isDirty ? 'isDirty' : ''}
{dirtyFields['test'] ? 'dirty field' : ''}
{touchedFields['test'] ? 'isTouched' : ''}
); }; const Component = () => { const { register, control } = useForm<{ test: string; }>(); count++; return (
); }; render(); fireEvent.input(screen.getByLabelText('test'), { target: { value: 'test', }, }); expect(screen.getByText('isDirty')).toBeVisible(); expect(screen.getByText('dirty field')).toBeVisible(); expect(count).toEqual(2); fireEvent.blur(screen.getByLabelText('test')); expect(screen.getByText('isTouched')).toBeVisible(); expect(count).toEqual(2); }); it('should render correct isolated errors message', async () => { let count = 0; const Test = ({ control }: { control: Control }) => { const { errors, isValid } = useFormState({ control, }); return ( <>
{errors['test'] ? 'error' : 'valid'}
{isValid ? 'yes' : 'no'}
); }; const Component = () => { const { register, control } = useForm({ mode: 'onChange', }); count++; return (
); }; render(); await waitFor(() => expect(screen.getByText('yes')).toBeVisible()); fireEvent.input(screen.getByLabelText('test'), { target: { value: 'test', }, }); expect(await screen.findByText('error')).toBeVisible(); expect(screen.getByText('no')).toBeVisible(); fireEvent.input(screen.getByLabelText('test'), { target: { value: 'testtest', }, }); expect(await screen.findByText('valid')).toBeVisible(); expect(screen.getByText('yes')).toBeVisible(); expect(count).toEqual(2); }); it('should update isValidating correctly', async () => { function Child() { const { isDirty, isValid, isValidating } = useFormState(); const enabled = !isValidating && isDirty && isValid; return ( ); } function App() { const formFunctions = useForm({ mode: 'onChange', }); const { register } = formFunctions; return (
); } render(); fireEvent.change(screen.getByRole('textbox'), { target: { value: '1', }, }); await waitFor(() => { expect(screen.getByRole('button')).not.toBeDisabled(); }); fireEvent.change(screen.getByRole('textbox'), { target: { value: '12', }, }); await waitFor(() => { expect(screen.getByRole('button')).not.toBeDisabled(); }); }); it('should update formState separately with useFormState', async () => { let count = 0; let testCount = 0; let test1Count = 0; const Test1 = ({ control }: { control: Control }) => { const { isDirty, dirtyFields } = useFormState({ control, }); testCount++; return ( <>
{dirtyFields['test'] ? 'hasDirtyField' : 'notHasDirtyField'}
{isDirty ? 'isDirty' : 'notDirty'}
); }; const Test = ({ control }: { control: Control }) => { const { touchedFields } = useFormState({ control, }); test1Count++; return ( <>
{touchedFields['test'] ? 'isTouched' : 'notTouched'}
); }; const Component = () => { const { register, control } = useForm({ mode: 'onChange', }); count++; return (
); }; render(); fireEvent.input(screen.getByLabelText('test'), { target: { value: 'test', }, }); expect(await screen.findByText('hasDirtyField')).toBeVisible(); expect(screen.getByText('isDirty')).toBeVisible(); expect(count).toEqual(2); expect(testCount).toEqual(3); expect(test1Count).toEqual(2); fireEvent.blur(screen.getByLabelText('test')); expect(screen.getByText('isTouched')).toBeVisible(); expect(count).toEqual(2); expect(testCount).toEqual(3); expect(test1Count).toEqual(3); fireEvent.input(screen.getByLabelText('test'), { target: { value: '', }, }); expect(count).toEqual(2); expect(testCount).toEqual(3); expect(test1Count).toEqual(3); }); it('should render correct submit state', async () => { let count = 0; const Test = ({ control }: { control: Control }) => { const { isSubmitted, submitCount } = useFormState({ control, }); return ( <>
{isSubmitted ? 'isSubmitted' : ''}
{submitCount}
); }; const Component = () => { const { control, handleSubmit } = useForm(); count++; return (
); }; render(); fireEvent.click(screen.getByRole('button')); expect(await screen.findByText('isSubmitted')).toBeVisible(); expect(screen.getByText('1')).toBeVisible(); expect(count).toEqual(2); }); it('should only re-render when subscribed field name updated', async () => { let count = 0; type FormValues = { firstName: string; lastName: string; }; const Test = ({ control }: { control: Control }) => { const { errors } = useFormState({ control, name: 'firstName', }); count++; return <>{errors?.firstName?.message}; }; const Component = () => { const { control, register } = useForm({ mode: 'onChange', defaultValues: { firstName: 'a', lastName: 'b', }, }); return (
); }; render(); fireEvent.change(screen.getByPlaceholderText('firstName'), { target: { value: '', }, }); await waitFor(() => expect(count).toEqual(2)); }); it('should not re-render when subscribed field name is not included', async () => { let count = 0; type FormValues = { firstName: string; lastName: string; }; const Test = ({ control }: { control: Control }) => { const { errors } = useFormState({ control, name: 'lastName', }); count++; return <>{errors?.lastName?.message}; }; const Component = () => { const { control, register } = useForm({ mode: 'onChange', defaultValues: { firstName: 'a', lastName: 'b', }, }); return (
); }; render(); fireEvent.change(screen.getByPlaceholderText('firstName'), { target: { value: '', }, }); expect(count).toEqual(2); }); it('should only re-render when subscribed field names updated', async () => { let count = 0; type FormValues = { firstName: string; lastName: string; age: number; }; const Test = ({ control }: { control: Control }) => { const { errors } = useFormState({ control, name: ['firstName', 'lastName'], }); count++; return <>{errors?.firstName?.message}; }; const Component = () => { const { control, register } = useForm({ mode: 'onChange', defaultValues: { firstName: 'a', lastName: 'b', }, }); return (
); }; render(); fireEvent.change(screen.getByPlaceholderText('firstName'), { target: { value: '', }, }); fireEvent.change(screen.getByPlaceholderText('lastName'), { target: { value: '', }, }); await waitFor(() => expect(count).toEqual(2)); }); it('should only re-render when subscribed field names updated', async () => { let count = 0; type FormValues = { firstName: string; lastName: string; age: number; }; const Test = ({ control }: { control: Control }) => { const { errors } = useFormState({ control, name: ['age', 'lastName'], }); count++; return <>{errors?.firstName?.message}; }; const Component = () => { const { control, register } = useForm({ mode: 'onChange', defaultValues: { firstName: 'a', lastName: 'b', }, }); return (
); }; render(); fireEvent.change(screen.getByPlaceholderText('firstName'), { target: { value: '', }, }); expect(count).toEqual(2); }); it('should be able to stop the formState subscription', async () => { type FormValues = { test: string; }; function Child({ control }: { control: Control }) { const [disabled, setDisabled] = React.useState(true); const { errors } = useFormState({ control, name: 'test', disabled, }); return (
{errors.test &&

error

}
); } const App = () => { const { trigger, register, control } = useForm(); return (
); }; render(); fireEvent.click(screen.getByRole('button', { name: 'trigger' })); expect(screen.queryByText('error')).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'toggle' })); fireEvent.click(screen.getByRole('button', { name: 'trigger' })); expect(await screen.findByText('error')).toBeVisible(); }); it('should not start early subscription and throw warning at strict mode', async () => { type FormValues = { test: { data: string }[] }; function FieldArray() { const { reset, control } = useForm({ defaultValues: { test: [] }, }); const { fields, append } = useFieldArray({ control, name: 'test' }); return (
{fields.map((field, index) => (
} />
))}
); } const App = () => { return ( ); }; render(); fireEvent.click(screen.getByRole('button', { name: 'add' })); fireEvent.click(screen.getByRole('button', { name: 'reset' })); fireEvent.click(screen.getByRole('button', { name: 'add' })); expect(await screen.findAllByRole('textbox')).toHaveLength(1); }); it('should subscribe to exact form state update', () => { const App = () => { const { control, register } = useForm(); const [exact, setExact] = React.useState(true); const { touchedFields } = useFormState({ name: 'test', control, exact, }); return (

{touchedFields.testData && 'touched'}

); }; render(); fireEvent.focus(screen.getByRole('textbox')); fireEvent.blur(screen.getByRole('textbox')); expect(screen.queryByText('touched')).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button')); fireEvent.focus(screen.getByRole('textbox')); fireEvent.blur(screen.getByRole('textbox')); expect(screen.getByText('touched')).toBeVisible(); }); it('should be able to access defaultValues', () => { type FormValues = { firstName: string; lastName: string; }; const defaultValues = { firstName: 'a', lastName: 'b', }; const Test = ({ control }: { control: Control }) => { const formState = useFormState({ control, }); return (

{deepEqual(formState.defaultValues, defaultValues) ? 'yes' : 'no'}

); }; const Component = () => { const { control } = useForm({ defaultValues, }); return ; }; render(); expect(screen.getByText('yes')).toBeVisible(); }); it('should conditionally update formState after mount', async () => { function DirtyState() { const { isDirty, isValid } = useFormState(); return (

{isDirty ? 'dirty' : 'pristine'}

{isValid ? 'valid' : 'error'}

); } function App() { const [showDirty, toggleShowDirty] = React.useReducer( (prev) => !prev, false, ); const formMethods = useForm({ defaultValues: { firstname: '', }, }); return ( {showDirty && } ); } render(); fireEvent.click(screen.getByRole('button')); waitFor(() => screen.getByText('Required')); fireEvent.change(screen.getByRole('textbox'), { target: { value: 'data' }, }); waitFor(() => expect(screen.queryByText('Required')).not.toBeInTheDocument(), ); }); it('should return the latest values with async values', async () => { type FormValues = { firstName: string; }; function Input({ control }: { control: Control }) { const { isValid } = useFormState({ control }); return

{isValid}

; } function Form({ values }: { values: FormValues }) { const { getValues, control } = useForm({ defaultValues: { firstName: '', }, values, resetOptions: { keepDefaultValues: true, }, }); return ( <>

{getValues().firstName}

); } function App() { return
; } render(); await waitFor(() => { screen.getByText('test'); }); }); it('should update form state with disabled state', async () => { function Form({ control }: { control: Control }) { const { disabled } = useFormState({ control, }); return

{disabled ? 'disabled' : ''}

; } function App() { const { control } = useForm({ disabled: true, }); return ; } render(); await waitFor(() => { screen.getByText('disabled'); }); }); });