-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#354] [Sample] Add HomeViewModelTest
- Loading branch information
Showing
6 changed files
with
122 additions
and
5 deletions.
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
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
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
32 changes: 32 additions & 0 deletions
32
sample-compose/app/src/test/java/co/nimblehq/sample/compose/test/CoroutineTestRule.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,32 @@ | ||
package co.nimblehq.sample.compose.test | ||
|
||
import co.nimblehq.sample.compose.util.DispatchersProvider | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.* | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class CoroutineTestRule( | ||
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() | ||
) : TestWatcher() { | ||
|
||
override fun starting(description: Description?) { | ||
super.starting(description) | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
|
||
override fun finished(description: Description?) { | ||
super.finished(description) | ||
Dispatchers.resetMain() | ||
} | ||
|
||
val testDispatcherProvider = object : DispatchersProvider { | ||
override val io: CoroutineDispatcher | ||
get() = testDispatcher | ||
override val main: CoroutineDispatcher | ||
get() = testDispatcher | ||
override val default: CoroutineDispatcher | ||
get() = testDispatcher | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...compose/app/src/test/java/co/nimblehq/sample/compose/ui/screens/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,84 @@ | ||
package co.nimblehq.sample.compose.ui.screens.home | ||
|
||
import app.cash.turbine.test | ||
import co.nimblehq.sample.compose.domain.model.Model | ||
import co.nimblehq.sample.compose.domain.usecase.UseCase | ||
import co.nimblehq.sample.compose.model.toUiModel | ||
import co.nimblehq.sample.compose.test.CoroutineTestRule | ||
import co.nimblehq.sample.compose.ui.AppDestination | ||
import co.nimblehq.sample.compose.util.DispatchersProvider | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.* | ||
import org.junit.* | ||
|
||
@ExperimentalCoroutinesApi | ||
class HomeViewModelTest { | ||
|
||
@get:Rule | ||
val coroutinesRule = CoroutineTestRule() | ||
|
||
private val mockUseCase: UseCase = mockk() | ||
|
||
private lateinit var viewModel: HomeViewModel | ||
|
||
private val models = listOf(Model(1), Model(2), Model(3)) | ||
|
||
@Before | ||
fun setUp() { | ||
every { mockUseCase() } returns flowOf(models) | ||
|
||
initViewModel() | ||
} | ||
|
||
@Test | ||
fun `when loading models successfully, it shows the model list`() = runTest { | ||
viewModel.uiModels.test { | ||
expectMostRecentItem() shouldBe models.map { it.toUiModel() } | ||
} | ||
} | ||
|
||
@Test | ||
fun `when loading models failed, it shows the corresponding error`() = runTest { | ||
val error = Exception() | ||
every { mockUseCase() } returns flow { throw error } | ||
initViewModel(dispatchers = CoroutineTestRule(StandardTestDispatcher()).testDispatcherProvider) | ||
|
||
viewModel.error.test { | ||
advanceUntilIdle() | ||
|
||
expectMostRecentItem() shouldBe error | ||
} | ||
} | ||
|
||
@Test | ||
fun `When loading models, it shows and hides loading correctly`() = runTest { | ||
initViewModel(dispatchers = CoroutineTestRule(StandardTestDispatcher()).testDispatcherProvider) | ||
|
||
viewModel.showLoading.test { | ||
awaitItem() shouldBe false | ||
awaitItem() shouldBe true | ||
awaitItem() shouldBe false | ||
} | ||
} | ||
|
||
@Test | ||
fun `when calling navigate to Second, it navigates to Second screen`() = runTest { | ||
viewModel.navigator.test { | ||
viewModel.navigateToSecond(models[0].toUiModel()) | ||
|
||
expectMostRecentItem() shouldBe AppDestination.Second | ||
} | ||
} | ||
|
||
private fun initViewModel(dispatchers: DispatchersProvider = coroutinesRule.testDispatcherProvider) { | ||
viewModel = HomeViewModel( | ||
mockUseCase, | ||
dispatchers | ||
) | ||
} | ||
} |
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