Как смоделировать history.push с помощью новых React Router Hooks с помощью Jest - PullRequest
0 голосов
/ 23 октября 2019

Я пытаюсь смоделировать history.push внутри нового useHistory хука на react-router и использую @testing-library/react. Я просто издевался над модулем, как первый ответ здесь: Как протестировать компоненты, используя новые обработчики реакции маршрутизатора?

Итак, я делаю:

//NotFound.js
import * as React from 'react';
import { useHistory } from 'react-router-dom';


const RouteNotFound = () => {
  const history = useHistory();
  return (
    <div>
      <button onClick={() => history.push('/help')} />
    </div>
  );
};

export default RouteNotFound;
//NotFound.test.js
describe('RouteNotFound', () => {
  it('Redirects to correct URL on click', () => {
    const mockHistoryPush = jest.fn();

    jest.mock('react-router-dom', () => ({
      ...jest.requireActual('react-router-dom'),
      useHistory: () => ({
        push: mockHistoryPush,
      }),
    }));

    const { getByRole } = render(
        <MemoryRouter>
          <RouteNotFound />
        </MemoryRouter>
    );

    fireEvent.click(getByRole('button'));
    expect(mockHistoryPush).toHaveBeenCalledWith('/help');
  });
})

Но mockHistoryPush не называется ... Что я делаю не так?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...