1 year ago

#341642

test-img

universe11

how can I test when I press a button to action a navigate function?

I want to test my profile page. How can I test it when I press a button and navigate it with test?

I get this error:

TypeError: Cannot read properties of undefined (reading 'onPress')
  if(!data) {
    function navigateToAuth(isLogin: number): void {
      isLogin === 1 ? navigation.navigate('Auth', { isLogin }) : navigation.navigate('Auth', { isLogin });
    }

    return (
      <View testID='Unlogged' style={s.container}>
        <Text style={s.title}>Login Title</Text>
        <View style={s.btnContainer}>
          <Button testID='login' onPress={() => navigateToAuth(1)} style={ButtonStyles.normal}>
            <Text style={s.btnText}>Login</Text>
          </Button>
 

        </View>  
      </View>

test.js

  it('should navigate on press', () => {
    const push = jest.fn();
    const { getByText, findByTestId } = render(
      <Provider store={store}>
        <NavigationContainer>
          <Profile navigation={{push}} />
        </NavigationContainer>
      </Provider>
    )

    fireEvent.press(findByTestId('login'));
    expect(push).toHaveBeenCalledWith('Auth');
  });

react-native

jestjs

expo

react-native-testing-library

0 Answers

Your Answer

Accepted video resources