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)