({
defaultValues: {
names: [],
},
});
const { fields, append } = useFieldArray({
control,
name: 'names',
});
const handleAddElement = () => {
append({ name: 'test' });
};
output.push(watch());
return (
);
};
render();
expect(output.at(-1)).toEqual({
names: [],
});
const appendButton = screen.getByRole('button');
fireEvent.click(appendButton);
fireEvent.click(appendButton);
fireEvent.change(screen.getAllByRole('textbox')[0], {
target: { value: '123' },
});
expect(output.at(-1)).toEqual({
names: [
{
name: '123',
},
{
name: 'test',
},
],
});
fireEvent.change(screen.getAllByRole('textbox')[1], {
target: { value: '456' },
});
// Let's check all values of renders with implicitly the number of render (for each value)
expect(output).toMatchSnapshot();
});
it('should have dirty marked when watch is enabled', async () => {
function Component() {
const {
register,
formState: { isDirty },
watch,
} = useForm<{
lastName: string;
}>({
defaultValues: { lastName: '' },
});
watch('lastName');
return (
);
}
render();
expect(screen.getByText('False')).toBeVisible();
fireEvent.change(screen.getByRole('textbox'), {
target: { value: 'test' },
});
expect(screen.getByText('True')).toBeVisible();
fireEvent.change(screen.getByRole('textbox'), {
target: { value: '' },
});
expect(await screen.findByText('False')).toBeVisible();
});
it('should return deeply nested field values with defaultValues', async () => {
let data;
function App() {
const { register, watch } = useForm<{
test: {
firstName: string;
lastName: string;
};
}>({
defaultValues: {
test: { lastName: '', firstName: '' },
},
});
data = watch();
return (
);
}
render();
fireEvent.change(screen.getByRole('textbox'), {
target: {
value: '1234',
},
});
expect(data).toEqual({
test: {
firstName: '',
lastName: '1234',
},
});
});
it('should remove input value after input is unmounted with shouldUnregister: true', () => {
const watched: unknown[] = [];
const App = () => {
const [show, setShow] = React.useState(true);
const { watch, register } = useForm({
shouldUnregister: true,
});
watched.push(watch());
return (
{show && }
);
};
render();
expect(watched).toEqual([{}, { test: '' }]);
fireEvent.change(screen.getByRole('textbox'), {
target: {
value: '1',
},
});
expect(watched).toEqual([
{},
{
test: '',
},
{
test: '1',
},
]);
fireEvent.click(screen.getByRole('button'));
expect(watched).toEqual([
{},
{
test: '',
},
{
test: '1',
},
{
test: '1',
},
{},
]);
});
it('should flush additional render for shouldUnregister: true', async () => {
const watchedData: unknown[] = [];
const App = () => {
const { watch, reset, register } = useForm({
shouldUnregister: true,
});
React.useEffect(() => {
reset({
test: '1234',
data: '1234',
});
}, [reset]);
const result = watch();
watchedData.push(result);
return (
{result.test &&
{result.test}
}
);
};
render();
expect(await screen.findByText('1234')).toBeVisible();
expect(watchedData).toEqual([
{},
{},
{
test: '1234',
},
]);
});
it('should not be able to overwrite global watch state', () => {
function Watcher({
control,
}: {
control: Control;
}) {
useWatch({
control,
});
return null;
}
function App() {
const { register, watch, control } = useForm({
defaultValues: {
firstName: '',
},
});
const { firstName } = watch();
return (
);
}
render();
fireEvent.change(screen.getByRole('textbox'), {
target: {
value: 'bill',
},
});
screen.getByText('bill');
});
it('should call the callback on every append', () => {
interface FormValues {
names: {
firstName: string;
}[];
}
const mockedFn = jest.fn();
function App() {
const { watch, control } = useForm({
defaultValues: { names: [] },
});
const { fields, append } = useFieldArray({
control,
name: 'names',
});
useEffect(() => {
const subscription = watch((_value, { name }) => {
mockedFn(name, _value);
});
return () => {
subscription.unsubscribe();
};
}, [watch]);
const addItem = (index: number) => {
append({ firstName: '' }, { focusName: `names.${index}.firstName` });
};
return (
);
}
render();
fireEvent.click(screen.getByRole('button'));
expect(mockedFn).toHaveBeenCalledTimes(1);
fireEvent.click(screen.getByRole('button'));
expect(mockedFn).toHaveBeenCalledTimes(2);
});
it('should remain isReady form state for subscription', () => {
function App() {
const {
watch,
formState: { isReady },
register,
control,
} = useForm({
defaultValues: { name: '' },
});
const { isReady: isFormStateReady } = useFormState({
control,
});
watch();
return (
);
}
render();
fireEvent.change(screen.getByRole('textbox'), {
target: {
value: 'test',
},
});
screen.getByText('formStateReady');
screen.getByText('useFormStateReady');
});
});