Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda