У меня есть тест, который ранее был написан так, с использованием match в качестве реквизита, который был передан ему:
const initialProps = {
history: {},
match: { params: { adId: 1 } },
}
describe('AdLineManager', () => {
it('fetches an ad line on mount', () => {
const props = {
...initialProps,
getDetailedAdLineById: jest.fn(),
};
shallow(<AdLineManager {...props} />);
expect(props.getDetailedAdLineById).toBeCalledWith(initialProps.match.params.adId);
});
});
Это сработало, как и ожидалось. Когда я переключился на использование ловушки useMatchParams с jest.mock, тест теперь не удался на toBeCalledWith. Похоже, что он получает правильное значение, но я не уверен, что вызывает его сбой.
const mockHistoryPush = jest.fn();
const mockMatchParams = { adId: 1 };
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
push: mockHistoryPush,
}),
useRouteMatch: () => ({
params: mockMatchParams,
}),
useParams: () => ({
push: jest.fn(),
}),
}));
describe('AdLineManager', () => {
const getShallowWrapper = props => {
shallowRenderer.render(<AdLineManager {...props} />);
const output = shallowRenderer.getRenderOutput();
return shallow(<div>{output}</div>);
};
it('fetches an ad line on mount', () => {
const props = {
...initialProps,
getDetailedAdLineById: jest.fn(),
};
getShallowWrapper(props);
expect(props.getDetailedAdLineById).toBeCalledWith(mockMatchParams.adId);
});
});
Это ошибка, которая возвращается, когда я запускаю jest -u:
expect(jest.fn()).toBeCalledWith(expected)
Expected mock function to have been called with:
[1]
But it was not called.
82 | };
83 | getShallowWrapper(props);
> 84 | expect(props.getDetailedAdLineById).toBeCalledWith(mockMatchParams.adId);
| ^
85 | });
86 |
87 | // Can't figure out how to do this with jest.mock
В общем, у меня проблемы с использованием jest.mock и useRouteMatch. Я не уверен, в чем может быть проблема.