Tengo el siguiente componente.
Buscando probar que el método del enrutador push dentro de useEffect se está llamando con una determinada ruta y búsqueda.
import React from 'react'; import { compose } from 'redux'; import { withRouter } from 'react-router'; const MyComponent = ({ router: { push, location }, }) => { const version = 'v2'; React.useEffect(() => { push({ pathname: `/${version}/new_path`, search: location.search ? location.search: '', }); }, []); return <div>Sample div text</div> }; const higherOrder = compose( withRouter, aCustomHigherOrderC, ); export default higherOrder(MyComponent); Esta es la prueba que he escrito para probar que el router se está llamando correctamente.
import React from 'react'; import { shallow } from 'enzyme'; import MyComponent from '../src/components/MyComponent'; jest.spyOn(React, 'useEffect').mockImplementation(f => f()); const props = { router: { push: jest.fn(), location: { search: '?param=1', }, }, }; describe('MyComponent component tests', () => { it('should work', () => { const render = () => shallow( <MyComponent {...props} /> ); expect(props.router.push).toHaveBeenCalledWith('/v2/new_path?param=1'); }); });Pero recibo el siguiente error cuando ejecuto la prueba.
Error: expect(jest.fn()).toHaveBeenCalledWith(esperado)
Se esperaba que se llamara a la función simulada con: ["/v2/new_path?param=1"] Pero no se llamó.
Parece que ni siquiera useEffect . Intenté agregar un punto de quiebre allí y no llegué a ese punto de quiebre. ¿Podría obtener algunos consejos sobre lo que estoy haciendo mal y cómo podría probar el enrutador que se llama con la ruta correcta y el parámetro de búsqueda? Gracias.