Problem testing React Native using React Navigation

Viewed 70

I am using react navigation as my router in the app, but I want to unit test my WelcomeScreen component. When I try to run a simple test I am getting this error:

I am using Enzyme, but tried testing library and same happens.

Couldn't find a navigation object. Is your component inside a screen in a navigator?

      12 |
      13 | const WelcomeScreen = () => {
    > 14 |     const navigation = useNavigation()
         |                        ^
      15 |
      16 |     return (
      17 |         <Screen>

How can i solve this? Thank you.

EDIT: (Added navigaition component)

const Stack = createStackNavigator()
const { Navigator, Screen } = Stack

const Navigation = () => {
    return (
        <NavigationContainer>
            <Navigator initialRouteName={'Welcome'} headerMode="none">
                <Screen name="Welcome" component={WelcomeScreen} />
                <Screen name="ListCats" component={ListScreen} />
                <Screen name="AddCat" component={AddScreen} />
                <Screen name="EditCat" component={EditScreen} />
                <Screen name="Cat" component={CatScreen} />
            </Navigator>
        </NavigationContainer>
    )
}

export default Navigation
0 Answers
Related