Skip to content

Commit

Permalink
Add unit test to verify that Error state is displayed with reloading …
Browse files Browse the repository at this point in the history
…property set to true when pull to refreshed
  • Loading branch information
AnirudhBhat committed Nov 11, 2024
1 parent 925ee86 commit 109f7b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,24 @@ class WooPosVariationsViewModel @Inject constructor(
fetchJob = viewModelScope.launch {
val product = getProductById(productId)

variationsDataSource.fetchVariations(productId, forceRefresh = true)

variationsDataSource.getVariationsFlow(productId).collect { variationList ->
_viewState.value = WooPosVariationsViewState.Content(
items = variationList.map {
WooPosItem.Variation(
id = it.remoteVariationId,
name = it.getName(product),
price = it.priceWithCurrency ?: "",
imageUrl = it.image?.source
)
},
loadingMore = false,
reloadingProductsWithPullToRefresh = false,
)
val result = variationsDataSource.fetchVariations(productId, forceRefresh = true)
if (result.isSuccess) {
variationsDataSource.getVariationsFlow(productId).collect { variationList ->
_viewState.value = WooPosVariationsViewState.Content(
items = variationList.map {
WooPosItem.Variation(
id = it.remoteVariationId,
name = it.getName(product),
price = it.priceWithCurrency ?: "",
imageUrl = it.image?.source
)
},
loadingMore = false,
reloadingProductsWithPullToRefresh = false,
)
}
} else {
_viewState.value = WooPosVariationsViewState.Error()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woocommerce.android.ui.woopos.home.variations

import androidx.lifecycle.asLiveData
import app.cash.turbine.test
import com.woocommerce.android.ui.products.ProductTestUtils
import com.woocommerce.android.ui.woopos.common.data.WooPosGetProductById
Expand All @@ -12,6 +13,9 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -181,4 +185,30 @@ class WooPosVariationsViewModelTest {
assertTrue(value.reloadingProductsWithPullToRefresh)
}
}

@Test
fun `given view state is Error, when pull to refreshed, then view state is updated with proper variation content`() = runTest {
val testDispatcher = StandardTestDispatcher(testScheduler)

val testScope = TestScope(testDispatcher)

testScope.launch {
whenever(getProductById.invoke(any())).thenReturn(
ProductTestUtils.generateProduct(1L, isVariable = true, productType = "variable")
)
whenever(variationsDataSource.fetchVariations(any(), any())).thenReturn(
Result.failure(Throwable())
)
wooPosVariationsViewModel = WooPosVariationsViewModel(getProductById, variationsDataSource)
wooPosVariationsViewModel.init(1L)
wooPosVariationsViewModel.onUIEvent(WooPosVariationsUIEvents.PullToRefreshTriggered(1L))

wooPosVariationsViewModel.viewState.test {
// THEN
val value = awaitItem() as WooPosVariationsViewState.Error
assertThat(value).isInstanceOf(WooPosVariationsViewState.Error::class.java)
assertTrue(value.reloadingProductsWithPullToRefresh)
}
}
}
}

0 comments on commit 109f7b9

Please sign in to comment.