Skip to content

Commit a336fd3

Browse files
committed
Merge branch 'unit-test-cleanup' into main
2 parents 61c675a + 5e3e8ac commit a336fd3

File tree

6 files changed

+210
-65
lines changed

6 files changed

+210
-65
lines changed

app/src/androidTest/java/dev/shorthouse/coinwatch/data/UserPreferencesRepositoryTest.kt

+74-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import androidx.datastore.core.DataStoreFactory
55
import androidx.datastore.dataStoreFile
66
import androidx.test.core.app.ApplicationProvider
77
import com.google.common.truth.Truth.assertThat
8+
import dev.shorthouse.coinwatch.data.datastore.CoinSort
89
import dev.shorthouse.coinwatch.data.datastore.Currency
9-
import dev.shorthouse.coinwatch.data.datastore.UserPreferences
1010
import dev.shorthouse.coinwatch.data.datastore.UserPreferencesRepository
1111
import dev.shorthouse.coinwatch.data.datastore.UserPreferencesSerializer
1212
import kotlinx.coroutines.Dispatchers
@@ -53,54 +53,112 @@ class UserPreferencesRepositoryTest {
5353
@Test
5454
fun when_setCurrencyToUSD_should_updateUserPreferencesCurrencyToUSD() =
5555
testCoroutineScope.runTest {
56-
val expectedUserPreferences = UserPreferences(
57-
currency = Currency.USD
58-
)
56+
val currency = Currency.USD
5957

6058
userPreferencesRepository.updateCurrency(
61-
currency = Currency.USD
59+
currency = currency
6260
)
6361

6462
val userPreferences = userPreferencesRepository
6563
.userPreferencesFlow
6664
.first()
6765

68-
assertThat(userPreferences).isEqualTo(expectedUserPreferences)
66+
assertThat(userPreferences.currency).isEqualTo(currency)
6967
}
7068

7169
@Test
7270
fun when_setCurrencyToGBP_should_updateUserPreferencesCurrencyToGBP() =
7371
testCoroutineScope.runTest {
74-
val expectedUserPreferences = UserPreferences(
75-
currency = Currency.GBP
76-
)
72+
val currency = Currency.GBP
7773

7874
userPreferencesRepository.updateCurrency(
79-
currency = Currency.GBP
75+
currency = currency
8076
)
8177

8278
val userPreferences = userPreferencesRepository
8379
.userPreferencesFlow
8480
.first()
8581

86-
assertThat(userPreferences).isEqualTo(expectedUserPreferences)
82+
assertThat(userPreferences.currency).isEqualTo(currency)
8783
}
8884

8985
@Test
9086
fun when_setCurrencyToEUR_should_updateUserPreferencesCurrencyToEUR() =
9187
testCoroutineScope.runTest {
92-
val expectedUserPreferences = UserPreferences(
93-
currency = Currency.EUR
94-
)
88+
val currency = Currency.EUR
9589

9690
userPreferencesRepository.updateCurrency(
97-
currency = Currency.EUR
91+
currency = currency
92+
)
93+
94+
val userPreferences = userPreferencesRepository
95+
.userPreferencesFlow
96+
.first()
97+
98+
assertThat(userPreferences.currency).isEqualTo(currency)
99+
}
100+
101+
@Test
102+
fun when_setCoinSortToMarketCap_should_updateUserPreferencesCoinSortToMarketCap() =
103+
testCoroutineScope.runTest {
104+
val coinSort = CoinSort.MarketCap
105+
106+
userPreferencesRepository.updateCoinSort(
107+
coinSort = coinSort
108+
)
109+
110+
val userPreferences = userPreferencesRepository
111+
.userPreferencesFlow
112+
.first()
113+
114+
assertThat(userPreferences.coinSort).isEqualTo(coinSort)
115+
}
116+
117+
@Test
118+
fun when_setCoinSortToPrice_should_updateUserPreferencesCoinSortToPrice() =
119+
testCoroutineScope.runTest {
120+
val coinSort = CoinSort.Price
121+
122+
userPreferencesRepository.updateCoinSort(
123+
coinSort = coinSort
124+
)
125+
126+
val userPreferences = userPreferencesRepository
127+
.userPreferencesFlow
128+
.first()
129+
130+
assertThat(userPreferences.coinSort).isEqualTo(coinSort)
131+
}
132+
133+
@Test
134+
fun when_setCoinSortToPriceChange_should_updateUserPreferencesCoinSortToPriceChange() =
135+
testCoroutineScope.runTest {
136+
val coinSort = CoinSort.PriceChange24h
137+
138+
userPreferencesRepository.updateCoinSort(
139+
coinSort = coinSort
140+
)
141+
142+
val userPreferences = userPreferencesRepository
143+
.userPreferencesFlow
144+
.first()
145+
146+
assertThat(userPreferences.coinSort).isEqualTo(coinSort)
147+
}
148+
149+
@Test
150+
fun when_setCoinSortToVolume_should_updateUserPreferencesCoinSortToVolume() =
151+
testCoroutineScope.runTest {
152+
val coinSort = CoinSort.Volume24h
153+
154+
userPreferencesRepository.updateCoinSort(
155+
coinSort = coinSort
98156
)
99157

100158
val userPreferences = userPreferencesRepository
101159
.userPreferencesFlow
102160
.first()
103161

104-
assertThat(userPreferences).isEqualTo(expectedUserPreferences)
162+
assertThat(userPreferences.coinSort).isEqualTo(coinSort)
105163
}
106164
}

app/src/main/java/dev/shorthouse/coinwatch/ui/screen/market/MarketScreen.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fun MarketScreen(
8787
viewModel.updateCurrency(currency)
8888
},
8989
onUpdateShowCurrencyBottomSheet = { showSheet ->
90-
viewModel.onUpdateShowCurrencyBottomSheet(showSheet)
90+
viewModel.updateShowCurrencyBottomSheet(showSheet)
9191
},
9292
onRefresh = {
9393
viewModel.pullRefreshCachedCoins()

app/src/main/java/dev/shorthouse/coinwatch/ui/screen/market/MarketViewModel.kt

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.shorthouse.coinwatch.ui.screen.market
22

33
import androidx.annotation.StringRes
4+
import androidx.annotation.VisibleForTesting
45
import androidx.lifecycle.ViewModel
56
import androidx.lifecycle.viewModelScope
67
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -45,20 +46,6 @@ class MarketViewModel @Inject constructor(
4546
}
4647

4748
fun initialiseUiState() {
48-
getUserPreferencesUseCase().onEach { userPreferences ->
49-
_uiState.update {
50-
it.copy(
51-
coinSort = userPreferences.coinSort,
52-
currency = userPreferences.currency
53-
)
54-
}
55-
56-
refreshCachedCoins(
57-
coinSort = userPreferences.coinSort,
58-
currency = userPreferences.currency
59-
)
60-
}.launchIn(viewModelScope)
61-
6249
getCachedCoinsUseCase().onEach { coinsResult ->
6350
when (coinsResult) {
6451
is Result.Success -> {
@@ -84,13 +71,23 @@ class MarketViewModel @Inject constructor(
8471
}
8572
}.launchIn(viewModelScope)
8673

87-
getCurrentHourFlow().onEach { currentHour ->
88-
val timeOfDay = when (currentHour) {
89-
in 4..11 -> TimeOfDay.Morning
90-
in 12..17 -> TimeOfDay.Afternoon
91-
else -> TimeOfDay.Evening
74+
getUserPreferencesUseCase().onEach { userPreferences ->
75+
_uiState.update {
76+
it.copy(
77+
coinSort = userPreferences.coinSort,
78+
currency = userPreferences.currency
79+
)
9280
}
9381

82+
refreshCachedCoins(
83+
coinSort = userPreferences.coinSort,
84+
currency = userPreferences.currency
85+
)
86+
}.launchIn(viewModelScope)
87+
88+
getCurrentHourFlow().onEach { currentHour ->
89+
val timeOfDay = calculateTimeOfDay(hour = currentHour)
90+
9491
_uiState.update {
9592
it.copy(timeOfDay = timeOfDay)
9693
}
@@ -137,7 +134,7 @@ class MarketViewModel @Inject constructor(
137134
_uiState.update { it.copy(showCoinSortBottomSheet = showSheet) }
138135
}
139136

140-
fun onUpdateShowCurrencyBottomSheet(showSheet: Boolean) {
137+
fun updateShowCurrencyBottomSheet(showSheet: Boolean) {
141138
if (showSheet && isAnyBottomSheetOpen()) return
142139

143140
_uiState.update { it.copy(showCurrencyBottomSheet = showSheet) }
@@ -164,6 +161,15 @@ class MarketViewModel @Inject constructor(
164161
}
165162
}
166163

164+
@VisibleForTesting
165+
fun calculateTimeOfDay(hour: Int): TimeOfDay {
166+
return when (hour) {
167+
in 4..11 -> TimeOfDay.Morning
168+
in 12..17 -> TimeOfDay.Afternoon
169+
else -> TimeOfDay.Evening
170+
}
171+
}
172+
167173
private suspend fun refreshCachedCoins(coinSort: CoinSort, currency: Currency) {
168174
val result = refreshCachedCoinsUseCase(coinSort = coinSort, currency = currency)
169175

app/src/test/java/dev/shorthouse/coinwatch/ui/screen/details/CoinDetailsViewModelTest.kt app/src/test/java/dev/shorthouse/coinwatch/ui/screen/details/DetailsViewModelTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.junit.Before
3030
import org.junit.Rule
3131
import org.junit.Test
3232

33-
class CoinDetailsViewModelTest {
33+
class DetailsViewModelTest {
3434

3535
@get:Rule
3636
val mainDispatcherRule = MainDispatcherRule()

0 commit comments

Comments
 (0)