Skip to content

Commit 2a2c75f

Browse files
committed
Enhance unit tests for new card payment eligibility approach.
1 parent 2f33b92 commit 2a2c75f

File tree

2 files changed

+110
-53
lines changed

2 files changed

+110
-53
lines changed

WooCommerce/WooCommerceTests/ViewRelated/OrderDetailsViewModelTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,60 @@ final class OrderDetailsViewModelTests: XCTestCase {
129129
let actionButtonIDs = viewModel.moreActionsButtons.map { $0.id }
130130
XCTAssertFalse(actionButtonIDs.contains(.editOrder))
131131
}
132+
133+
func test_it_sets_dataSource_isEligibleForCardPresentPayment_to_true_if_it_is_eligible() {
134+
// Given
135+
let order = Order.fake().copy(total: "10.0")
136+
let stores = MockStoresManager(sessionManager: .testingInstance)
137+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
138+
switch action {
139+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
140+
completion(.success(true))
141+
}
142+
}
143+
144+
// When
145+
let viewModel = OrderDetailsViewModel(order: order, stores: stores)
146+
147+
// Then
148+
XCTAssertTrue(viewModel.dataSource.isEligibleForCardPresentPayment)
149+
}
150+
151+
func test_it_sets_dataSource_isEligibleForCardPresentPayment_to_false_if_it_is_not_eligible() {
152+
// Given
153+
let order = Order.fake().copy(total: "10.0")
154+
let stores = MockStoresManager(sessionManager: .testingInstance)
155+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
156+
switch action {
157+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
158+
completion(.success(false))
159+
}
160+
}
161+
162+
// When
163+
let viewModel = OrderDetailsViewModel(order: order, stores: stores)
164+
165+
// Then
166+
XCTAssertFalse(viewModel.dataSource.isEligibleForCardPresentPayment)
167+
}
168+
169+
func test_it_sets_dataSource_isEligibleForCardPresentPayment_to_false_if_there_is_an_error() {
170+
// Given
171+
let order = Order.fake().copy(total: "10.0")
172+
let stores = MockStoresManager(sessionManager: .testingInstance)
173+
let error = NSError(domain: "Error", code: 0)
174+
175+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
176+
switch action {
177+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
178+
completion(.failure(error))
179+
}
180+
}
181+
182+
// When
183+
let viewModel = OrderDetailsViewModel(order: order, stores: stores)
184+
185+
// Then
186+
XCTAssertFalse(viewModel.dataSource.isEligibleForCardPresentPayment)
187+
}
132188
}

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsMethodsViewModelTests.swift

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -336,75 +336,76 @@ final class SimplePaymentsMethodsViewModelTests: XCTestCase {
336336
assertEqual(analytics.receivedProperties.last?["payment_method"] as? String, "card")
337337
}
338338

339-
func test_card_row_is_shown_for_eligible_order() {
339+
func test_card_row_is_shown_for_eligible_order_and_country() {
340340
// Given
341+
let stores = MockStoresManager(sessionManager: .testingInstance)
341342
let storage = MockStorageManager()
342-
let orderItem = OrderItem.fake().copy(itemID: 1234,
343-
name: "Chocolate cake",
344-
productID: 678,
345-
quantity: 1.0)
346-
let cppEligibleOrder = Order.fake().copy(siteID: 1212,
347-
orderID: 111,
348-
status: .pending,
349-
currency: "USD",
350-
datePaid: nil,
351-
total: "5.00",
352-
paymentMethodID: "woocommerce_payments",
353-
items: [orderItem])
354-
let nonSubscriptionProduct = Product.fake().copy(siteID: 1212,
355-
productID: 678,
356-
name: "Chocolate cake",
357-
productTypeKey: "simple")
343+
let configuration = CardPresentPaymentsConfiguration.init(country: "US")
344+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
345+
switch action {
346+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
347+
completion(.success(true))
348+
}
349+
}
358350

359-
storage.insertSampleProduct(readOnlyProduct: nonSubscriptionProduct)
360-
storage.insertSampleOrder(readOnlyOrder: cppEligibleOrder)
351+
let dependencies = Dependencies(stores: stores, storage: storage, cardPresentPaymentsConfiguration: configuration)
352+
let viewModel = SimplePaymentsMethodsViewModel(siteID: 1212, orderID: 111, formattedTotal: "$5.00", dependencies: dependencies)
353+
354+
// Then
355+
XCTAssertTrue(viewModel.showPayWithCardRow)
356+
}
361357

358+
func test_card_row_is_not_shown_when_there_is_an_error_checking_for_order_eligibility() {
359+
// Given
360+
let stores = MockStoresManager(sessionManager: .testingInstance)
361+
let storage = MockStorageManager()
362362
let configuration = CardPresentPaymentsConfiguration.init(country: "US")
363+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
364+
switch action {
365+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
366+
completion(.failure(NSError(domain: "Error", code: 0)))
367+
}
368+
}
363369

364-
let dependencies = Dependencies(storage: storage, cardPresentPaymentsConfiguration: configuration)
370+
let dependencies = Dependencies(stores: stores, storage: storage, cardPresentPaymentsConfiguration: configuration)
365371
let viewModel = SimplePaymentsMethodsViewModel(siteID: 1212, orderID: 111, formattedTotal: "$5.00", dependencies: dependencies)
366372

367373
// Then
368-
XCTAssertTrue(viewModel.showPayWithCardRow)
374+
XCTAssertFalse(viewModel.showPayWithCardRow)
369375
}
370376

371-
func test_card_row_is_not_shown_for_eligible_order_but_ineligible_country() {
377+
func test_card_row_is_not_shown_for_non_eligible_order() {
372378
// Given
379+
let stores = MockStoresManager(sessionManager: .testingInstance)
373380
let storage = MockStorageManager()
374-
let orderItem = OrderItem.fake().copy(itemID: 1234,
375-
name: "Chocolate cake",
376-
productID: 678,
377-
quantity: 1.0)
378-
let cppEligibleOrder = Order.fake().copy(siteID: 1212,
379-
orderID: 111,
380-
status: .pending,
381-
currency: "USD",
382-
datePaid: nil,
383-
total: "5.00",
384-
paymentMethodID: "woocommerce_payments",
385-
items: [orderItem])
386-
let nonSubscriptionProduct = Product.fake().copy(siteID: 1212,
387-
productID: 678,
388-
name: "Chocolate cake",
389-
productTypeKey: "simple")
381+
let configuration = CardPresentPaymentsConfiguration.init(country: "US")
382+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
383+
switch action {
384+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
385+
completion(.success(false))
386+
}
387+
}
390388

391-
storage.insertSampleProduct(readOnlyProduct: nonSubscriptionProduct)
392-
storage.insertSampleOrder(readOnlyOrder: cppEligibleOrder)
389+
let dependencies = Dependencies(stores: stores, storage: storage, cardPresentPaymentsConfiguration: configuration)
390+
let viewModel = SimplePaymentsMethodsViewModel(siteID: 1212, orderID: 111, formattedTotal: "$5.00", dependencies: dependencies)
391+
392+
// Then
393+
XCTAssertFalse(viewModel.showPayWithCardRow)
394+
}
393395

394-
// Antarctica 🤞 (it's actually based on the lack of the following 4 properties)
395-
let configuration = CardPresentPaymentsConfiguration.init(countryCode: "AQ",
396-
paymentMethods: [],
397-
currencies: [],
398-
paymentGateways: [],
399-
supportedReaders: [],
400-
supportedPluginVersions: [
401-
.init(plugin: .wcPay, minimumVersion: "3.2.1"),
402-
.init(plugin: .stripe, minimumVersion: "6.2.0")
403-
],
404-
minimumAllowedChargeAmount: NSDecimalNumber(string: "0.5"),
405-
stripeSmallestCurrencyUnitMultiplier: 100)
396+
func test_card_row_is_not_shown_for_eligible_order_but_ineligible_country() {
397+
// Given
398+
let stores = MockStoresManager(sessionManager: .testingInstance)
399+
let storage = MockStorageManager()
400+
let configuration = CardPresentPaymentsConfiguration.init(country: "AQ")
401+
stores.whenReceivingAction(ofType: OrderCardPresentPaymentEligibilityAction.self) { action in
402+
switch action {
403+
case let .orderIsEligibleForCardPresentPayment(_, _, _, completion):
404+
completion(.success(true))
405+
}
406+
}
406407

407-
let dependencies = Dependencies(storage: storage, cardPresentPaymentsConfiguration: configuration)
408+
let dependencies = Dependencies(stores: stores, storage: storage, cardPresentPaymentsConfiguration: configuration)
408409
let viewModel = SimplePaymentsMethodsViewModel(siteID: 1212, orderID: 111, formattedTotal: "$5.00", dependencies: dependencies)
409410

410411
// Then

0 commit comments

Comments
 (0)