|
| 1 | +package dev.shorthouse.coinwatch.data |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.datastore.core.DataStoreFactory |
| 5 | +import androidx.datastore.dataStoreFile |
| 6 | +import androidx.test.core.app.ApplicationProvider |
| 7 | +import com.google.common.truth.Truth.assertThat |
| 8 | +import dev.shorthouse.coinwatch.data.datastore.Currency |
| 9 | +import dev.shorthouse.coinwatch.data.datastore.UserPreferences |
| 10 | +import dev.shorthouse.coinwatch.data.datastore.UserPreferencesRepository |
| 11 | +import dev.shorthouse.coinwatch.data.datastore.UserPreferencesSerializer |
| 12 | +import kotlinx.coroutines.Dispatchers |
| 13 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 14 | +import kotlinx.coroutines.Job |
| 15 | +import kotlinx.coroutines.cancel |
| 16 | +import kotlinx.coroutines.flow.first |
| 17 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 18 | +import kotlinx.coroutines.test.TestScope |
| 19 | +import kotlinx.coroutines.test.resetMain |
| 20 | +import kotlinx.coroutines.test.runTest |
| 21 | +import kotlinx.coroutines.test.setMain |
| 22 | +import org.junit.After |
| 23 | +import org.junit.Before |
| 24 | +import org.junit.Test |
| 25 | + |
| 26 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 27 | +class UserPreferencesRepositoryTest { |
| 28 | + |
| 29 | + private val testContext: Context = ApplicationProvider.getApplicationContext() |
| 30 | + private val testCoroutineDispatcher = StandardTestDispatcher() |
| 31 | + private val testCoroutineScope = TestScope(testCoroutineDispatcher + Job()) |
| 32 | + |
| 33 | + private val testDataStore = DataStoreFactory.create( |
| 34 | + serializer = UserPreferencesSerializer, |
| 35 | + scope = testCoroutineScope, |
| 36 | + produceFile = { testContext.dataStoreFile("test_datastore") } |
| 37 | + ) |
| 38 | + |
| 39 | + // Class under test |
| 40 | + private val userPreferencesRepository = UserPreferencesRepository(testDataStore) |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setup() { |
| 44 | + Dispatchers.setMain(testCoroutineDispatcher) |
| 45 | + } |
| 46 | + |
| 47 | + @After |
| 48 | + fun cleanup() { |
| 49 | + Dispatchers.resetMain() |
| 50 | + testCoroutineScope.cancel() |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun when_setCurrencyToUSD_should_updateUserPreferencesCurrencyToUSD() = |
| 55 | + testCoroutineScope.runTest { |
| 56 | + val expectedUserPreferences = UserPreferences( |
| 57 | + currency = Currency.USD |
| 58 | + ) |
| 59 | + |
| 60 | + userPreferencesRepository.updateCurrency( |
| 61 | + currency = Currency.USD |
| 62 | + ) |
| 63 | + |
| 64 | + val userPreferences = userPreferencesRepository |
| 65 | + .userPreferencesFlow |
| 66 | + .first() |
| 67 | + |
| 68 | + assertThat(userPreferences).isEqualTo(expectedUserPreferences) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun when_setCurrencyToGBP_should_updateUserPreferencesCurrencyToGBP() = |
| 73 | + testCoroutineScope.runTest { |
| 74 | + val expectedUserPreferences = UserPreferences( |
| 75 | + currency = Currency.GBP |
| 76 | + ) |
| 77 | + |
| 78 | + userPreferencesRepository.updateCurrency( |
| 79 | + currency = Currency.GBP |
| 80 | + ) |
| 81 | + |
| 82 | + val userPreferences = userPreferencesRepository |
| 83 | + .userPreferencesFlow |
| 84 | + .first() |
| 85 | + |
| 86 | + assertThat(userPreferences).isEqualTo(expectedUserPreferences) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun when_setCurrencyToEUR_should_updateUserPreferencesCurrencyToEUR() = |
| 91 | + testCoroutineScope.runTest { |
| 92 | + val expectedUserPreferences = UserPreferences( |
| 93 | + currency = Currency.EUR |
| 94 | + ) |
| 95 | + |
| 96 | + userPreferencesRepository.updateCurrency( |
| 97 | + currency = Currency.EUR |
| 98 | + ) |
| 99 | + |
| 100 | + val userPreferences = userPreferencesRepository |
| 101 | + .userPreferencesFlow |
| 102 | + .first() |
| 103 | + |
| 104 | + assertThat(userPreferences).isEqualTo(expectedUserPreferences) |
| 105 | + } |
| 106 | +} |
0 commit comments