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

95
Vistas
React testing library fire beforeinstallprompt event

I have a React component that listen to beforeinstallprompt event to handle PWA app installation. It has an effect hook like this:

useEffect(() => {
    window.addEventListener('beforeinstallprompt', handleBeforeInstallPromptEvent);

    return () => {
      window.removeEventListener('beforeinstallprompt', handleBeforeInstallPromptEvent);
    };
  }, [handleBeforeInstallPromptEvent]);

The handler handleBeforeInstallPromptEvent do some logic to show a banner asking the user to install the application if it has not already been installed.

To test this behaviour I created this jest test but it is not calling the event handler and consequently not showing the alert.

Am I missing something?

it('should render install app alert if not yet installed', async () => {
    const event = createEvent('beforeinstallprompt', window, {
      userChoice: new Promise((res) => res({ outcome: 'accepted', platform: '' })),
      prompt: () => new Promise((res) => res(undefined)),
    });

    render(<InstallApp />);

    await act(async () => {
      fireEvent(window, event);
    });

    expect(screen.getByText(/Install app!/g)).toBeVisible();
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found the problem. The describe test block has this setup/teardown configuration that is preventing the events to be caught

beforeAll(() => {
  window.addEventListener = jest.fn();
  window.removeEventListener = jest.fn();
});

afterAll(() => {
  (window.addEventListener as any).mockClear();
  (window.removeEventListener as any).mockClear();
});
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