-
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.
WTA #44: Started adding tests for GetCachedHomePageUiData.
- Loading branch information
Showing
4 changed files
with
107 additions
and
13 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
core/common/src/main/java/com/jacob/wakatimeapp/core/common/Utils.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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package com.jacob.wakatimeapp.core.common // ktlint-disable filename | ||
package com.jacob.wakatimeapp.core.common | ||
|
||
import java.time.format.TextStyle.SHORT | ||
import java.util.Locale | ||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toLocalDateTime | ||
|
||
val LocalDate.Companion.today | ||
get() = Instant.DISTANT_PAST.toLocalDateTime(TimeZone.currentSystemDefault()).date | ||
get() = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date | ||
|
||
fun LocalDate.getDisplayNameForDay(): String = | ||
dayOfWeek.getDisplayName(SHORT, Locale.getDefault()) |
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
75 changes: 75 additions & 0 deletions
75
.../src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataRobot.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,75 @@ | ||
package com.jacob.wakatimeapp.home.domain.usecases | ||
|
||
import arrow.core.Either | ||
import com.jacob.wakatimeapp.core.common.auth.AuthDataStore | ||
import com.jacob.wakatimeapp.core.models.Error | ||
import com.jacob.wakatimeapp.home.data.local.HomePageCache | ||
import com.jacob.wakatimeapp.home.domain.InstantProvider | ||
import io.kotest.matchers.collections.shouldContainExactly | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.clearMocks | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlin.time.Duration.Companion.days | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.TimeZone | ||
|
||
internal class GetCachedHomePageUiDataRobot { | ||
private lateinit var useCase: GetCachedHomePageUiData | ||
|
||
private val results = mutableListOf<Either<Error, CachedHomePageUiData?>>() | ||
|
||
private val mockHomePageCache: HomePageCache = mockk() | ||
private val mockAuthDataStore: AuthDataStore = mockk() | ||
|
||
fun buildUseCase(instantProvider: InstantProvider = defaultTestInstantProvider) = apply { | ||
clearMocks(mockHomePageCache, mockAuthDataStore) | ||
results.clear() | ||
|
||
useCase = GetCachedHomePageUiData( | ||
instantProvider = instantProvider, | ||
homePageCache = mockHomePageCache, | ||
authDataStore = mockAuthDataStore, | ||
) | ||
} | ||
|
||
suspend fun callUseCase() = apply { | ||
useCase().toList(results) | ||
} | ||
|
||
fun setLastRequestTime(previousDay: Instant) = apply { | ||
coEvery { mockHomePageCache.getLastRequestTime() } returns flowOf(previousDay) | ||
} | ||
|
||
fun resultsSizeShouldBe(size: Int) = apply { | ||
results.size shouldBe size | ||
} | ||
|
||
fun resultsShouldBe(expected: List<Either<Error, CachedHomePageUiData?>>) = apply { | ||
results shouldContainExactly expected | ||
} | ||
|
||
companion object { | ||
val defaultTestInstantProvider = object : InstantProvider { | ||
override val timeZone = TimeZone.UTC | ||
|
||
override fun now() = currentDayInstant | ||
} | ||
|
||
/** | ||
* Start of a random day | ||
* | ||
* Value: | ||
* - date: 11/10/2022 (dd/mm/yyyy) | ||
* - time: 00:00:00 (hh:mm::ss) | ||
*/ | ||
val currentDayInstant = Instant.parse("2022-10-11T00:00:00Z") | ||
|
||
/** | ||
* Takes [currentDayInstant] and subtracts 1 day from it | ||
*/ | ||
val previousDayInstant = currentDayInstant.minus(1.days) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataTest.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 com.jacob.wakatimeapp.home.domain.usecases | ||
|
||
import arrow.core.Either | ||
import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.previousDayInstant | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
internal class GetCachedHomePageUiDataTest { | ||
private val robot = GetCachedHomePageUiDataRobot() | ||
|
||
@Test | ||
internal fun `when last request was made the previous day, then use case should return null`() = | ||
runTest { | ||
robot.buildUseCase() | ||
.setLastRequestTime(previousDayInstant) | ||
.callUseCase() | ||
.resultsSizeShouldBe(1) | ||
.resultsShouldBe(listOf(Either.Right(null))) | ||
} | ||
} |