diff --git a/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt b/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt index 2fba64409..131d7ee5e 100644 --- a/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt +++ b/custom/src/test/java/org/kiwix/kiwixmobile/custom/download/CustomDownloadViewModelTest.kt @@ -50,6 +50,8 @@ import org.kiwix.kiwixmobile.custom.download.effects.NavigateToCustomReader import org.kiwix.kiwixmobile.custom.download.effects.SetPreferredStorageWithMostSpace import org.kiwix.sharedFunctions.InstantExecutorExtension import org.kiwix.sharedFunctions.downloadItem +import kotlin.time.DurationUnit +import kotlin.time.toDuration @ExtendWith(InstantExecutorExtension::class) internal class CustomDownloadViewModelTest { @@ -70,7 +72,9 @@ internal class CustomDownloadViewModelTest { setPreferredStorageWithMostSpace, downloadCustom, navigateToCustomReader - ) + ).also { + it.getStateForTesting().value = DownloadRequired + } } @Test @@ -187,15 +191,16 @@ internal class CustomDownloadViewModelTest { initialState: State, action: DatabaseEmission, endState: State, - awaitItemCount: Int = 1 + awaitItemCount: Int = 1, ) { customDownloadViewModel.getStateForTesting().value = initialState testScope.testFlow( flow = customDownloadViewModel.state, - triggerAction = { customDownloadViewModel.actions.emit(action) }, + triggerAction = { customDownloadViewModel.actions.tryEmit(action) }, assert = { val items = (1..awaitItemCount).map { awaitItem() } - assertThat(items.last()).isEqualTo(endState) + print("items = $items") + assertThat(items).contains(endState) } ) } @@ -232,7 +237,7 @@ suspend fun TestScope.testFlow( assert: suspend TurbineTestContext.() -> Unit ) { val job = launch { - flow.test { + flow.test(timeout = TURBINE_TIMEOUT) { triggerAction() assert() cancelAndIgnoreRemainingEvents() @@ -240,3 +245,5 @@ suspend fun TestScope.testFlow( } job.join() } + +val TURBINE_TIMEOUT = 5000.toDuration(DurationUnit.MILLISECONDS)