K Merle
01/31/2024, 2:54 PMfun setSearchQuery(searchQuery: String) {
if (_searchQuery.value != searchQuery) {
_searchQuery.tryEmit(searchQuery)
foodSearchJob?.cancel()
foodSearchJob = viewModelScope.launch(ioDispatcher) {
delay(SEARCH_DEBOUNCE_MILLIS)
val query = _searchQuery.value
if (query.length >= 3) {
_foodListState.emit(FoodListState.Loading)
handleSearchQuery(query)
} else {
_foodListState.emit(FoodListState.Idle)
}
}
}
}
When I run androidTest
tests, I am not able to pass this delay. Removing delay makes test pass, so I am strictly blocked on that delay. How can I fix this? advancing time does not help. Here is my test example:
@HiltAndroidTest
class FoodListScreenTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Before
fun init() {
hiltRule.inject()
}
@Test
fun should_show_food_list() {
with(composeTestRule) {
activity.setContent { FoodListRoute() }
mainClock.advanceTimeBy(10000)
val searchQuery = activity.getString(R.string.search_food)
onNodeWithText(searchQuery).performTextInput("test")
composeTestRule.mainClock.advanceTimeBy(500)
onNodeWithText("abc").assertExists()
}
}
}