'Cannot read properties of undefined (reading 'onPress') testing
I want to test a function that navigates to a screen. But I get this error message:
Test profile Page › should navigate on press
TypeError: Cannot read properties of undefined (reading 'onPress')
Profile.tsx
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>
Profile_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');
});
What I am doing wrong and how can I solve this issue ?
Solution 1:[1]
I think the code should be:
<Profile navigation={push} />
with {{push}} you are sending undefined to navigation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
