Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
softartdev committed Oct 12, 2024
1 parent 117fa7a commit 200d6e2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,22 +14,24 @@ import org.junit.Test
import org.mockito.Mockito
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds

class SaveViewModelTest {

@get:Rule
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)
}
Expand All @@ -39,6 +41,7 @@ class SaveViewModelTest {
saveViewModel.saveNoteAndNavBack()
advanceUntilIdle()
assertTrue(SaveNoteUseCase.saveChannel.receiveCatching().getOrThrow())
advanceUntilIdle()
Mockito.verify(mockRouter).popBackStack()
Mockito.verifyNoMoreInteractions(mockRouter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -65,10 +67,6 @@ class EnterViewModelTest {
enterViewModel.enterCheck(pass)
assertEquals(EnterResult.Loading, awaitItem())
assertEquals(EnterResult.IncorrectPasswordError, awaitItem())
// advanceUntilIdle()
// val actuals: List<EnterResult> = awaitAll<EnterResult>()
// assertEquals(EnterResult.Loading, actuals[0])
// assertEquals(EnterResult.IncorrectPasswordError, actuals[1])

cancelAndIgnoreRemainingEvents()
}
Expand Down Expand Up @@ -108,6 +106,7 @@ class EnterViewModelTest {
@Test
fun navigateUp() = runTest {
enterViewModel.navigateUp()
advanceUntilIdle()
Mockito.verify(router).popBackStack()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 200d6e2

Please sign in to comment.