I have a question the proper way to test Navigatio...
# compose-wear
j
I have a question the proper way to test Navigation in compose-wear. When I use the TestNavHostController, and I create a graph with
createGraph
and
composable(Screen.MainMenu.route)
set I get a casting error:
Copy code
java.lang.ClassCastException: androidx.navigation.testing.TestNavigatorProvider$navigator$1 cannot be cast to androidx.wear.compose.navigation.WearNavigator
at androidx.wear.compose.navigation.NavGraphBuilderKt.composable(NavGraphBuilder.kt:55)
at androidx.wear.compose.navigation.NavGraphBuilderKt.composable$default(NavGraphBuilder.kt:36)
If I don't create a graph, I get a null-pointer error, since there's nowhere to navigate to. All the examples I see online seem to be using
R.navigation
, but I think that's used when your app uses an XML navigation definition, and not jetpack compose...
i
Yes,
TestNavHostController
is only relevant to Navigation XML based apps using Fragments where individual fragments had a dependency on NavController. In the Compose world, as explained in the docs, individual screens should not have any reference to Navigation at all, but instead expose events: https://developer.android.com/guide/navigation/use-graph/navigate#events
The Encapsulate your navigation code page goes into more details on how you might separate your navigation logic from your individual screen: https://developer.android.com/guide/navigation/design/encapsulate
So you shouldn't need any NavController at all to preview or test an individual screen and for integration style tests, you'd use your actual NavHost and its NavController
j
Ah, that makes a lot of sense. Thanks for taking the time to answer my newbie question 😄