0

Below is the code for HOC

const HOC = ({ component: Component }) => {
  return () => {
     const id = useQuery(query);
     return (<div> 
         {!id ? ( <SomeOtherComponent prop1={'hello'} prop2={'world'} /> ) : ( <Component /> )}
     </div>)
  }
}

Below is the test to render HOC-

const myComponent = () => <div data-testid={'component-testid'}>ABC</div>;
    const renderHOC = HOC({component: myComponent})();
    const {getByTestId} = render(renderHOC);
    expect(getByTestId('component-testid')).toBeInTheDocument();

Getting the error- Invalid hook call. React hooks must be called inside react functional component.

3
  • Right now there are no hooks in your code. What is Component and SomeOtherComponent respectively? Are they the same? Commented Apr 13, 2020 at 10:10
  • useQuery() is the hook. These are two separate component.The HOC is working perfectly on UI , however i am unable to write the test for same. Commented Apr 13, 2020 at 10:11
  • Ah! Then you should be able to fix this by moving const id = useQuery(query); out of the return () => {...} statement. Commented Apr 13, 2020 at 10:15

1 Answer 1

1

It should be as follows

const RenderHOC = HOC({component: myComponent});
const { getByTestId } = render(<RenderHOC />);

You cant call a component as a function if it contains hooks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.