Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Oct 9, 2024
1 parent 8967681 commit 035628d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.woocommerce.android.ui.products

import com.woocommerce.android.model.Product
import com.woocommerce.android.model.ProductAggregate
import com.woocommerce.android.model.ProductVariation
import com.woocommerce.android.ui.products.details.ProductDetailRepository
import com.woocommerce.android.ui.products.variations.VariationRepository
Expand Down Expand Up @@ -41,9 +42,9 @@ class DuplicateProductTest : BaseUnitTest() {
@Test
fun `should duplicate a product and set expected properties`() = testBlocking {
// given
val productToDuplicate = ProductTestUtils.generateProduct().copy(sku = "not an empty value")
val productToDuplicate = ProductAggregate(ProductTestUtils.generateProduct().copy(sku = "not an empty value"))
productDetailRepository.stub {
onBlocking { addProduct(any()) } doReturn Pair(true, 123)
onBlocking { addProduct(any<ProductAggregate>()) } doReturn Pair(true, 123)
}

// when
Expand All @@ -66,10 +67,10 @@ class DuplicateProductTest : BaseUnitTest() {
fun `should duplicate a variable product and keep all properties of variations except sku and remoteProductId`() =
testBlocking {
// given
val productToDuplicate = ProductTestUtils.generateProduct().copy(numVariations = 15)
val productToDuplicate = ProductAggregate(ProductTestUtils.generateProduct().copy(numVariations = 15))
val duplicatedProductId = 456L
productDetailRepository.stub {
onBlocking { addProduct(any()) } doReturn Pair(true, duplicatedProductId)
onBlocking { addProduct(any<ProductAggregate>()) } doReturn Pair(true, duplicatedProductId)
}

val variationsOfProductToDuplicate =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {
fun `Display success message on add product success`() = testBlocking {
// given
doReturn(ProductAggregate(product)).whenever(productRepository).getProductAggregate(any())
doReturn(Pair(true, 1L)).whenever(productRepository).addProduct(any())
doReturn(Pair(true, 1L)).whenever(productRepository).addProduct(any<ProductAggregate>())

var successSnackbarShown = false
viewModel.event.observeForever {
Expand Down Expand Up @@ -256,7 +256,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {
@Test
fun `Display error message on add product failed`() = testBlocking {
// given
doReturn(Pair(false, 0L)).whenever(productRepository).addProduct(any())
doReturn(Pair(false, 0L)).whenever(productRepository).addProduct(any<ProductAggregate>())

var successSnackbarShown = false
viewModel.event.observeForever {
Expand Down Expand Up @@ -310,7 +310,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {
testBlocking {
// given
doReturn(ProductAggregate(product)).whenever(productRepository).getProductAggregate(any())
doReturn(Pair(true, 1L)).whenever(productRepository).addProduct(any())
doReturn(Pair(true, 1L)).whenever(productRepository).addProduct(any<ProductAggregate>())

var successSnackbarShown = false
viewModel.event.observeForever {
Expand Down Expand Up @@ -399,7 +399,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {

@Test
fun `when a new product is saved, then assign the new id to ongoing image uploads`() = testBlocking {
doReturn(Pair(true, PRODUCT_REMOTE_ID)).whenever(productRepository).addProduct(any())
doReturn(Pair(true, PRODUCT_REMOTE_ID)).whenever(productRepository).addProduct(any<ProductAggregate>())
doReturn(product).whenever(productRepository).getProductAggregate(any())
savedState = ProductDetailFragmentArgs(
mode = ProductDetailFragment.Mode.AddNewProduct
Expand Down Expand Up @@ -454,7 +454,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {
@Test
fun `given a product is under creation, when clicking on save product, then assign uploads to the new id`() =
testBlocking {
doReturn(Pair(true, PRODUCT_REMOTE_ID)).whenever(productRepository).addProduct(any())
doReturn(Pair(true, PRODUCT_REMOTE_ID)).whenever(productRepository).addProduct(any<ProductAggregate>())
doReturn(product).whenever(productRepository).getProductAggregate(any())
viewModel.productDetailViewStateData.observeForever { _, _ -> }

Expand Down Expand Up @@ -494,7 +494,7 @@ class ProductDetailViewModel_AddFlowTest : BaseUnitTest() {

@Test
fun `given product status is draft, when save is clicked, then save product with correct status`() = testBlocking {
whenever(productRepository.addProduct(any())).thenAnswer { it.arguments.first() as Product }
whenever(productRepository.addProduct(any<ProductAggregate>())).thenAnswer { it.arguments.first() as Product }
var viewState: ProductDetailViewModel.ProductDetailViewState? = null
viewModel.productDetailViewStateData.observeForever { _, new -> viewState = new }

Expand Down

0 comments on commit 035628d

Please sign in to comment.