forked from philipplackner/IvyWallet-PL_Version
-
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.
- Loading branch information
1 parent
c25605b
commit 14a3341
Showing
6 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
common/android-test/src/main/java/com/ivy/common/androidtest/MainCoroutineRule.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,21 @@ | ||
package com.ivy.common.androidtest | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.* | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class MainCoroutineRule( | ||
val testDispatcher: TestDispatcher = StandardTestDispatcher() | ||
) : TestWatcher() { | ||
|
||
override fun starting(description: Description?) { | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
|
||
override fun finished(description: Description?) { | ||
Dispatchers.resetMain() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
common/android-test/src/main/java/com/ivy/common/androidtest/TestCommonModuleDI.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,21 @@ | ||
package com.ivy.common.androidtest | ||
|
||
import com.ivy.common.di.CommonModuleDI | ||
import com.ivy.common.time.provider.TimeProvider | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.hilt.testing.TestInstallIn | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@TestInstallIn( | ||
components = [SingletonComponent::class], | ||
replaces = [CommonModuleDI::class] | ||
) | ||
abstract class TestCommonModuleDI { | ||
@Binds | ||
abstract fun timeProvider(provider: TimeProviderFake): TimeProvider | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
common/android-test/src/main/java/com/ivy/common/androidtest/TimeProviderFake.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,28 @@ | ||
package com.ivy.common.androidtest | ||
|
||
import com.ivy.common.time.provider.TimeProvider | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class TimeProviderFake @Inject constructor(): TimeProvider { | ||
|
||
var timeNow = LocalDateTime.now() | ||
var dateNow = LocalDate.now() | ||
var zoneId = ZoneId.systemDefault() | ||
|
||
override fun timeNow(): LocalDateTime { | ||
return timeNow | ||
} | ||
|
||
override fun dateNow(): LocalDate { | ||
return dateNow | ||
} | ||
|
||
override fun zoneId(): ZoneId { | ||
return zoneId | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
core/ui/src/androidTest/java/com/ivy/core/ui/time/picker/date/DatePickerViewModelTest.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,55 @@ | ||
package com.ivy.core.ui.time.picker.date | ||
|
||
import app.cash.turbine.test | ||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import com.ivy.common.androidtest.IvyAndroidTest | ||
import com.ivy.common.androidtest.MainCoroutineRule | ||
import com.ivy.core.ui.time.picker.date.data.PickerDay | ||
import com.ivy.core.ui.time.picker.date.data.PickerMonth | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.time.LocalDate | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@HiltAndroidTest | ||
class DatePickerViewModelTest: IvyAndroidTest() { | ||
|
||
@get:Rule | ||
val mainCoroutineRule = MainCoroutineRule() | ||
|
||
private lateinit var viewModel: DatePickerViewModel | ||
|
||
override fun setUp() { | ||
super.setUp() | ||
viewModel = DatePickerViewModel( | ||
appContext = context, | ||
timeProvider = timeProvider | ||
) | ||
} | ||
|
||
@Test | ||
fun testSelectingDate() = runTest { | ||
// Making sure, the test runs with a month != February, so | ||
// February can be selected | ||
setDate(LocalDate.of(2023, 1, 1)) | ||
viewModel.uiState.test { | ||
awaitItem() // Skip initial emission | ||
|
||
viewModel.onEvent(DatePickerEvent.DayChange(PickerDay("30", 30))) | ||
awaitItem() // Skip day emission | ||
|
||
viewModel.onEvent(DatePickerEvent.MonthChange(PickerMonth("Feb", 2))) | ||
|
||
val finalEmission = awaitItem() | ||
|
||
val timeProviderDate = timeProvider.dateNow() | ||
assertThat(finalEmission.selected).isEqualTo( | ||
LocalDate.of(timeProviderDate.year, 2, 28) | ||
) | ||
} | ||
} | ||
} |