Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question. is there a way to perform whole application tests (instrumented tests)? #547

Closed
AlexanderKott opened this issue Dec 15, 2023 · 3 comments
Labels
feedback needed Extra attention is needed

Comments

@AlexanderKott
Copy link

Hello! thank you for your library.

I have an app that have about 20 compose screens and uses your library to navigate beetwn them. I am looking for a soulution to write ui tests.

here is google codelab for a regular jetpack navigation https://developer.android.com/codelabs/basic-android-kotlin-compose-test-cupcake#3

You can see there
Junit

@before
fun setupCupcakeNavHost() {
composeTestRule.setContent {
navController = TestNavHostController(LocalContext.current).apply {
navigatorProvider.addNavigator(ComposeNavigator())
}
CupcakeApp(navController = navController)
}
}

here:
they offer us to use a TestNavHostController
CupcakeApp is a regular composable function that represent the entry point (main screen):

@composable
fun CupcakeApp(
viewModel: OrderViewModel = viewModel(),
navController: NavHostController = rememberNavController()
) {
// Get current back stack entry
val backStackEntry by navController.currentBackStackEntryAsState()
// Get the name of the current screen
val currentScreen = CupcakeScreen.valueOf(
backStackEntry?.destination?.route ?: CupcakeScreen.Start.name
)

Scaffold(
    topBar = {
        CupcakeAppBar(
            currentScreen = currentScreen,
            canNavigateBack = navController.previousBackStackEntry != null,
            navigateUp = { navController.navigateUp() }
        )

.....

you can navigate among screens like this:
navigateToPickupScreen()
composeTestRule.onNodeWithText(getFormattedDate())
.performClick()
composeTestRule.onNodeWithStringId(R.string.next)
.performClick()

lets imagine this app is using your navigation library. How to provide a test navhost to this app?

thank you

@raamcosta
Copy link
Owner

Admitedly, I haven't had timee to think about this yet.
But I believe you can also use NavController in your tests in the exact same way, right?

Ultimately we are using NavController too. If you need DestinationsNavigator in your tests, you can create an instance of it using TestNavHostController as the NavController.

You can copy the implementation from here:
https://github.com/raamcosta/compose-destinations/blob/main/compose-destinations/src/main/java/com/ramcosta/composedestinations/navigation/DestinationsNavController.kt

Let me know if you could make it work 🙂

@raamcosta raamcosta added the feedback needed Extra attention is needed label Jan 1, 2024
@AlexanderKott
Copy link
Author

thank you i will try

@raamcosta
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback needed Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants