Does the TestNavHostController create the fragments?

Viewed 22

I try to explain my issue:

I have three fragment in my scenario:

  • FragmenA the app homeview on a navigation graph: nav_graphA;

  • FragmentB and FragmentC on an app feature on its nav_graphB.

I'm wrinting an instrumentation tests on FragmentC. Here I check the VM states, if the behaviour is right It's setted a previousBackStackEntry arg:

previousBackStackEntry?.savedStateHandle?.set(IS_LOGGED_RESULT_KEY, true)

and called popBackStack() to FragmentB

Then I check if he navController.currentDestination is FragmentB and that's is OK.

fragmentB with IS_LOGGED_RESULT_KEY = true does something similar:

previousBackStackEntry?.savedStateHandle?.set(SIGNING_GRAPH_RESULT_KEY, true)
popBackStack()

to FragmentA

My question is:

Does the TestNavHostController create the fragments so my test has to end on FragmentA?

Or Is it right as is and the TestNavHostController stays only nav_graphB the reads the id of the previous fragment?

Thank you to any one has the answer

I copy below my test code:

   @Test
    fun check_login_success_state() {
        val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
        val userTest = User(
            id = "1",
            email = "test",
            firstName = "test",
            lastName = "test",
            phone = "0000",
            phoneCode = "",
            friendReferralCode = null,
            availableCreditAmount = 0
        )


        launchFragmentInHiltContainerWithConstructor(
            fragmentArgs = FragmentCArgs().toBundle()
        ) {
            FragmentC().also{ fragmentC ->
                fragmentC.viewLifecycleOwnerLiveData.observeForever{ viewLifecycleOwner ->
                    if(viewLifecycleOwner != null){
                        navController.setViewModelStore(ViewModelStore())
                        navController.setGraph(R.navigation.nav_graphB)
                        Navigation.setViewNavController(fragmentC.requireView(), navController)

                        navController.setCurrentDestination(R.id.fragmentC)

                        fragmentC.renderUI(
                            SignInState(
                                user = userTest,
                                error = false,
                                isLoading = false,
                                isDeviceRegistered = true
                            )
                        )
                    }
                }
            }
        }
        assertThat(navController.currentDestination?.id, `is`(R.id.fragmentB))
    }

fragmentC.renderUI code:

@VisibleForTesting
    fun renderUI(fragmentCState: FragmentCState) {
        when {
            signInState.user != null && fragmentCState.isDeviceRegistered -> {
                //loading = false
                with(findNavController()) {
                    previousBackStackEntry?.savedStateHandle?.set(IS_LOGGED_RESULT_KEY, true)
                    popBackStack()
                }
            }
            .....
            ...
        }
    }
0 Answers
Related