Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!