Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to test screen in nested Navigation #1518

Unanswered
szkieb asked this question in Q&A
Discussion options

Hi,
I really could use help setting up a test for my LoginScreen. My goal is to test the screen not just the login button (and many more screens once that works) . But setting up the wrapper turns out to be quite the challenge.

I managed to get the redux provider to work, I quieted an annoying redux persistor error, and now after adding the navigation my test fails to find the login button on my login screen.

FYI I also have a tab navigator and I plan to extract the wrapper for repeated use in a test-util file but I reckon that is not relevant for the problem at hand.

Here's my test
LoginScreen.test.ts

test('renders correctly', () => {
 const Stack = createStackNavigator<AuthRoutes>();
 render(
 <NavigationContainer>
 <Provider store={store}>
 <Stack.Screen name="Login" component={LoginScreen} />
 </Provider>
 </NavigationContainer>
 );
 const loginButtton = screen.getByText('Login');
 expect(loginButtton).toBeOnTheScreen();
}); 

My App.tsx returns the following elements

return (<SafeAreaProvider onLayout={onLayoutRootView}>
 <Provider store={store}>
 <PersistGate loading={null} persistor={persistor}>
 <BottomSheetModalProvider>
 <ToastProvider>
 <StatusBar
 barStyle="dark-content"
 backgroundColor="transparent"
 />
 <AuthGuard> 
 <RootNavigator />
 </AuthGuard>
 </ToastProvider>
 </BottomSheetModalProvider>
 </PersistGate>
 </Provider>
 </SafeAreaProvider>)

And my RootNavigator return this

 return (
 <NavigationContainer>
 <Stack.Navigator
 screenOptions={{ headerShown: false, freezeOnBlur: true }}
 >
 {!isAuthenticated ? (
 <Stack.Screen name="Auth" component={AuthNavigator} />
 ) : (
 <Stack.Screen name="Main" component={MainNavigator} />
 )}
 </Stack.Navigator>
 </NavigationContainer>
 );
You must be logged in to vote

Replies: 1 comment

Comment options

Hello @szkieb! The test looks fine, I can't tell what could be wrong here without a reproducible demo. If you do a screen.debug after rendering what is output?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /