I am writing a form component using react-final-form library. As component starts growing, I split it into multiple sub components such that parent component has the <Form>{... }</Form> component and related <Field>{... }</Field> are in separate components. Now, I'm trying to write unit test for my separated sub components containing <Field></Field>, I'm getting the error as <Field>{... }</Field> have dependency on <Form>{... }</Form> and cannot run separately. Below is the exact error message which shows up when I try to run unit test.
useField must be used inside of a component
One way I can solve this is to mock my actual <Field>{... }</Field> component in its unit test, but I feel that won't be a correct approach as for unit test I'm mocking the actual component which needs to get tested.
Could someone please help me what way should I go for ? Is it the right way to mock the component in its own unit test and then test it or should I have to think of restructuring my parent <Form> and its sub components ? And how can I restructure it better, any ideas?
Fieldcomponent uses useField hook,useFieldhook uses useForm underly.useFormuses React.useContext(ReactFinalFormContext) underly. As you can see, it usesReactFinalFormContext, so the component which uses this react context must be a descendant of ReactFinalFormContext.Provider.ReactFinalFormContext.Provideris provided byFormcomponent.I recommend you to use React Testing Library to test your react components.
It's not recommended to mock the component, you should test your components
For more, see Why should I use React Testing Library
You should wrap your
Fieldcustom component with aFormcomponent. See official test recipesE.g.