Skip to content

Commit

Permalink
Make test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhBhat committed Dec 5, 2024
1 parent 7c1cbbd commit 727c9e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class ProductListHandlerTest : BaseUnitTest() {
on(it.observeProducts(any())) doReturn flow { emit(generateSampleProducts()) }

onBlocking {
(it.fetchProducts(any(), any(), any(), any()))
(it.fetchProducts(any(), any(), any(), any(), any()))
} doReturn Result.success(true)
}

Expand All @@ -34,7 +34,7 @@ internal class ProductListHandlerTest : BaseUnitTest() {

@Test
fun `when load invoked, then emits first 25 products from db`() = testBlocking {
whenever(repo.fetchProducts(any(), any(), any(), any())).doReturn(Result.success(true))
whenever(repo.fetchProducts(any(), any(), any(), any(), any())).doReturn(Result.success(true))
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)

Expand All @@ -51,17 +51,23 @@ internal class ProductListHandlerTest : BaseUnitTest() {
fun `when load invoked, then side fetches first 25 products from backend`() = testBlocking {
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)
verify(repo).fetchProducts(false, 0, 25, emptyMap())
verify(repo).fetchProducts(false, 0, 25, emptyMap(), emptyList())
}

@Test
fun `when load more invoked, then fetches next 25 products`() = testBlocking {
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)
handler.loadFromCacheAndFetch(
false,
"",
emptyMap(),
searchType = SearchType.DEFAULT,
emptyList()
)

handler.loadMore()

verify(repo).fetchProducts(false, 25, 25, emptyMap())
verify(repo).fetchProducts(false, 25, 25, emptyMap(), emptyList())

handler.productsFlow.test {
val products = awaitItem()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.woocommerce.android.ui.woopos.home.items

import com.woocommerce.android.ui.products.ProductStatus
import com.woocommerce.android.ui.products.ProductTestUtils
import com.woocommerce.android.ui.products.ProductType
import com.woocommerce.android.ui.products.selector.ProductListHandler
import com.woocommerce.android.ui.woopos.home.items.products.WooPosProductsDataSource
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
Expand All @@ -15,9 +13,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.wordpress.android.fluxc.store.WCProductStore
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.Test
import kotlin.test.assertFalse
Expand Down Expand Up @@ -70,14 +66,13 @@ class WooPosProductsDataSourceTest {
)

private val handler: ProductListHandler = mock()
private val isNonSimpleProductTypesEnabled: IsNonSimpleProductTypesEnabled = mock()

@Test
fun `given force refresh, when loadSimpleProducts called, then should clear cache`() = runTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
val sut = WooPosProductsDataSource(handler)

// Pre-populate the cache
sut.loadSimpleProducts(forceRefreshProducts = false).first()
Expand All @@ -101,8 +96,8 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
sut.loadSimpleProducts(forceRefreshProducts = false).first()
Expand All @@ -120,8 +115,8 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
sut.loadSimpleProducts(forceRefreshProducts = false).first()
Expand All @@ -143,14 +138,16 @@ class WooPosProductsDataSourceTest {
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
val exception = Exception("Remote load failed")
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))

val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
val sut = WooPosProductsDataSource(handler)

// Prepopulate the cache by calling loadSimpleProducts once
sut.loadSimpleProducts(forceRefreshProducts = false).first()

whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.failure(exception))
whenever(
handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())
).thenReturn(Result.failure(exception))

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand All @@ -174,7 +171,7 @@ class WooPosProductsDataSourceTest {
flowOf(sampleProducts + additionalProducts)
)
whenever(handler.loadMore()).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
val sut = WooPosProductsDataSource(handler)

sut.loadSimpleProducts(forceRefreshProducts = false).first()

Expand All @@ -199,7 +196,7 @@ class WooPosProductsDataSourceTest {
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
val exception = Exception("Load more failed")
whenever(handler.loadMore()).thenReturn(Result.failure(exception))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
val sut = WooPosProductsDataSource(handler)

sut.loadSimpleProducts(forceRefreshProducts = false).first()

Expand All @@ -223,9 +220,11 @@ class WooPosProductsDataSourceTest {
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(emptyList()))
val exception = Exception("Remote load failed")
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.failure(exception))
whenever(
handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())
).thenReturn(Result.failure(exception))

val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand All @@ -245,8 +244,8 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(emptyList()))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand Down Expand Up @@ -285,8 +284,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand Down Expand Up @@ -322,8 +321,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = true).toList()
Expand Down Expand Up @@ -360,8 +359,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand Down Expand Up @@ -399,8 +398,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = true).toList()
Expand Down Expand Up @@ -436,8 +435,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand Down Expand Up @@ -474,8 +473,8 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler)

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = true).toList()
Expand All @@ -484,47 +483,4 @@ class WooPosProductsDataSourceTest {
val remoteResult = flow[1] as WooPosProductsDataSource.ProductsResult.Remote
assertThat(remoteResult.productsResult.getOrNull()?.any { it.remoteId == 1L }).isFalse()
}

@Test
fun `given non-simple product types feature disabled, when loadSimpleProducts called, then add filter to display only simple products`() = runTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(isNonSimpleProductTypesEnabled.invoke()).thenReturn(false)
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
sut.loadSimpleProducts(forceRefreshProducts = true).first()

// THEN
verify(handler).loadFromCacheAndFetch(
forceRefresh = true,
searchType = ProductListHandler.SearchType.DEFAULT,
filters = mapOf(
WCProductStore.ProductFilterOption.TYPE to ProductType.SIMPLE.value,
WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value
)
)
}

@Test
fun `given non-simple product types feature enabled, when loadSimpleProducts called, then do not add filter to display only simple products`() = runTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(isNonSimpleProductTypesEnabled.invoke()).thenReturn(true)
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
sut.loadSimpleProducts(forceRefreshProducts = true).first()

// THEN
verify(handler).loadFromCacheAndFetch(
forceRefresh = true,
searchType = ProductListHandler.SearchType.DEFAULT,
filters = mapOf(
WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value
)
)
}
}

0 comments on commit 727c9e7

Please sign in to comment.