Skip to content

Commit

Permalink
Merge pull request #12615 from woocommerce/12550-woo-pos-orders-creat…
Browse files Browse the repository at this point in the history
…ed-in-pos-dont-show-product-names-when-reviewed-in-ipp-later-on

[Woo POS] Ensure Product Names are Displayed Correctly
samiuelson authored Sep 19, 2024
2 parents 6ea261b + 613d878 commit cd298ee
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package com.woocommerce.android.ui.woopos.home.totals

import com.woocommerce.android.model.Order
import com.woocommerce.android.ui.orders.creation.OrderCreateEditRepository
import com.woocommerce.android.ui.woopos.common.data.WooPosGetProductById
import com.woocommerce.android.util.DateUtils
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers.IO
@@ -13,6 +14,7 @@ import javax.inject.Inject
class WooPosTotalsRepository @Inject constructor(
private val orderCreateEditRepository: OrderCreateEditRepository,
private val dateUtils: DateUtils,
private val getProductById: WooPosGetProductById
) {
private var orderCreationJob: Deferred<Result<Order>>? = null

@@ -36,6 +38,8 @@ class WooPosTotalsRepository @Inject constructor(
.groupingBy { it }
.eachCount()
.map { (productId, quantity) ->
val productResult = getProductById(productId)!!

Order.Item.EMPTY.copy(
itemId = 0L,
productId = productId,
@@ -44,6 +48,7 @@ class WooPosTotalsRepository @Inject constructor(
total = EMPTY_TOTALS_SUBTOTAL_VALUE,
subtotal = EMPTY_TOTALS_SUBTOTAL_VALUE,
attributesList = emptyList(),
name = productResult.name,
)
}
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.woocommerce.android.ui.woopos.home.totals

import com.woocommerce.android.model.Order
import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.orders.creation.OrderCreateEditRepository
import com.woocommerce.android.ui.products.ProductBackorderStatus
import com.woocommerce.android.ui.products.ProductStockStatus
import com.woocommerce.android.ui.products.ProductTaxStatus
import com.woocommerce.android.ui.woopos.common.data.WooPosGetProductById
import com.woocommerce.android.util.DateUtils
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
@@ -13,19 +18,47 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.math.BigDecimal
import java.util.Date

class WooPosTotalsRepositoryTest {
private val orderCreateEditRepository: OrderCreateEditRepository = mock()
private val getProductById: WooPosGetProductById = mock()

private val dateUtils: DateUtils = mock()

private lateinit var repository: WooPosTotalsRepository

private val product1 = Product(
remoteId = 1L, name = "Product 1", price = BigDecimal(10),
sku = "SKU1", attributes = emptyList(),
parentId = 0L, description = "", shortDescription = "", slug = "", type = "",
status = null, catalogVisibility = null, isFeatured = false,
stockStatus = ProductStockStatus.InsufficientStock, backorderStatus = ProductBackorderStatus.No,
dateCreated = Date(), firstImageUrl = null, totalSales = 0L, reviewsAllowed = false,
isVirtual = false, ratingCount = 0, averageRating = 0.0f, permalink = "", externalUrl = "",
buttonText = "", salePrice = BigDecimal.ZERO, regularPrice = BigDecimal.ZERO,
taxClass = "", isStockManaged = false, stockQuantity = 0.0, shippingClass = "",
shippingClassId = 0L, isDownloadable = false, downloads = emptyList(),
downloadLimit = 0L, downloadExpiry = 0, purchaseNote = "", numVariations = 0,
images = emptyList(), saleEndDateGmt = null, saleStartDateGmt = null,
isSoldIndividually = false, taxStatus = ProductTaxStatus.Taxable,
isSaleScheduled = false, isPurchasable = false, menuOrder = 0, categories = emptyList(),
tags = emptyList(), groupedProductIds = emptyList(), crossSellProductIds = emptyList(),
upsellProductIds = emptyList(), variationIds = emptyList(), length = 0f, width = 0f,
height = 0f, weight = 0f, subscription = null, isSampleProduct = false, specialStockStatus = null,
isConfigurable = false, minAllowedQuantity = null, maxAllowedQuantity = null,
bundleMinSize = null, bundleMaxSize = null, groupOfQuantity = null,
combineVariationQuantities = null
)

@Test
fun `given empty product list, when createOrderWithProducts called, then return error`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
dateUtils,
getProductById
)
val productIds = emptyList<Long>()

@@ -41,10 +74,15 @@ class WooPosTotalsRepositoryTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
dateUtils,
getProductById
)
val productIds = listOf(1L, 2L, 3L)

whenever(getProductById(1L)).thenReturn(product1)
whenever(getProductById(2L)).thenReturn(product1)
whenever(getProductById(3L)).thenReturn(product1)

// WHEN
repository.createOrderWithProducts(productIds = productIds)

@@ -57,17 +95,49 @@ class WooPosTotalsRepositoryTest {

assertThat(orderCapture.lastValue.items.size).isEqualTo(3)
assertThat(orderCapture.lastValue.items.map { it.quantity }).containsOnly(1f)
assertThat(orderCapture.lastValue.items[0].name).isEqualTo(product1.name)
}

@Test
fun `given product id, when createOrderWithProducts, then item name matches original product`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils,
getProductById
)
val productIds = listOf(1L)

whenever(getProductById(1L)).thenReturn(product1)

// WHEN
repository.createOrderWithProducts(productIds = productIds)

// THEN
val orderCapture = argumentCaptor<Order>()
verify(orderCreateEditRepository).createOrUpdateOrder(
orderCapture.capture(),
eq("")
)

assertThat(orderCapture.lastValue.items.size).isEqualTo(1)
assertThat(orderCapture.lastValue.items[0].name).isEqualTo(product1.name)
}

@Test
fun `given product ids with duplicates, when createOrderWithProducts, then items quantity is correct`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
dateUtils,
getProductById
)
val productIds = listOf(1L, 1L, 2L, 3L, 3L, 3L)

whenever(getProductById(1L)).thenReturn(product1)
whenever(getProductById(2L)).thenReturn(product1)
whenever(getProductById(3L)).thenReturn(product1)

// WHEN
repository.createOrderWithProducts(productIds = productIds)

@@ -87,7 +157,8 @@ class WooPosTotalsRepositoryTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
dateUtils,
getProductById
)
val productIds = listOf(1L, -1L, 3L)
val mockOrder: Order = mock()

0 comments on commit cd298ee

Please sign in to comment.