Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
Wait for location pathname to change in useEffect and test

I'm looking for some help with testing component. In my case in the effect I'm doing this:

  useEffect(() => {
    if (lodashHas(meetings[0], 'id')) {
      history.push(`${routes.zoomMeetings}/${meetings[0].id}`);
    }
  }, [meetings, key]);

I would like to wait until this effect applies and make testing. However, when I'm testing it with:

expect(window.location.pathname).toBe(`${routes.zoomMeetings}/${meetingsListInfo.meetings[0].id}`)

It still shows me / as a path instead of proper pathname. When I'm console logging I can see in the terminal the effect is working. It feels like the test is not waiting for this effect to evaluate before doing assertions.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm using useHistory from react-router-dom like the following:

const history = useHistory();

In my tests I'm trying to mock history.push(). I've managed to solve it by doing this in my test:

import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';

let history;

const renderComponent = () => {
    history = createMemoryHistory();

    render(<Router history={history}><MyComponent /></Router>);
}

it('tests location changes', () => {
    renderComponent();

    const pushSpy = jest.spyOn(history, 'push');

    expect(pushSpy).toHaveBeenLastCalledWith(`some-route`);
    expect(pushSpy).toHaveBeenCalledTimes(1);
});

This way I can test assertions properly.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda