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

222
Vistas
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 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