I have an app where I have 2 screens:
NavHost(
navController = navController,
startDestination = UserListScreen
) {
composable<UserListScreen> {
UserListScreen(
navigateToUserDetails = { user ->
navController.navigate(user.toUserDetails())
}
)
}
composable<UserDetails> { entry ->
UserDetailsScreen(
user = entry.toRoute<UserDetails>().toUser()
)
}
}
Where the UserListScreen is this:
fun UserListScreen(
viewModel: UserListViewModel = hiltViewModel(),
navigateToUserDetails: (User) -> Unit
) {
Scaffold(
topBar = { UserListTopBar() }
) {
when(viewModel.userListResponse) {
is Loading -> CircularProgressIndicator()
is Success -> {
val userList = userListResponse.userList
UserListContent(
userList = userList,
onUserClick = navigateToUserDetails
)
}
is Failure -> print(userListResponse.e.message)
}
}
}
Here I get the user list from the ViewModel and pass it to the UserListContent composable. All works fine. The problem comes when testing. For example, on user click, I want to test if the user is correctly navigating to the UserDetailsScreen. So I need to somehow pass a fake user list to the UserListContent so I can see the result of the testing. Here is what I have tried:
class UserListNavigationTest {
lateinit var navController: TestNavHostController
@Before
fun setupNavHost() {
composeTestRule.activity.setContent {
navController = TestNavHostController(LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())
NavHost(
navController = navController,
startDestination = UserListScreen
) {
composable<UserListScreen> {
UserListScreen(
navigateToUserDetails = { user ->
navController.navigate(user.toUserDetails())
}
)
}
composable<UserDetails> { entry ->
UserDetailsScreen(
user = entry.toRoute<UserDetails>().toUser()
)
}
}
}
}
@Test
fun testDestination() {
//ToDo
}
}
So I can call UserListScreen(), but I don't have access to the UserListContent() so I can pass a fake list in order to test. How to solve this problem?