score:3

Accepted answer

After reading https://dev.to/theactualgivens/testing-react-hook-state-changes-2oga I decided to mock the useReducer as the dispatches cause state updates in the useEffect hook. After that it worked. I now mock the reducer initial state.

const dispatch = jest.fn();
const useReducerSpy = jest.spyOn(React, 'useReducer');
useReducerSpy.mockImplementation((init: any) => [MY_CONTEXT_MOCK, dispatch]);

and my test looks like

it('should render', async () => {
  const { result } = renderHook(useMyContext, { wrapper: bySlugWrapper });
  expect(result.current.somevar).toEqual(MY_CONTEXT.somevar);
});

Related Query

More Query from same tag