typeError: cannot read properties of null (reading 'props'). react native testing library

2.1k views Asked by At

I wanted to test if i click the button, it would navigate to login screen with react native testing library but i am unable to do it. the error was typeError: cannot read properties of null (reading 'props'). here is my register.test.js:

describe('Login button', () => {
    test('should move to registration completed', () => {
      const navigation = {
        navigate: () => {
          mocked;
        },
      };

      const page = render(reduxTesting(<Register navigation={navigation} />));

      const registerButton = page.queryByTestId('RegisterButton');

      fireEvent.press(registerButton);

      expect(registerButton).toBeInTheDocument();

      expect(navigation.navigate).toHaveBeenCalledWith('RegistrationCompleted');
    });

and this is my register screen

 <TouchableOpacity
        testID="RegisterButton"
        style={styles.button}
        onPress={register}>
        <Text style={styles.buttonText}>Register</Text>
      </TouchableOpacity>

it seems that the registerButton is null. even when i have made testID in my touchable opacity.

1

There are 1 answers

0
Nagibaba On

use getByTestId instead of queryByTestId. It doesn't get the component asqueryByTestId is async.