Skip to content

Commit

Permalink
WTA #44: Updated GetCachedHomePageUiDataTests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Oct 22, 2022
1 parent 3839e37 commit 9534492
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.datetime.LocalDate
internal class CalculateCurrentStreakUCRobot {
private lateinit var useCase: CalculateCurrentStreakUC

private var result: Error? = null
private var result: Either<Error, StreakRange>? = null

private val mockCache: HomePageCache = mockk(relaxUnitFun = true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,42 @@ import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.core.models.StatsRange
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.models.WeeklyStats
import com.jacob.wakatimeapp.home.data.local.HomePageCache
import com.jacob.wakatimeapp.home.data.network.HomePageNetworkData
import com.jacob.wakatimeapp.home.domain.models.Last7DaysStats
import io.kotest.matchers.shouldBe
import io.mockk.clearMocks
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.datetime.LocalDate

@OptIn(ExperimentalCoroutinesApi::class)
internal class GetLast7DaysStatsUCRobot {
private lateinit var useCase: GetLast7DaysStatsUC

private var result: Error? = null
private var result: Either<Error, Last7DaysStats>? = null

private val networkDataMock: HomePageNetworkData = mockk(relaxUnitFun = true)
private val cacheMock: HomePageCache = mockk(relaxUnitFun = true)

fun buildViewModel() = apply {
clearMocks(networkDataMock, cacheMock)
clearMocks(networkDataMock)
result = null

useCase = GetLast7DaysStatsUC(
dispatcher = UnconfinedTestDispatcher(),
homePageNetworkData = networkDataMock,
homePageCache = cacheMock,
)
}

suspend fun callUseCase() = apply {
result = useCase()
}

fun resultsShouldBe(expected: Error?) = apply {
fun resultsShouldBe(expected: Either<Error, Last7DaysStats>) = apply {
result shouldBe expected
}

fun mockNetworkData(data: Either<Error, WeeklyStats>) = apply {
coEvery { networkDataMock.getLast7DaysStats() } returns data
}

fun verifyUpdateLast7DaysStatsCacheCalled(count: Int = 1) = apply {
coVerify(exactly = count) { cacheMock.updateLast7DaysStats(any()) }
}

companion object {
private val todaysDate = LocalDate(2022, 10, 10)

Expand All @@ -72,5 +61,14 @@ internal class GetLast7DaysStatsUCRobot {
date = todaysDate,
)
)

val last7DaysStats = Last7DaysStats(
timeSpentToday = Time.fromDecimal(1.0f),
projectsWorkedOn = listOf(),
weeklyTimeSpent = mapOf(),
mostUsedLanguage = "",
mostUsedEditor = "",
mostUsedOs = "",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jacob.wakatimeapp.home.domain.usecases
import arrow.core.left
import arrow.core.right
import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.home.domain.usecases.GetLast7DaysStatsUCRobot.Companion.last7DaysStats
import com.jacob.wakatimeapp.home.domain.usecases.GetLast7DaysStatsUCRobot.Companion.weeklyStats
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
Expand All @@ -12,40 +13,21 @@ import org.junit.jupiter.api.Test
internal class GetLast7DaysStatsUCTest {
private val useCaseRobot: GetLast7DaysStatsUCRobot = GetLast7DaysStatsUCRobot()

@Test
internal fun `when network request returns valid data, then update cache`() = runTest {
useCaseRobot.buildViewModel()
.mockNetworkData(weeklyStats.right())
.callUseCase()
.verifyUpdateLast7DaysStatsCacheCalled()
}

@Test
internal fun `when network request returns valid data, then return null`() = runTest {
useCaseRobot.buildViewModel()
.mockNetworkData(weeklyStats.right())
.callUseCase()
.resultsShouldBe(null)
.resultsShouldBe(last7DaysStats.right())
}

@Test
internal fun `when network request fails with an error, then return the error`() = runTest {
val error = Error.UnknownError("error")
val error = Error.UnknownError("error").left()

useCaseRobot.buildViewModel()
.mockNetworkData(error.left())
.mockNetworkData(error)
.callUseCase()
.resultsShouldBe(error)
}

@Test
internal fun `when network request fails with an error, then update cache should not be called`() =
runTest {
val error = Error.UnknownError("error")

useCaseRobot.buildViewModel()
.mockNetworkData(error.left())
.callUseCase()
.verifyUpdateLast7DaysStatsCacheCalled(0)
}
}

0 comments on commit 9534492

Please sign in to comment.