Mocking a higher order component with jest

Solution 1:

Mocking your HOC should look like this:

jest.mock('../your/HOC', () => () => 
    Component => props => <Component {...props} /> 
)

it can be read as :

jest.mock('../your/HOC', () => `

create a mock that returns your HOC function,

() => 

the function that returns your HOC aka withEntity(...args),

Component => props => <Component {...props} /> 

the HOC itself, which is a function that gets the component and return a function that get the props and return a function that returns the rendered component with its props.

Solution 2:

In my case because I am using typescript this is what works for me.

jest.mock('components/MyComponent', () => ({
  __esModule: true,
  default({children}: any) {
    return children(() => {});
  },
}));