Skip to content

Commit

Permalink
[#354] [Sample] Add HomeViewModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
luongvo committed Feb 3, 2023
1 parent 12651e5 commit 4103389
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 5 deletions.
2 changes: 2 additions & 0 deletions sample-compose/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ dependencies {
releaseImplementation("com.github.chuckerteam.chucker:library-no-op:${Versions.CHUCKER_VERSION}")

// Testing
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.KOTLINX_COROUTINES_VERSION}")
testImplementation("io.kotest:kotest-assertions-core:${Versions.TEST_KOTEST_VERSION}")
testImplementation("junit:junit:${Versions.TEST_JUNIT_VERSION}")
testImplementation("androidx.test:core:${Versions.TEST_ANDROIDX_CORE_VERSION}")
Expand All @@ -151,6 +152,7 @@ dependencies {
testImplementation("com.google.dagger:hilt-android-testing:${Versions.HILT_VERSION}")
testImplementation("org.jetbrains.kotlin:kotlin-reflect:${Versions.KOTLIN_REFLECT_VERSION}")
testImplementation("io.mockk:mockk:${Versions.TEST_MOCKK_VERSION}")
testImplementation("app.cash.turbine:turbine:${Versions.TEST_TURBINE}")

kaptTest("com.google.dagger:hilt-android-compiler:${Versions.HILT_VERSION}")
testAnnotationProcessor("com.google.dagger:hilt-android-compiler:${Versions.HILT_VERSION}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ data class UiModel(
val id: String
) : Parcelable

private fun Model.toUiModel() = UiModel(id.toString())

fun List<Model>.toUiModels() = this.map { it.toUiModel() }
fun Model.toUiModel() = UiModel(id.toString())
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package co.nimblehq.sample.compose.ui.screens.home

import co.nimblehq.sample.compose.domain.usecase.UseCase
import co.nimblehq.sample.compose.model.UiModel
import co.nimblehq.sample.compose.model.toUiModels
import co.nimblehq.sample.compose.model.toUiModel
import co.nimblehq.sample.compose.ui.AppDestination
import co.nimblehq.sample.compose.ui.base.BaseViewModel
import co.nimblehq.sample.compose.util.DispatchersProvider
Expand All @@ -28,7 +28,7 @@ class HomeViewModel @Inject constructor(
_error.emit(it)
}
.collect { result ->
val uiModels = result.toUiModels()
val uiModels = result.map { it.toUiModel() }
_uiModels.emit(uiModels)
}
hideLoading()
Expand Down
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
}
}
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
)
}
}
1 change: 1 addition & 0 deletions sample-compose/buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ object Versions {
const val TEST_KOTEST_VERSION = "4.6.3"
const val TEST_MOCKK_VERSION = "1.10.6"
const val TEST_RUNNER_VERSION = "1.4.0"
const val TEST_TURBINE = "0.12.1"
}

0 comments on commit 4103389

Please sign in to comment.