Jest unit testing cant manage to mock a router.push from next/router

39 Views Asked by At

the error I got

in the test i simulate a firevent and click on a button that does a router.push, the router is from import router from 'next/router'; and I've tried mocking the push function with this code:

 test('AutocompleteList click productsuggestion', async () => {
    jest.mock('next/router', () => ({
      router: {
        push: jest.fn(),
      },
    }));
    renderTest((formikProps) => (
      <MockedApolloProvider
        mocks={[
          frameworkMock({ url: '/no' }),
          autocompleteQueryMock(),
          removeRecentSearchMutationMock,
        ]}
        addTypename={false}
      >
        <Formik {...formikProps}>
          <AutocompleteList
            focusChange={() => {}}
            resetFocus={false}
            setFieldValue={() => {}}
            searchPlaceholderText="Search"
          />
        </Formik>
      </MockedApolloProvider>
    ));

    const productName = await screen.findByText('nihil');
    expect(productName).toBeInTheDocument();
    fireEvent.click(productName);
    mockAllIsIntersecting(true);
  });

normally I see others use useRouter() instead of router inside the mock function, but in my case I am using router from next/link and not useRouter, because the router.push is implemented in a helperfunction and not a react hook. Anyone have a clue what I'm doing wrong??

0

There are 0 best solutions below