-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0546899
commit 8ef227e
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
feature/home/src/test/kotlin/nl/q42/template/presentation/home/HomeViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package nl.q42.template.presentation.home | ||
|
||
import app.cash.turbine.test | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import junit.framework.TestCase.assertEquals | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.test.runTest | ||
import nl.q42.template.actionresult.domain.ActionResult | ||
import nl.q42.template.domain.user.model.User | ||
import nl.q42.template.domain.user.usecase.GetUserUseCase | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
|
||
class HomeViewModelTest(){ | ||
@get:Rule | ||
val mainDispatcherRule = MainDispatcherRule() | ||
@Test | ||
fun `WHEN I subscribe to uiState with a slow UserUseCase THEN I get the loading state and expected email address`() = runTest{ | ||
val getUserUseCaseMock: GetUserUseCase = mockk() | ||
coEvery {getUserUseCaseMock.invoke() }.coAnswers { | ||
// demonstration of test scheduler. This does not actually block the test for 4 seconds | ||
delay(4.seconds) | ||
ActionResult.Success(User("[email protected]")) | ||
} | ||
|
||
val viewModel = HomeViewModel(getUserUseCaseMock, mockk()) | ||
|
||
viewModel.uiState.test { | ||
val expectedData: HomeViewState = HomeViewState.Data("[email protected]") | ||
|
||
assertEquals(HomeViewState.Loading, awaitItem()) | ||
assertEquals(expectedData, awaitItem()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `WHEN I subscribe to uiState with a fast UserUseCase THEN I get expected email address immediately`() = runTest{ | ||
val getUserUseCaseMock: GetUserUseCase = mockk() | ||
coEvery { getUserUseCaseMock.invoke() }.returns( | ||
ActionResult.Success(User("[email protected]") | ||
)) | ||
|
||
|
||
val viewModel = HomeViewModel(getUserUseCaseMock, mockk()) | ||
|
||
viewModel.uiState.test { | ||
val expectedData: HomeViewState = HomeViewState.Data("[email protected]") | ||
assertEquals(expectedData, awaitItem()) | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
feature/home/src/test/kotlin/nl/q42/template/presentation/home/MainDispatcherRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package nl.q42.template.presentation.home | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
// Reusable JUnit4 TestRule to override the Main dispatcher | ||
class MainDispatcherRule( | ||
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(), | ||
) : TestWatcher() { | ||
override fun starting(description: Description) { | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
|
||
override fun finished(description: Description) { | ||
Dispatchers.resetMain() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters