import React from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import { useController } from '../../useController'; import { useFieldArray } from '../../useFieldArray'; import { useForm } from '../../useForm'; import { FormProvider } from '../../useFormContext'; interface TestValue { x: string; } interface DefaultValues { test: TestValue[]; } let i = 0; jest.mock('../../logic/generateId', () => () => String(i++)); describe('replace', () => { beforeEach(() => { i = 0; }); it('should replace fields correctly', () => { let currentFields: any = []; const defaultValues: DefaultValues = { test: [{ x: '101' }, { x: '102' }, { x: '103' }], }; const labelSingle = 'replace'; const labelBatch = 'replaceBatch'; const Component = () => { const { register, control } = useForm({ defaultValues, }); const { fields, replace } = useFieldArray({ control, name: 'test', }); currentFields = fields; const handleSingleReplace = () => replace({ x: '201' }); const handleBatchReplace = () => replace([{ x: '301' }, { x: '302' }]); return (
{fields.map((field, index) => { return ( ); })}
); }; render(); fireEvent.click(screen.getByRole('button', { name: labelSingle })); expect(currentFields).toEqual([{ id: '6', x: '201' }]); fireEvent.click(screen.getByRole('button', { name: labelBatch })); expect(currentFields).toEqual([ { id: '8', x: '301' }, { id: '9', x: '302' }, ]); }); it('should not omit keyName when provided', async () => { type FormValues = { test: { test: string; id: string; }[]; }; const App = () => { const [data, setData] = React.useState(); const { control, register, handleSubmit } = useForm({ defaultValues: { test: [ { id: '1234', test: 'data' }, { id: '4567', test: 'data1' }, ], }, }); const { fields, replace } = useFieldArray({ control, name: 'test', }); return (
{fields.map((field, index) => { return ; })}

{JSON.stringify(data)}

); }; render(); fireEvent.click(screen.getByRole('button', { name: 'replace' })); fireEvent.click(screen.getByRole('button', { name: 'submit' })); expect( await screen.findByText('{"test":[{"id":"test","test":"data"}]}'), ).toBeVisible(); }); it('should not omit keyName when provided and defaultValue is empty', async () => { type FormValues = { test: { test: string; id: string; }[]; }; let k = 0; const App = () => { const [data, setData] = React.useState(); const { control, register, handleSubmit } = useForm(); const { fields, append, replace } = useFieldArray({ control, name: 'test', }); return (
{fields.map((field, index) => { return ; })}

{JSON.stringify(data)}

); }; render(); fireEvent.click(screen.getByRole('button', { name: 'append' })); fireEvent.click(screen.getByRole('button', { name: 'append' })); fireEvent.click(screen.getByRole('button', { name: 'replace' })); fireEvent.click(screen.getByRole('button', { name: 'submit' })); expect( await screen.findByText('{"test":[{"id":"whatever","test":"data"}]}'), ).toBeVisible(); }); it('should not replace errors state', async () => { const App = () => { const { control, register, trigger, formState: { errors }, } = useForm({ defaultValues: { test: [ { firstName: '', }, ], }, }); const { fields, replace } = useFieldArray({ name: 'test', control, }); React.useEffect(() => { trigger(); }, [trigger]); return (
{fields.map((field, i) => ( ))}

{errors?.test?.[0]?.firstName?.message}

); }; render(); expect(await screen.findByText('This is required')).toBeVisible(); fireEvent.click(screen.getByRole('button')); expect(await screen.findByText('This is required')).toBeVisible(); }); it('should not affect other formState during replace action', () => { const ControlledInput = ({ index }: { index: number }) => { const { field } = useController({ name: `fieldArray.${index}.firstName`, }); return ; }; const defaultValue = { firstName: 'test', }; const FieldArray = () => { const { fields, replace } = useFieldArray({ name: 'fieldArray', }); React.useEffect(() => { replace([defaultValue]); }, [replace]); return (
{fields.map((field, index) => { return ; })}
); }; function App() { const form = useForm({ mode: 'onChange', }); const [, updateState] = React.useState(0); return (

{JSON.stringify(form.formState.touchedFields)}

); } render(); fireEvent.focus(screen.getByRole('textbox')); fireEvent.blur(screen.getByRole('textbox')); fireEvent.click(screen.getByRole('button', { name: 'replace' })); fireEvent.click(screen.getByRole('button', { name: 'updateState' })); expect( screen.getByText('{"fieldArray":[{"firstName":true}]}'), ).toBeVisible(); }); });