Skip to content

Commit c41b308

Browse files
Fix unit tests
1 parent 21bf4e2 commit c41b308

File tree

6 files changed

+305
-295
lines changed

6 files changed

+305
-295
lines changed

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/products/SubscriptionDetailsMapperTest.kt

Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
88
import org.assertj.core.api.Assertions.assertThat
99
import org.junit.Test
1010
import org.wordpress.android.fluxc.model.WCProductModel
11+
import org.wordpress.android.fluxc.model.metadata.WCMetaData
1112
import java.math.BigDecimal
1213

1314
@OptIn(ExperimentalCoroutinesApi::class)
1415
class SubscriptionDetailsMapperTest : BaseUnitTest() {
15-
1616
@Test
1717
fun `when metadata is valid then a SubscriptionDetails is returned`() {
1818
val result = SubscriptionDetailsMapper.toAppModel(successMetadata)
@@ -112,77 +112,74 @@ class SubscriptionDetailsMapperTest : BaseUnitTest() {
112112
* trialLength = 2,
113113
* oneTimeShipping = yes
114114
*/
115-
private val successMetadata = """ [ {
116-
"id": 5182,
117-
"key": "_subscription_payment_sync_date",
118-
"value": "0"
119-
},
120-
{
121-
"id": 5183,
122-
"key": "_subscription_price",
123-
"value": "60"
124-
},
125-
{
126-
"id": 5187,
127-
"key": "_subscription_trial_length",
128-
"value": "2"
129-
},
130-
{
131-
"id": 5188,
132-
"key": "_subscription_sign_up_fee",
133-
"value": "5"
134-
},
135-
{
136-
"id": 5189,
137-
"key": "_subscription_period",
138-
"value": "month"
139-
},
140-
{
141-
"id": 5190,
142-
"key": "_subscription_period_interval",
143-
"value": "1"
144-
},
145-
{
146-
"id": 5191,
147-
"key": "_subscription_length",
148-
"value": "0"
149-
},
150-
{
151-
"id": 5192,
152-
"key": "_subscription_trial_period",
153-
"value": "day"
154-
},
155-
{
156-
"id": 5193,
157-
"key": "_subscription_limit",
158-
"value": "no"
159-
},
160-
{
161-
"id": 5194,
162-
"key": "_subscription_one_time_shipping",
163-
"value": "yes"
164-
} ]
165-
"""
115+
private val successMetadata = listOf(
116+
WCMetaData(
117+
id = 5182,
118+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PAYMENT_SYNC_DATE,
119+
value = "0"
120+
),
121+
WCMetaData(
122+
id = 5183,
123+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PRICE,
124+
value = "60"
125+
),
126+
WCMetaData(
127+
id = 5187,
128+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_TRIAL_LENGTH,
129+
value = "2"
130+
),
131+
WCMetaData(
132+
id = 5188,
133+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_SIGN_UP_FEE,
134+
value = "5"
135+
),
136+
WCMetaData(
137+
id = 5189,
138+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PERIOD,
139+
value = "month"
140+
),
141+
WCMetaData(
142+
id = 5190,
143+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PERIOD_INTERVAL,
144+
value = "1"
145+
),
146+
WCMetaData(
147+
id = 5191,
148+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_LENGTH,
149+
value = "0"
150+
),
151+
WCMetaData(
152+
id = 5192,
153+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_TRIAL_PERIOD,
154+
value = "day"
155+
),
156+
WCMetaData(
157+
id = 5194,
158+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_ONE_TIME_SHIPPING,
159+
value = "yes"
160+
)
161+
)
166162

167163
/**
168164
* Metadata with no subscription key
169165
*/
170-
private val noSubscriptionKeysMetadata = """ [ {
171-
"id": 5182,
172-
"key": "sync_date",
173-
"value": "0"
174-
},
175-
{
176-
"id": 5183,
177-
"key": "price",
178-
"value": "60"
179-
},
180-
{
181-
"id": 5187,
182-
"key": "trial_length",
183-
"value": "2"
184-
}]
185-
"""
166+
private val noSubscriptionKeysMetadata = listOf(
167+
WCMetaData(
168+
id = 5182,
169+
key = "sync_date",
170+
value = "0"
171+
),
172+
WCMetaData(
173+
id = 5183,
174+
key = "price",
175+
value = "60"
176+
),
177+
WCMetaData(
178+
id = 5187,
179+
key = "trial_length",
180+
value = "2"
181+
)
182+
)
186183

187184
/**
188185
* price = 60,
@@ -194,25 +191,26 @@ class SubscriptionDetailsMapperTest : BaseUnitTest() {
194191
* trialLength = 2,
195192
* oneTimeShipping =
196193
*/
197-
private val successMetadataPartial = """ [ {
198-
"id": 5182,
199-
"key": "_subscription_payment_sync_date",
200-
"value": "0"
201-
},
202-
{
203-
"id": 5183,
204-
"key": "_subscription_price",
205-
"value": "60"
206-
},
207-
{
208-
"id": 5187,
209-
"key": "_subscription_trial_length",
210-
"value": "2"
211-
},
212-
{
213-
"id": 5188,
214-
"key": "_subscription_sign_up_fee",
215-
"value": "5"
216-
}]
217-
"""
194+
private val successMetadataPartial = listOf(
195+
WCMetaData(
196+
id = 5182,
197+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PAYMENT_SYNC_DATE,
198+
value = "0"
199+
),
200+
WCMetaData(
201+
id = 5183,
202+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_PRICE,
203+
value = "60"
204+
),
205+
WCMetaData(
206+
id = 5187,
207+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_TRIAL_LENGTH,
208+
value = "2"
209+
),
210+
WCMetaData(
211+
id = 5188,
212+
key = WCProductModel.SubscriptionMetadataKeys.SUBSCRIPTION_SIGN_UP_FEE,
213+
value = "5"
214+
)
215+
)
218216
}

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/products/details/ProductDetailBottomSheetBuilderTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.woocommerce.android.ui.products.details
22

3+
import com.woocommerce.android.model.ProductAggregate
34
import com.woocommerce.android.ui.customfields.CustomFieldsRepository
45
import com.woocommerce.android.ui.products.ProductNavigationTarget
56
import com.woocommerce.android.ui.products.ProductTestUtils
@@ -39,7 +40,7 @@ class ProductDetailBottomSheetBuilderTest : BaseUnitTest() {
3940
whenever(customFieldsRepository.hasDisplayableCustomFields(any())).thenReturn(true)
4041

4142
val product = ProductTestUtils.generateProduct(productId = 1L)
42-
val result = sut.buildBottomSheetList(product)
43+
val result = sut.buildBottomSheetList(ProductAggregate(product))
4344

4445
assertThat(result).noneMatch {
4546
it.type == ProductDetailBottomSheetBuilder.ProductDetailBottomSheetType.CUSTOM_FIELDS
@@ -51,7 +52,7 @@ class ProductDetailBottomSheetBuilderTest : BaseUnitTest() {
5152
whenever(customFieldsRepository.hasDisplayableCustomFields(any())).thenReturn(false)
5253

5354
val product = ProductTestUtils.generateProduct(productId = 1L)
54-
val result = sut.buildBottomSheetList(product)
55+
val result = sut.buildBottomSheetList(ProductAggregate(product))
5556

5657
val customFieldsItem = result.single {
5758
it.type == ProductDetailBottomSheetBuilder.ProductDetailBottomSheetType.CUSTOM_FIELDS
@@ -62,7 +63,7 @@ class ProductDetailBottomSheetBuilderTest : BaseUnitTest() {
6263
@Test
6364
fun `when product is not saved in server, then hide the custom fields item`() = testBlocking {
6465
val product = ProductTestUtils.generateProduct(productId = ProductDetailViewModel.DEFAULT_ADD_NEW_PRODUCT_ID)
65-
val result = sut.buildBottomSheetList(product)
66+
val result = sut.buildBottomSheetList(ProductAggregate(product))
6667

6768
assertThat(result).noneMatch {
6869
it.type == ProductDetailBottomSheetBuilder.ProductDetailBottomSheetType.CUSTOM_FIELDS

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/products/details/ProductDetailCardBuilderTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.woocommerce.android.ui.products.details
22

33
import com.woocommerce.android.R
44
import com.woocommerce.android.model.Product
5+
import com.woocommerce.android.model.ProductAggregate
56
import com.woocommerce.android.tools.SelectedSite
67
import com.woocommerce.android.ui.blaze.IsBlazeEnabled
78
import com.woocommerce.android.ui.customfields.CustomFieldsRepository
@@ -83,7 +84,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
8384
height = 0F
8485
)
8586

86-
val cards = sut.buildPropertyCards(productStub, "")
87+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
8788
Assertions.assertThat(cards).isNotEmpty
8889

8990
cards.find { it.type == ProductPropertyCard.Type.SECONDARY }
@@ -110,7 +111,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
110111
)
111112

112113
var foundAttributesCard = false
113-
val cards = sut.buildPropertyCards(productStub, "")
114+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
114115
Assertions.assertThat(cards).isNotEmpty
115116

116117
cards.find { it.type == ProductPropertyCard.Type.SECONDARY }
@@ -134,7 +135,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
134135
)
135136

136137
var foundQuantityRulesCard = false
137-
val cards = sut.buildPropertyCards(productStub, "")
138+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
138139
Assertions.assertThat(cards).isNotEmpty
139140

140141
cards.find { it.type == ProductPropertyCard.Type.SECONDARY }
@@ -156,7 +157,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
156157
whenever(customFieldsRepository.hasDisplayableCustomFields(any())) doReturn false
157158

158159
productStub = ProductTestUtils.generateProduct(productId = 1L)
159-
val cards = sut.buildPropertyCards(productStub, "")
160+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
160161

161162
val properties = cards.first { it.type == ProductPropertyCard.Type.SECONDARY }.properties
162163
val customFieldsCard = properties.find {
@@ -171,7 +172,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
171172
whenever(customFieldsRepository.hasDisplayableCustomFields(any())) doReturn true
172173

173174
productStub = ProductTestUtils.generateProduct(productId = 1L)
174-
val cards = sut.buildPropertyCards(productStub, "")
175+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
175176

176177
val properties = cards.first { it.type == ProductPropertyCard.Type.SECONDARY }.properties
177178
val customFieldsCard = properties.find {
@@ -184,7 +185,7 @@ class ProductDetailCardBuilderTest : BaseUnitTest() {
184185
@Test
185186
fun `when a new is not saved on the server, then hide the custom fields card`() = testBlocking {
186187
productStub = ProductTestUtils.generateProduct(productId = ProductDetailViewModel.DEFAULT_ADD_NEW_PRODUCT_ID)
187-
val cards = sut.buildPropertyCards(productStub, "")
188+
val cards = sut.buildPropertyCards(ProductAggregate(productStub), "")
188189

189190
val properties = cards.first { it.type == ProductPropertyCard.Type.SECONDARY }.properties
190191
val customFieldsCard = properties.find {

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/products/details/ProductDetailViewModelGenerateVariationFlowTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.woocommerce.android.analytics.AnalyticsTracker
77
import com.woocommerce.android.analytics.AnalyticsTrackerWrapper
88
import com.woocommerce.android.media.MediaFilesRepository
99
import com.woocommerce.android.media.ProductImagesServiceWrapper
10+
import com.woocommerce.android.model.ProductAggregate
1011
import com.woocommerce.android.model.RequestResult
1112
import com.woocommerce.android.model.VariantOption
1213
import com.woocommerce.android.tools.NetworkStatus
@@ -92,7 +93,7 @@ class ProductDetailViewModelGenerateVariationFlowTest : BaseUnitTest() {
9293
doReturn(true).whenever(networkStatus).isConnected()
9394

9495
productRepository = mock {
95-
onBlocking { fetchAndGetProduct(PRODUCT_REMOTE_ID) } doReturn product
96+
onBlocking { fetchAndGetProductAggregate(PRODUCT_REMOTE_ID) } doReturn ProductAggregate(product)
9697
}
9798

9899
variationRepository = mock {

0 commit comments

Comments
 (0)