I am trying to run a simple compose ui test. I am setting it up like this:
@Before
fun setUp(){
hiltRule.inject()
composeRule.activity.setContent {
ApolloTheme {
Navigator(CharactersListScreen())
}
}
}
And the test:
@OptIn(ExperimentalTestApi::class)
@Test
fun onClick() {
composeRule.onNodeWithTag(TestTags.SEARCH_FIELD).performTextInput("Anything")
composeRule.onNodeWithContentDescription("Search").performClick()
composeRule.waitUntilAtLeastOneExists(hasTestTag("1"), 5000)
composeRule.onNodeWithTag("1").assertIsDisplayed()
}
CharactersListScreen contains viewmodel.
The problem is suspend functions of the viewmodel does not seem to work correctly. Calling delay() immediately cancels the coroutine:
kotlinx.coroutines.JobCancellationException: Job was cancelled; job=SupervisorJobImpl{Cancelling}@8ff2fb5
Also when updating state value ...
fun updateSearchQuery(value : String) {
searchQuery.value = value
}
... the new value is not collected:
@OptIn(ExperimentalCoroutinesApi::class)
val charactersPagingFlow = searchQuery.flatMapLatest { query ->
getNewCharactersPagingFlow(query )
}.cachedIn(viewModelScope)
Works though when I am launching my whole activity. Not setting content