From 1a0b08c5f46be3fe7290b1a6632d5c025ba6ea13 Mon Sep 17 00:00:00 2001 From: Bliss Pisit Wetcha Date: Wed, 14 Dec 2022 11:28:02 +0700 Subject: [PATCH] [#21] Add platform date test --- shared/build.gradle.kts | 3 +- ...eFormatter.kt => DateTimeFormatterImpl.kt} | 0 .../repository/DeviceInfoRepositoryImpl.kt | 12 +++-- .../blisskmmic/domain/model/DateComponent.kt | 5 +++ .../blisskmmic/domain/model/DateComponents.kt | 7 --- .../blisskmmic/domain/model/DateReadable.kt | 7 --- .../domain/repository/DeviceInfoRepository.kt | 4 +- .../domain/usecase/GetCurrentDateUseCase.kt | 17 ++----- .../data/datasource/LocalDataSourceTest.kt | 4 +- .../data/datasource/NetworkDataSourceTest.kt | 12 ++--- .../data/datastore/core/DataStoreTest.kt | 6 +-- .../data/network/core/NetworkClientTest.kt | 6 +-- .../core/TokenizedNetworkClientTest.kt | 6 +-- .../AccountRecoveryRepositoryTest.kt | 4 +- .../AuthenticationRepositoryTest.kt | 10 ++--- .../repository/DeviceInfoRepositoryTest.kt | 13 +++--- .../data/repository/SurveyRepositoryTest.kt | 4 +- .../data/repository/UserRepositoryTest.kt | 4 +- .../usecase/GetCurrentDateUseCaseTest.kt | 18 ++++---- .../domain/usecase/GetProfileUseCaseTest.kt | 4 +- .../domain/usecase/LogInUseCaseTest.kt | 4 +- .../usecase/ResetPasswordUseCaseTest.kt | 4 +- .../domain/usecase/SurveyListUseCaseTest.kt | 4 +- .../presentation/helpers/MokoLocalizeTest.kt | 4 +- .../modules/login/LoginViewModelTest.kt | 4 +- .../ResetPasswordViewModelTest.kt | 4 +- .../datetime/DateTimeFormatterImpl.kt | 5 ++- .../co/nimblehq/blisskmmic/IosGreetingTest.kt | 2 +- .../platform/datetime/DateFormatterTest.kt | 44 +++++++++++++++++++ .../datetime/DateTimeFormatterTest.kt | 24 ++++++++++ 30 files changed, 147 insertions(+), 98 deletions(-) rename shared/src/androidMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/{DateTimeFormatter.kt => DateTimeFormatterImpl.kt} (100%) create mode 100644 shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponent.kt delete mode 100644 shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponents.kt delete mode 100644 shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateReadable.kt create mode 100644 shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateFormatterTest.kt create mode 100644 shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterTest.kt diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 533049d2..3db0676a 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -233,7 +233,8 @@ kover { "*Mock*", "co.nimblehq.blisskmmic.di*", "*helpers.extensions.ios*", - "SharedBuildConfig*" + "SharedBuildConfig*", + "*domain.platform.*" ) } } diff --git a/shared/src/androidMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatter.kt b/shared/src/androidMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt similarity index 100% rename from shared/src/androidMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatter.kt rename to shared/src/androidMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryImpl.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryImpl.kt index 32ce563e..b67c77cf 100644 --- a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryImpl.kt @@ -1,6 +1,6 @@ package co.nimblehq.blisskmmic.data.repository -import co.nimblehq.blisskmmic.domain.model.DateComponents +import co.nimblehq.blisskmmic.domain.model.DateComponent import co.nimblehq.blisskmmic.domain.repository.DeviceInfoRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow @@ -10,12 +10,10 @@ class DeviceInfoRepositoryImpl( private val clock: Clock ): DeviceInfoRepository { - override fun getCurrentDate(): Flow { - val datetimeInSystemZone = clock.now().toLocalDateTime(TimeZone.currentSystemDefault()) - val dateHeader = DateComponents( - datetimeInSystemZone.dayOfMonth, - datetimeInSystemZone.monthNumber, - datetimeInSystemZone.dayOfWeek.isoDayNumber + override fun getCurrentDate(): Flow { + val now = clock.now() + val dateHeader = DateComponent( + now.epochSeconds ) return flow { emit(dateHeader) diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponent.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponent.kt new file mode 100644 index 00000000..ea609ff7 --- /dev/null +++ b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponent.kt @@ -0,0 +1,5 @@ +package co.nimblehq.blisskmmic.domain.model + +data class DateComponent ( + val timeInterval: Long +) diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponents.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponents.kt deleted file mode 100644 index 1b23b6c9..00000000 --- a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateComponents.kt +++ /dev/null @@ -1,7 +0,0 @@ -package co.nimblehq.blisskmmic.domain.model - -data class DateComponents( - val day: Int, - val month: Int, - val dayOfWeek: Int -) diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateReadable.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateReadable.kt deleted file mode 100644 index 8fc34325..00000000 --- a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/DateReadable.kt +++ /dev/null @@ -1,7 +0,0 @@ -package co.nimblehq.blisskmmic.domain.model - -data class DateReadable ( - val day: Int, - val month: String, - val dayOfWeek: String -) diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/DeviceInfoRepository.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/DeviceInfoRepository.kt index f53965e7..f453b79f 100644 --- a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/DeviceInfoRepository.kt +++ b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/DeviceInfoRepository.kt @@ -1,9 +1,9 @@ package co.nimblehq.blisskmmic.domain.repository -import co.nimblehq.blisskmmic.domain.model.DateComponents +import co.nimblehq.blisskmmic.domain.model.DateComponent import kotlinx.coroutines.flow.Flow interface DeviceInfoRepository { - fun getCurrentDate(): Flow + fun getCurrentDate(): Flow } diff --git a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCase.kt b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCase.kt index 4fb2d72f..3b5ae5dc 100644 --- a/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCase.kt +++ b/shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCase.kt @@ -1,7 +1,6 @@ package co.nimblehq.blisskmmic.domain.usecase -import co.nimblehq.blisskmmic.domain.model.DateComponents -import co.nimblehq.blisskmmic.domain.model.DateReadable +import co.nimblehq.blisskmmic.domain.model.DateComponent import co.nimblehq.blisskmmic.domain.repository.DeviceInfoRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map @@ -10,25 +9,15 @@ import kotlinx.datetime.Month interface GetCurrentDateUseCase { - operator fun invoke(): Flow + operator fun invoke(): Flow } class GetCurrentDateUseCaseImpl( private val deviceInfoRepository: DeviceInfoRepository ) : GetCurrentDateUseCase { - override operator fun invoke(): Flow { + override operator fun invoke(): Flow { return deviceInfoRepository .getCurrentDate() - .map { - // Ideally should be mapped with localize, but with date-time is sufficient - val dayOfWeek = DayOfWeek(it.dayOfWeek) - val month = Month(it.month) - DateReadable( - it.day, - month.name, - dayOfWeek.name - ) - } } } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/LocalDataSourceTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/LocalDataSourceTest.kt index 608ac3cf..47e2c934 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/LocalDataSourceTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/LocalDataSourceTest.kt @@ -31,7 +31,7 @@ class LocalDataSourceTest: TestsWithMocks() { } @Test - fun `When saving token, it saves the correct key and value to dataStore`() = runTest { + fun `When saving token- it saves the correct key and value to dataStore`() = runTest { mocker.every { dataStore.save( TokenDatabaseModel.serializer(), @@ -53,7 +53,7 @@ class LocalDataSourceTest: TestsWithMocks() { } @Test - fun `When calling getToken, dataStore returns correct value`() = runTest { + fun `When calling getToken- dataStore returns correct value`() = runTest { mocker.every { dataStore.get( TokenDatabaseModel.serializer(), diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/NetworkDataSourceTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/NetworkDataSourceTest.kt index e67fd394..4c66e716 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/NetworkDataSourceTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datasource/NetworkDataSourceTest.kt @@ -23,7 +23,7 @@ class NetworkDataSourceTest { // Log in @Test - fun `When calling log in with success response, it returns correct object`() = runTest { + fun `When calling log in with success response- it returns correct object`() = runTest { val engine = jsonMockEngine(LOG_IN_JSON_RESULT, "oauth/token") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) @@ -36,7 +36,7 @@ class NetworkDataSourceTest { } @Test - fun `When calling log in with failure response, it returns correct error`() = runTest { + fun `When calling log in with failure response- it returns correct error`() = runTest { val engine = jsonMockEngine(ERROR_JSON_RESULT, "oauth/token") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) @@ -53,7 +53,7 @@ class NetworkDataSourceTest { // Reset password @Test - fun `When calling reset password with success response, it returns correct object`() = runTest { + fun `When calling reset password with success response- it returns correct object`() = runTest { val engine = jsonMockEngine(RESET_PASSWORD_JSON_RESULT, "passwords") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) @@ -70,7 +70,7 @@ class NetworkDataSourceTest { // Survey @Test - fun `When calling survey with success response, it returns correct object`() = runTest { + fun `When calling survey with success response- it returns correct object`() = runTest { val engine = jsonMockEngine(SURVEY_LIST_JSON_RESULT, "surveys") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) @@ -86,7 +86,7 @@ class NetworkDataSourceTest { } @Test - fun `When calling survey with failure response, it returns correct error`() = runTest { + fun `When calling survey with failure response- it returns correct error`() = runTest { val engine = jsonMockEngine(ERROR_JSON_RESULT, "surveys") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) @@ -103,7 +103,7 @@ class NetworkDataSourceTest { // Profile @Test - fun `When calling profile with success response, it returns correct object`() = runTest { + fun `When calling profile with success response- it returns correct object`() = runTest { val engine = jsonMockEngine(USER_PROFILE_JSON_RESULT, "me") val networkClient = NetworkClient(engine = engine) val dataSource = NetworkDataSourceImpl(networkClient) diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datastore/core/DataStoreTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datastore/core/DataStoreTest.kt index 5cdb9f21..0711d753 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datastore/core/DataStoreTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/datastore/core/DataStoreTest.kt @@ -38,7 +38,7 @@ class DataStoreTest : TestsWithMocks() { } @Test - fun `When saving a token, it sets settings with the correct key and value`() = runTest { + fun `When saving a token- it sets settings with the correct key and value`() = runTest { val dataStore = DataStoreImpl(settings) dataStore.save( TokenDatabaseModel.serializer(), @@ -53,7 +53,7 @@ class DataStoreTest : TestsWithMocks() { } @Test - fun `When retrieving the token, it gets the correct key and value from settings`() = runTest { + fun `When retrieving the token- it gets the correct key and value from settings`() = runTest { settings.encodeValue(TokenDatabaseModel.serializer(), testKey, token) val dataStore = DataStoreImpl(settings) @@ -65,7 +65,7 @@ class DataStoreTest : TestsWithMocks() { } @Test - fun `When getting a key with no saved value, it returns the correct error`() = runTest { + fun `When getting a key with no saved value- it returns the correct error`() = runTest { val dataStore = DataStoreImpl(settings) dataStore.get(TokenDatabaseModel.serializer(), testKey) diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/NetworkClientTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/NetworkClientTest.kt index 3dea1bc9..42902902 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/NetworkClientTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/NetworkClientTest.kt @@ -30,7 +30,7 @@ class NetworkClientTest { } @Test - fun `when calling fetch, it returns correct object`() = runTest { + fun `when calling fetch- it returns correct object`() = runTest { val engine = jsonMockEngine(NETWORK_MOCK_MODEL_RESULT, path) val networkClient = NetworkClient(engine = engine) networkClient @@ -42,7 +42,7 @@ class NetworkClientTest { } @Test - fun `when calling fetchWithMeta, it returns correct object`() = runTest { + fun `when calling fetchWithMeta- it returns correct object`() = runTest { val engine = jsonMockEngine(NETWORK_META_MOCK_MODEL_RESULT, path) val networkClient = NetworkClient(engine = engine) networkClient @@ -56,7 +56,7 @@ class NetworkClientTest { } @Test - fun `when calling fetch with invalid path, it returns correct object`() = runTest { + fun `when calling fetch with invalid path- it returns correct object`() = runTest { val engine = jsonMockEngine(NETWORK_MOCK_MODEL_RESULT, "") val networkClient = NetworkClient(engine = engine) networkClient diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/TokenizedNetworkClientTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/TokenizedNetworkClientTest.kt index 4b31e018..8ffb0e09 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/TokenizedNetworkClientTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/network/core/TokenizedNetworkClientTest.kt @@ -48,7 +48,7 @@ class TokenizedNetworkClientTest { } @Test - fun `when calling fetch, it returns correct object`() = runTest { + fun `when calling fetch- it returns correct object`() = runTest { mocker.every { localDataSource.getToken() } returns flowOf(token) @@ -67,7 +67,7 @@ class TokenizedNetworkClientTest { } @Test - fun `when calling fetchWithMeta, it returns correct object`() = runTest { + fun `when calling fetchWithMeta- it returns correct object`() = runTest { mocker.every { localDataSource.getToken() } returns flowOf(token) @@ -88,7 +88,7 @@ class TokenizedNetworkClientTest { } @Test - fun `when calling fetch with incorrect token, it returns correct error`() = runTest { + fun `when calling fetch with incorrect token- it returns correct error`() = runTest { mocker.every { localDataSource.getToken() } returns flowOf(token) diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AccountRecoveryRepositoryTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AccountRecoveryRepositoryTest.kt index 5f5b645b..41aabff6 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AccountRecoveryRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AccountRecoveryRepositoryTest.kt @@ -32,7 +32,7 @@ class AccountRecoveryRepositoryTest { } @Test - fun `When calling reset with success response, it returns correct object`() = runTest { + fun `When calling reset with success response- it returns correct object`() = runTest { mocker.every { networkDataSource.resetPassword(isAny()) } returns flow { emit(resetPasswordMeta) } @@ -44,7 +44,7 @@ class AccountRecoveryRepositoryTest { } @Test - fun `When calling reset with failure response, it returns correct error`() = runTest { + fun `When calling reset with failure response- it returns correct error`() = runTest { mocker.every { networkDataSource.resetPassword(isAny()) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AuthenticationRepositoryTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AuthenticationRepositoryTest.kt index 80c77a05..f7415d24 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AuthenticationRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/AuthenticationRepositoryTest.kt @@ -38,7 +38,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() { } @Test - fun `When calling log in with success response, it returns correct object`() = runTest { + fun `When calling log in with success response- it returns correct object`() = runTest { mocker.every { networkDataSource.logIn(isAny()) } returns flow { emit(token) } @@ -53,7 +53,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() { } @Test - fun `When calling log in with failure response, it returns correct error`() = runTest { + fun `When calling log in with failure response- it returns correct error`() = runTest { mocker.every { networkDataSource.logIn(isAny()) } returns flow { error("Fail") } @@ -68,7 +68,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() { } @Test - fun `When logging in successfully, it saves data to localDataSource`() = runTest { + fun `When logging in successfully- it saves data to localDataSource`() = runTest { var saveCount = 0 mocker.every { networkDataSource.logIn(isAny()) @@ -87,7 +87,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() { } @Test - fun `When saving a token, it stores the correct key and value to localDataSource`() = runTest { + fun `When saving a token- it stores the correct key and value to localDataSource`() = runTest { var saveCount = 0 mocker.every { localDataSource.save(tokenDB) @@ -103,7 +103,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() { } @Test - fun `When calling getToken, it returns the correct value`() = runTest { + fun `When calling getToken- it returns the correct value`() = runTest { mocker.every { localDataSource.getToken() } returns flow { emit(tokenDB) } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryTest.kt index 2e85eee6..07bf6311 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/DeviceInfoRepositoryTest.kt @@ -10,13 +10,16 @@ import org.kodein.mock.UsesMocks import kotlin.test.BeforeTest import kotlin.test.Test +private const val TIME: Long = 100_000 + @UsesMocks(Clock::class) @ExperimentalCoroutinesApi class DeviceInfoRepositoryTest { + private val mocker = Mocker() private val clock = MockClock(mocker) - private val instant = Instant.DISTANT_PAST + private val instant = Instant.fromEpochSeconds(TIME) private val deviceInfoRepository = DeviceInfoRepositoryImpl(clock) @BeforeTest @@ -25,18 +28,14 @@ class DeviceInfoRepositoryTest { } @Test - fun `When calling profile with success response, it returns correct object`() = runTest { + fun `When calling profile with success response- it returns correct object`() = runTest { mocker.every { clock.now() } returns instant deviceInfoRepository .getCurrentDate() .test { - val item = awaitItem() - val local = instant.toLocalDateTime(TimeZone.currentSystemDefault()) - item.day shouldBe local.dayOfMonth - item.month shouldBe local.monthNumber - item.dayOfWeek shouldBe local.dayOfWeek.isoDayNumber + awaitItem().timeInterval shouldBe TIME awaitComplete() } } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryTest.kt index c083659f..c6eb87c8 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryTest.kt @@ -35,7 +35,7 @@ class SurveyRepositoryTest: TestsWithMocks() { } @Test - fun `When calling survey with success response, it returns correct object`() = runTest { + fun `When calling survey with success response- it returns correct object`() = runTest { mocker.every { networkDataSource.survey(isAny()) } returns flowOf(Pair(listOf(survey), meta)) @@ -50,7 +50,7 @@ class SurveyRepositoryTest: TestsWithMocks() { } @Test - fun `When calling survey with failure response, it returns correct error`() = runTest { + fun `When calling survey with failure response- it returns correct error`() = runTest { mocker.every { networkDataSource.survey(isAny()) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/UserRepositoryTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/UserRepositoryTest.kt index 424ff599..9f134974 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/UserRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/UserRepositoryTest.kt @@ -32,7 +32,7 @@ class UserRepositoryTest { } @Test - fun `When calling getProfile with success response, it returns correct object`() = runTest { + fun `When calling getProfile with success response- it returns correct object`() = runTest { mocker.every { networkDataSource.profile(isAny()) } returns flowOf(user) @@ -47,7 +47,7 @@ class UserRepositoryTest { } @Test - fun `When calling getProfile with failure response, it returns correct error`() = runTest { + fun `When calling getProfile with failure response- it returns correct error`() = runTest { mocker.every { networkDataSource.profile(isAny()) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCaseTest.kt index b62f88ed..2a18a33d 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCaseTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetCurrentDateUseCaseTest.kt @@ -1,7 +1,8 @@ package co.nimblehq.blisskmmic.domain.usecase import app.cash.turbine.test -import co.nimblehq.blisskmmic.domain.model.DateComponents +import co.nimblehq.blisskmmic.domain.model.DateComponent +import co.nimblehq.blisskmmic.domain.model.fakeDateComponent import co.nimblehq.blisskmmic.domain.repository.DeviceInfoRepository import co.nimblehq.blisskmmic.domain.repository.MockDeviceInfoRepository import io.kotest.matchers.shouldBe @@ -10,18 +11,20 @@ import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest import org.kodein.mock.Mocker +import org.kodein.mock.UsesFakes import org.kodein.mock.UsesMocks import kotlin.test.BeforeTest import kotlin.test.Test @UsesMocks(DeviceInfoRepository::class) +@UsesFakes(DateComponent::class) @ExperimentalCoroutinesApi class GetCurrentDateUseCaseTest { private val mocker = Mocker() private val deviceInfoRepository = MockDeviceInfoRepository(mocker) + private val dateComponent = fakeDateComponent() private val getCurrentDateUseCase = GetCurrentDateUseCaseImpl(deviceInfoRepository) - private val dateComponents = DateComponents(day = 2, month = 2, dayOfWeek = 3) @BeforeTest fun setUp() { @@ -29,23 +32,20 @@ class GetCurrentDateUseCaseTest { } @Test - fun `When calling getCurrent with a success response, it returns correct object`() = runTest { + fun `When calling getCurrent with a success response- it returns correct object`() = runTest { mocker.every { deviceInfoRepository.getCurrentDate() - } returns flowOf(dateComponents) + } returns flowOf(dateComponent) getCurrentDateUseCase() .test { - val item = awaitItem() - item.day shouldBe 2 - item.dayOfWeek shouldBe "WEDNESDAY" - item.month shouldBe "FEBRUARY" + awaitItem().timeInterval shouldBe dateComponent.timeInterval awaitComplete() } } @Test - fun `When calling getCurrent with a failure response, it returns correct error`() = runTest { + fun `When calling getCurrent with a failure response- it returns correct error`() = runTest { mocker.every { deviceInfoRepository.getCurrentDate() } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetProfileUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetProfileUseCaseTest.kt index d0984fa1..070e5df9 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetProfileUseCaseTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/GetProfileUseCaseTest.kt @@ -35,7 +35,7 @@ class GetProfileUseCaseTest { } @Test - fun `When calling getProfile with a success response, it returns correct object`() = runTest { + fun `When calling getProfile with a success response- it returns correct object`() = runTest { mocker.every { userRepository.getProfile() } returns flowOf(user) @@ -50,7 +50,7 @@ class GetProfileUseCaseTest { } @Test - fun `When calling getProfile with a failure response, it returns correct error`() = runTest { + fun `When calling getProfile with a failure response- it returns correct error`() = runTest { mocker.every { userRepository.getProfile() } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/LogInUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/LogInUseCaseTest.kt index f9cf8d63..683e5533 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/LogInUseCaseTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/LogInUseCaseTest.kt @@ -34,7 +34,7 @@ class LogInUseCaseTest : TestsWithMocks() { } @Test - fun `When calling log in with a success response, it returns correct object`() = runTest { + fun `When calling log in with a success response- it returns correct object`() = runTest { mocker.every { authenticationRepository.logIn(email, password) } returns flow { emit(token) } @@ -46,7 +46,7 @@ class LogInUseCaseTest : TestsWithMocks() { } @Test - fun `When calling log in with a failure response, it returns correct error`() = runTest { + fun `When calling log in with a failure response- it returns correct error`() = runTest { mocker.every { authenticationRepository.logIn(email, password) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/ResetPasswordUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/ResetPasswordUseCaseTest.kt index 1f0808de..ac6f821f 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/ResetPasswordUseCaseTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/ResetPasswordUseCaseTest.kt @@ -31,7 +31,7 @@ class ResetPasswordUseCaseTest { } @Test - fun `When calling reset with a success response, it returns correct object`() = runTest { + fun `When calling reset with a success response- it returns correct object`() = runTest { mocker.every { accountRecoveryRepository.resetPasswordWith(email) } returns flow { emit(resetPasswordMeta) } @@ -43,7 +43,7 @@ class ResetPasswordUseCaseTest { } @Test - fun `When calling reset with a failure response, it returns correct error`() = runTest { + fun `When calling reset with a failure response- it returns correct error`() = runTest { mocker.every { accountRecoveryRepository.resetPasswordWith(email) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt index a592e26a..69f49056 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt @@ -37,7 +37,7 @@ class SurveyListUseCaseTest { } @Test - fun `When calling surveyList with a success response, it returns correct object`() = runTest { + fun `When calling surveyList with a success response- it returns correct object`() = runTest { mocker.every { surveyRepository.survey(1) } returns flowOf(Pair(listOf(survey), paginationMeta)) @@ -50,7 +50,7 @@ class SurveyListUseCaseTest { } @Test - fun `When calling surveyList with a failure response, it returns correct error`() = runTest { + fun `When calling surveyList with a failure response- it returns correct error`() = runTest { mocker.every { surveyRepository.survey(1) } returns flow { error("Fail") } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/helpers/MokoLocalizeTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/helpers/MokoLocalizeTest.kt index 9f587691..5eb7d755 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/helpers/MokoLocalizeTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/helpers/MokoLocalizeTest.kt @@ -9,13 +9,13 @@ import kotlin.test.Test class MokoLocalizeTest { @Test - fun `When calling localize with common_error desc, it returns correct StringDesc`() { + fun `When calling localize with common_error desc- it returns correct StringDesc`() { MokoLocalize().localize(MR.strings.common_error.toString()) shouldBe StringDesc.Resource(MR.strings.common_error) } @Test - fun `When calling localize with an unknown desc, it returns null`() { + fun `When calling localize with an unknown desc- it returns null`() { MokoLocalize().localize("unknown") shouldBe null } } diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/login/LoginViewModelTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/login/LoginViewModelTest.kt index 0503bfeb..da2ebf01 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/login/LoginViewModelTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/login/LoginViewModelTest.kt @@ -46,7 +46,7 @@ class LoginViewModelTest : TestsWithMocks() { } @Test - fun `When calling log in with success response, it changes viewState to success`() = runTest { + fun `When calling log in with success response- it changes viewState to success`() = runTest { mocker.every { logInUseCase(email, password) } returns flow { emit(token) } @@ -61,7 +61,7 @@ class LoginViewModelTest : TestsWithMocks() { } @Test - fun `When calling log in with faliure response, it changes viewState to error`() = runTest { + fun `When calling log in with faliure response- it changes viewState to error`() = runTest { val errorMessage = "Test Error" mocker.every { logInUseCase(email, password) diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/resetpassword/ResetPasswordViewModelTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/resetpassword/ResetPasswordViewModelTest.kt index eddc7153..513ac77b 100644 --- a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/resetpassword/ResetPasswordViewModelTest.kt +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/presentation/modules/resetpassword/ResetPasswordViewModelTest.kt @@ -45,7 +45,7 @@ class ResetPasswordViewModelTest : TestsWithMocks() { } @Test - fun `When calling reset with success response, it changes viewState to success with correct item`() = runTest { + fun `When calling reset with success response- it changes viewState to success with correct item`() = runTest { mocker.every { resetPasswordUseCase(email) } returns flow { emit(resultMessage) } @@ -61,7 +61,7 @@ class ResetPasswordViewModelTest : TestsWithMocks() { } @Test - fun `When calling reset with faliure response, it changes viewState to error`() = runTest { + fun `When calling reset with faliure response- it changes viewState to error`() = runTest { val errorMessage = "Test Error" mocker.every { resetPasswordUseCase(email) diff --git a/shared/src/iosMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt b/shared/src/iosMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt index ab33609b..20434c79 100644 --- a/shared/src/iosMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt +++ b/shared/src/iosMain/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterImpl.kt @@ -2,13 +2,16 @@ package co.nimblehq.blisskmmic.domain.platform.datetime import platform.Foundation.NSDate +private const val REFERENCED_INTERVAL: Double = 978_307_200.0 + actual class DateTimeFormatterImpl: DateTimeFormatter { actual override fun getFormattedString( secondsSinceEpoch: Long, format: DateFormat ): String { - val date = NSDate(secondsSinceEpoch.toDouble()) + val realInterval = secondsSinceEpoch.toDouble() - REFERENCED_INTERVAL + val date = NSDate(realInterval) return DateFormatter.get(format).stringFromDate(date) } } diff --git a/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/IosGreetingTest.kt b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/IosGreetingTest.kt index 6e44c2e2..88073fb7 100644 --- a/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/IosGreetingTest.kt +++ b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/IosGreetingTest.kt @@ -7,6 +7,6 @@ class IosGreetingTest { @Test fun testExample() { - assertTrue(Greeting().greeting().contains("iOasdfasdfS"), "Check iOS is mentioned") + assertTrue(Greeting().greeting().contains("iOS"), "Check iOS is mentioned") } } diff --git a/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateFormatterTest.kt b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateFormatterTest.kt new file mode 100644 index 00000000..c95e036a --- /dev/null +++ b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateFormatterTest.kt @@ -0,0 +1,44 @@ +package co.nimblehq.blisskmmic.domain.platform.datetime + +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import kotlin.test.AfterTest +import kotlin.test.Test + +class DateFormatterTest { + + @AfterTest + fun tearDown() { + DateFormatter.dateFormatters.clear() + } + + @Test + fun `when init- dateFormatters should be empty`() { + DateFormatter.dateFormatters.count() shouldBe 0 + } + + @Test + fun `when get- dateFormatters should have value`() { + DateFormatter.get(DateFormat.DayOfWeekMonthDay) + DateFormatter.dateFormatters.count() shouldNotBe 0 + } + + @Test + fun `when get- it return a DateTimeFormatter`() { + DateFormatter.get(DateFormat.DayOfWeekMonthDay) shouldNotBe null + } + + @Test + fun `when get same date formatter twice- it returns the same object`() { + val first = DateFormatter.get(DateFormat.DayOfWeekMonthDay) + val second = DateFormatter.get(DateFormat.DayOfWeekMonthDay) + first shouldBe second + } + + @Test + fun `when get same date formatter twice- dateFormatters should have correct value`() { + DateFormatter.get(DateFormat.DayOfWeekMonthDay) + DateFormatter.get(DateFormat.DayOfWeekMonthDay) + DateFormatter.dateFormatters.count() shouldBe 1 + } +} diff --git a/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterTest.kt b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterTest.kt new file mode 100644 index 00000000..434d635c --- /dev/null +++ b/shared/src/iosTest/kotlin/co/nimblehq/blisskmmic/domain/platform/datetime/DateTimeFormatterTest.kt @@ -0,0 +1,24 @@ +package co.nimblehq.blisskmmic.domain.platform.datetime + +import io.kotest.matchers.shouldBe +import kotlin.test.Test + +private const val PAST_TIME_SINCE: Long = 1_000_000 +private const val TIME_SINCE: Long = 1_000_000_000 + +class DateTimeFormatterTest { + + private val dateTimeFormatter = DateTimeFormatterImpl() + + @Test + fun `when getFormattedString with DayOfWeekMonthDay- it returns correct string`() { + dateTimeFormatter.getFormattedString(TIME_SINCE, DateFormat.DayOfWeekMonthDay) shouldBe + "Sunday, September 9" + } + + @Test + fun `when getFormattedString with past date and DayOfWeekMonthDay- it returns correct string`() { + dateTimeFormatter.getFormattedString(PAST_TIME_SINCE, DateFormat.DayOfWeekMonthDay) shouldBe + "Monday, January 12" + } +}