Skip to content

Commit

Permalink
add tests for details view model
Browse files Browse the repository at this point in the history
  • Loading branch information
kibettheophilus committed Jun 3, 2024
1 parent f39f944 commit 182e384
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.theophiluskibet.calorees.details.screens

import com.theophilus.kibet.caloree.data.sources.CaloreeRepositoryImpl
import com.theophiluskibet.calorees.details.utils.DetailsUiState
import com.theophiluskibet.calorees.details.utils.FakeCaloreeRepository
import com.theophiluskibet.calorees.details.utils.searchData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class DetailsViewModelTest {

Expand All @@ -18,7 +24,7 @@ class DetailsViewModelTest {

@BeforeTest
fun setup() {
Dispatchers.setMain(Dispatchers.Unconfined)
Dispatchers.setMain(StandardTestDispatcher())
detailsViewModel = DetailsViewModel(repository = repository)
}

Expand All @@ -28,9 +34,56 @@ class DetailsViewModelTest {
}

@Test
fun `sample test`() = runTest {
val data = repository.searchCalories("meat")
detailsViewModel.getCaloreeDetails("meat")
assertEquals("meat", data.first().name)
fun `given DetailsViewModel- initial details ui state is default`() = runTest {
assertEquals(detailsViewModel.caloreeUiState.value, DetailsUiState.Default)
}

@Test
fun `given string of food - when getCalireeDetails is invoked - then details ui state is loading`() =
runTest {
// given
val food = "rice"

// when
detailsViewModel.getCaloreeDetails(food)

// then
assertEquals(DetailsUiState.Loading, detailsViewModel.caloreeUiState.value)
}

@Test
fun `given string of food - when getCalireeDetails is success - then details ui state is success with data`() =
runTest {
// given
val food = "rice"

// when
detailsViewModel.getCaloreeDetails(food)
advanceUntilIdle()

// then
assertEquals(
DetailsUiState.Success(data = searchData.first()),
detailsViewModel.caloreeUiState.value
)
}

@Test
fun `given string of food - when getCalireeDetails is error - then details ui state is error with message`() =
runTest {
// simulate error in the repository
repository.shouldThrowError = true
// given
val food = "rice"

// when
detailsViewModel.getCaloreeDetails(food)
advanceUntilIdle()

// then
assertEquals(
DetailsUiState.Error(errorMessage = "An error occured"),
detailsViewModel.caloreeUiState.value
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@ import com.theophilus.kibet.caloree.data.sources.CaloreeRepository
import kotlinx.coroutines.flow.Flow

class FakeCaloreeRepository : CaloreeRepository {
var shouldThrowError = false
override suspend fun searchCalories(query: String): List<Caloree> {
return listOf(
Caloree(
name = "meat",
calories = 20.0,
carbohydratesTotalGrams = 20.0,
cholesterolMilliGrams = 100,
fatSaturatedGrams = 30.0,
fatTotalGrams = 44.0,
fiberGrams = 30.0,
potassiumMilliGrams = 261,
proteinGrams = 104.0,
servingSizeGrams = 100.0,
sodiumMilliGrams = 127,
sugarGrams = 68.0,
)
)
if (shouldThrowError) {
throw RuntimeException("An error occured")
}
return searchData
}

fun simulateError(): List<Caloree> {
throw Throwable(message = "Error occured")
}

override suspend fun getSavedCalories(): Flow<List<Caloree>> {
Expand All @@ -31,4 +24,21 @@ class FakeCaloreeRepository : CaloreeRepository {
override suspend fun getCalorieDetails(food: String): Flow<Caloree> {
TODO("Not yet implemented")
}
}
}

val searchData = listOf(
Caloree(
name = "meat",
calories = 20.0,
carbohydratesTotalGrams = 20.0,
cholesterolMilliGrams = 100,
fatSaturatedGrams = 30.0,
fatTotalGrams = 44.0,
fiberGrams = 30.0,
potassiumMilliGrams = 261,
proteinGrams = 104.0,
servingSizeGrams = 100.0,
sodiumMilliGrams = 127,
sugarGrams = 68.0,
)
)

0 comments on commit 182e384

Please sign in to comment.