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

223
Visualizações
How can I test react native's alert in testing library?

I wanted to check a text inside the alert, and maybe get a button from it

it('should show message in press button', () => {
const { getByText, getAllByText } = render(
  <OrganizationDocumentsController />
);

const buttonAddressCompany = getByText('ProofAddress');
expect(buttonAddressCompany).not.toBeNull();

fireEvent.press(buttonAddressCompany);
expect(Alert.alert).toHaveBeenCalled();

<<< error appears on this line below >>>
const buttonAlert = getAllByText('Cancel');

});

I have a controller(functions) and screen(view) structure if you can help with sample test codes in text to alert or button, I appreciate it.

I would like to test the message that is displayed on the screen when the alert is opened, example: "Cancel"

Alert.alert(
  'Would you like a photo or a document?',
  '',
  [
    {
      text: 'Send Document',
      onPress: () => openDocumentFile(type),
    },
    {
      text: 'Take Photo',
      onPress: () => {
        setModalVisiblePhoto(true);
        return setTypeDocument(type);
      },
    },
    {
      text: 'Cancel',
      style: 'cancel',
    },
  ],
  {
    cancelable: true,
  }
);

I found a way to resolve my problem you need import the alert and you can test the call, the texts within the alert and also the onpress functions

import { Alert } from 'react-native';

jest.spyOn(Alert, 'alert');

expect(Alert.alert).toHaveBeenCalled(); //check the call

const alertContent = Alert.alert.mock.calls[0]; //you can check text and onpress make a console log (alertContent)

about 4 years ago · Juan Pablo Isaza
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