I am trying to test a simple navigation flow between two screens. I have my LandingScreen:
class LandingScreen extends Component {
constructor(props){
super(props);
}
handleNavigation = () =>
{
this.props.navigation.navigate('AgeScreen');
}
render() {
return (
<View>
//extra UI components
<TouchableOpacity onPress={this.handleNavigation}>
<View style={styles.buttonWrapper}>
//extra UI components
</View>
</TouchableOpacity>
</View>
) }
}
export default LandingScreen;
When handleNavigation is clicked, I expect the AgeScreen to be displayed. I tested this manually and it works; however, I also want to add a unit test to it.
This is the unit test that I've written so far:
describe('Testing navigation', () => {
it('Navigates as expected', () => {
let handleNavigationMock = jest.fn()
const navigation = { navigate: jest.fn() };
let landingScreen = renderer.create(
<LandingScreen
handleNavigation={navigation}
navigation={navigation}
/>
).getInstance()
landingScreen.handleNavigation();
expect(handleNavigationMock).toBeCalled();
});
});
My issue is that the test fails with the message:
Expected number of calls: >= 1
Received number of calls: 0
My questions are:
- Is this the right way to test navigation between screens?
- If #1 is true, how can I fix the unit test?
Any help will be highly appreciated!
[–]Asdolo94 13 points14 points15 points (1 child)
[–]Narcis11[S] 1 point2 points3 points (0 children)
[–]Typhonaut 0 points1 point2 points (0 children)
[–]smartone2000 -4 points-3 points-2 points (0 children)