diff --git a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/note/SaveViewModelTest.kt b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/note/SaveViewModelTest.kt index c4c2d482..a0b53947 100644 --- a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/note/SaveViewModelTest.kt +++ b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/note/SaveViewModelTest.kt @@ -2,8 +2,8 @@ package com.softartdev.notedelight.shared.presentation.note -import com.softartdev.notedelight.shared.navigation.Router import com.softartdev.notedelight.shared.CoroutineDispatchersStub +import com.softartdev.notedelight.shared.navigation.Router import com.softartdev.notedelight.shared.presentation.MainDispatcherRule import com.softartdev.notedelight.shared.usecase.note.SaveNoteUseCase import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -14,7 +14,6 @@ import org.junit.Test import org.mockito.Mockito import kotlin.test.assertFalse import kotlin.test.assertTrue -import kotlin.time.Duration.Companion.seconds class SaveViewModelTest { @@ -22,14 +21,17 @@ class SaveViewModelTest { val mainDispatcherRule = MainDispatcherRule() private val mockRouter = Mockito.mock(Router::class.java) - private val coroutineDispatchers = CoroutineDispatchersStub(testDispatcher = mainDispatcherRule.testDispatcher) + private val coroutineDispatchers = CoroutineDispatchersStub( + scheduler = mainDispatcherRule.testDispatcher.scheduler + ) private val saveViewModel: SaveViewModel = SaveViewModel(mockRouter, coroutineDispatchers) @Test - fun `Don't save and nav back`() = runTest(timeout = 3.seconds) { + fun `Don't save and nav back`() = runTest { saveViewModel.doNotSaveAndNavBack() advanceUntilIdle() assertFalse(SaveNoteUseCase.saveChannel.receiveCatching().getOrThrow()) + advanceUntilIdle() Mockito.verify(mockRouter).popBackStack() Mockito.verifyNoMoreInteractions(mockRouter) } @@ -39,6 +41,7 @@ class SaveViewModelTest { saveViewModel.saveNoteAndNavBack() advanceUntilIdle() assertTrue(SaveNoteUseCase.saveChannel.receiveCatching().getOrThrow()) + advanceUntilIdle() Mockito.verify(mockRouter).popBackStack() Mockito.verifyNoMoreInteractions(mockRouter) } diff --git a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/change/ChangeViewModelTest.kt b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/change/ChangeViewModelTest.kt index e0c2ba63..f51e1979 100644 --- a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/change/ChangeViewModelTest.kt +++ b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/change/ChangeViewModelTest.kt @@ -28,7 +28,9 @@ class ChangeViewModelTest { private val checkPasswordUseCase = Mockito.mock(CheckPasswordUseCase::class.java) private val changePasswordUseCase = Mockito.mock(ChangePasswordUseCase::class.java) private val router = Mockito.mock(Router::class.java) - private val coroutineDispatchers = CoroutineDispatchersStub(testDispatcher = mainDispatcherRule.testDispatcher) + private val coroutineDispatchers = CoroutineDispatchersStub( + scheduler = mainDispatcherRule.testDispatcher.scheduler + ) private val changeViewModel = ChangeViewModel(checkPasswordUseCase, changePasswordUseCase, router, coroutineDispatchers) @Test diff --git a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/confirm/ConfirmViewModelTest.kt b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/confirm/ConfirmViewModelTest.kt index 2e677d7d..e13fcc20 100644 --- a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/confirm/ConfirmViewModelTest.kt +++ b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/confirm/ConfirmViewModelTest.kt @@ -26,7 +26,9 @@ class ConfirmViewModelTest { private val changePasswordUseCase = Mockito.mock(ChangePasswordUseCase::class.java) private val router = Mockito.mock(Router::class.java) - private val coroutineDispatchers = CoroutineDispatchersStub(testDispatcher = mainDispatcherRule.testDispatcher) + private val coroutineDispatchers = CoroutineDispatchersStub( + scheduler = mainDispatcherRule.testDispatcher.scheduler + ) private val confirmViewModel = ConfirmViewModel(changePasswordUseCase, router, coroutineDispatchers) @Test diff --git a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/enter/EnterViewModelTest.kt b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/enter/EnterViewModelTest.kt index a342c3ec..f9f0e28b 100644 --- a/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/enter/EnterViewModelTest.kt +++ b/shared/src/androidUnitTest/kotlin/com/softartdev/notedelight/shared/presentation/settings/security/enter/EnterViewModelTest.kt @@ -2,9 +2,9 @@ package com.softartdev.notedelight.shared.presentation.settings.security.enter import androidx.arch.core.executor.testing.InstantTaskExecutorRule import app.cash.turbine.test +import com.softartdev.notedelight.shared.CoroutineDispatchersStub import com.softartdev.notedelight.shared.StubEditable import com.softartdev.notedelight.shared.navigation.Router -import com.softartdev.notedelight.shared.CoroutineDispatchersStub import com.softartdev.notedelight.shared.presentation.MainDispatcherRule import com.softartdev.notedelight.shared.usecase.crypt.ChangePasswordUseCase import com.softartdev.notedelight.shared.usecase.crypt.CheckPasswordUseCase @@ -29,7 +29,9 @@ class EnterViewModelTest { private val checkPasswordUseCase = Mockito.mock(CheckPasswordUseCase::class.java) private val changePasswordUseCase = Mockito.mock(ChangePasswordUseCase::class.java) private val router = Mockito.mock(Router::class.java) - private val coroutineDispatchers = CoroutineDispatchersStub(testDispatcher = mainDispatcherRule.testDispatcher) + private val coroutineDispatchers = CoroutineDispatchersStub( + scheduler = mainDispatcherRule.testDispatcher.scheduler + ) private var enterViewModel: EnterViewModel = EnterViewModel(checkPasswordUseCase, changePasswordUseCase, router, coroutineDispatchers) @Before @@ -65,10 +67,6 @@ class EnterViewModelTest { enterViewModel.enterCheck(pass) assertEquals(EnterResult.Loading, awaitItem()) assertEquals(EnterResult.IncorrectPasswordError, awaitItem()) -// advanceUntilIdle() -// val actuals: List = awaitAll() -// assertEquals(EnterResult.Loading, actuals[0]) -// assertEquals(EnterResult.IncorrectPasswordError, actuals[1]) cancelAndIgnoreRemainingEvents() } @@ -108,6 +106,7 @@ class EnterViewModelTest { @Test fun navigateUp() = runTest { enterViewModel.navigateUp() + advanceUntilIdle() Mockito.verify(router).popBackStack() } } \ No newline at end of file diff --git a/shared/src/commonTest/kotlin/com/softartdev/notedelight/shared/CoroutineDispatchersStub.kt b/shared/src/commonTest/kotlin/com/softartdev/notedelight/shared/CoroutineDispatchersStub.kt index 13f42295..4b99dc44 100644 --- a/shared/src/commonTest/kotlin/com/softartdev/notedelight/shared/CoroutineDispatchersStub.kt +++ b/shared/src/commonTest/kotlin/com/softartdev/notedelight/shared/CoroutineDispatchersStub.kt @@ -2,15 +2,16 @@ package com.softartdev.notedelight.shared import com.softartdev.notedelight.shared.util.CoroutineDispatchers import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.IO +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestCoroutineScheduler import kotlinx.coroutines.test.TestDispatcher -@ExperimentalCoroutinesApi class CoroutineDispatchersStub(testDispatcher: TestDispatcher) : CoroutineDispatchers { + + constructor(scheduler: TestCoroutineScheduler) : this(StandardTestDispatcher(scheduler)) + override val default: CoroutineDispatcher = testDispatcher override val main: CoroutineDispatcher = testDispatcher override val unconfined: CoroutineDispatcher = testDispatcher - override val io: CoroutineDispatcher = Dispatchers.IO + override val io: CoroutineDispatcher = testDispatcher }