2

Is there a way to test a component that uses react-hook-form's Controller API using Jest/enzyme?

Kindly take a look at this code sandbox to see what my component looks like

I did try mocking Controller using the below for a snapshot test;

jest.mock('react-hook-form', () => ({
  Controller: () => (<></>),
  useForm: () => ({
    control: () => ({}),
    handleSubmit: () => jest.fn(),
  }),
}))

But then the inputs are not rendered because the mocked Controller would return null (because of the <></>).

Any idea how to go about this, or must I use testing library to have a chance at writing a test for this?

Orelongz
  • 41
  • 1
  • 5
  • I have same problem too. Do you have any solution for this? – Prieyudha Akadita S Apr 06 '22 at 05:28
  • Hey @PrieyudhaAkaditaS... I didn't get how to mock the `Controller` component. I switched to the hook version - [useController](https://react-hook-form.com/api/usecontroller) - and then I was able to mock that similar to how `useForm` is mocked in the question. – Orelongz Apr 07 '22 at 07:21

0 Answers0