Skip to content

Commit 3e0a813

Browse files
committed
Adapt tests to the new print receipt implementation approach.
1 parent 8fd4595 commit 3e0a813

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

WooCommerce/WooCommerceTests/ViewModels/CardPresentPayments/LegacyCollectOrderPaymentUseCaseTests.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,6 @@ final class LegacyCollectOrderPaymentUseCaseTests: XCTestCase {
110110
}
111111

112112
// MARK: Success alert actions
113-
114-
func test_printing_receipt_from_collectPayment_success_alert_tracks_receiptPrintTapped_event() throws {
115-
// Given
116-
let intent = PaymentIntent.fake().copy(charges: [.fake().copy(paymentMethod: .cardPresent(details: .fake()))])
117-
mockSuccessfulCardPresentPaymentActions(intent: intent)
118-
119-
// When
120-
waitFor { promise in
121-
self.useCase.collectPayment(onCollect: { _ in
122-
promise(())
123-
}, onCancel: {}, onCompleted: {})
124-
}
125-
alerts.printReceiptFromSuccessAlert?()
126-
127-
// Then
128-
let indexOfEvent = try XCTUnwrap(analyticsProvider.receivedEvents.firstIndex(where: { $0 == "receipt_print_tapped"}))
129-
let eventProperties = try XCTUnwrap(analyticsProvider.receivedProperties[indexOfEvent])
130-
XCTAssertEqual(eventProperties["card_reader_model"] as? String, Mocks.cardReaderModel)
131-
XCTAssertEqual(eventProperties["country"] as? String, "US")
132-
}
133-
134113
func test_emailing_receipt_from_collectPayment_success_alert_tracks_receiptEmailTapped_event() throws {
135114
// Given
136115
let intent = PaymentIntent.fake().copy(charges: [.fake().copy(paymentMethod: .cardPresent(details: .fake()))])

WooCommerce/WooCommerceTests/ViewRelated/CardPresentPayments/ReceiptActionCoordinatorTests.swift

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,39 @@ import Hardware
66
@testable import WooCommerce
77

88
final class ReceiptActionCoordinatorTests: XCTestCase {
9-
func test_printReceipt_logs_receiptPrintTapped_analyticEvent() throws {
9+
private var storesManager: MockStoresManager!
10+
11+
override func setUp() {
12+
super.setUp()
13+
14+
storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
15+
}
16+
17+
override func tearDown() {
18+
storesManager = nil
19+
}
20+
21+
func test_printReceipt_logs_receiptPrintTapped_analyticEvent() async throws {
1022
// Given
1123
let analytics = MockAnalyticsProvider()
1224
let order = MockOrders().makeOrder()
1325
let params = CardPresentReceiptParameters.makeParams()
1426

27+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
28+
switch action {
29+
case let .print(_, _, completion):
30+
completion(.success)
31+
default:
32+
break
33+
}
34+
}
35+
1536
// When
16-
ReceiptActionCoordinator.printReceipt(for: order,
37+
await ReceiptActionCoordinator.printReceipt(for: order,
1738
params: params,
1839
countryCode: "CA",
1940
cardReaderModel: "WISEPAD_3",
20-
stores: ServiceLocator.stores,
41+
stores: storesManager,
2142
analytics: WooAnalytics(analyticsProvider: analytics))
2243

2344
// Then
@@ -27,18 +48,26 @@ final class ReceiptActionCoordinatorTests: XCTestCase {
2748
XCTAssertEqual(eventProperties["country"] as? String, "CA")
2849
}
2950

30-
func test_printReceipt_sends_print_receiptAction() throws {
51+
func test_printReceipt_sends_print_receiptAction() async throws {
3152
// Given
3253
let order = MockOrders().makeOrder()
3354
let params = CardPresentReceiptParameters.makeParams()
3455

35-
let storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
3656
storesManager.reset()
3757

3858
assertEmpty(storesManager.receivedActions)
3959

60+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
61+
switch action {
62+
case let .print(_, _, completion):
63+
completion(.success)
64+
default:
65+
break
66+
}
67+
}
68+
4069
// When
41-
ReceiptActionCoordinator.printReceipt(for: order,
70+
await ReceiptActionCoordinator.printReceipt(for: order,
4271
params: params,
4372
countryCode: "CA",
4473
cardReaderModel: nil,
@@ -57,48 +86,53 @@ final class ReceiptActionCoordinatorTests: XCTestCase {
5786
}
5887
}
5988

60-
func test_printReceipt_success_logs_receiptPrintSuccess_analyticEvent() throws {
61-
try assertAnalyticLogged(.receiptPrintSuccess, for: .success)
89+
func test_printReceipt_success_logs_receiptPrintSuccess_analyticEvent() async throws {
90+
try await assertAnalyticLogged(.receiptPrintSuccess, for: .success)
6291
}
6392

64-
func test_printReceipt_cancel_logs_receiptPrintCanceled_analyticEvent() throws {
65-
try assertAnalyticLogged(.receiptPrintCanceled, for: .cancel)
93+
func test_printReceipt_cancel_logs_receiptPrintCanceled_analyticEvent() async throws {
94+
try await assertAnalyticLogged(.receiptPrintCanceled, for: .cancel)
6695
}
6796

68-
func test_printReceipt_fail_logs_receiptPrintFailed_analyticEvent() throws {
97+
func test_printReceipt_fail_logs_receiptPrintFailed_analyticEvent() async throws {
6998
let error = NSError(domain: "errordomain", code: 123, userInfo: nil)
70-
try assertAnalyticLogged(.receiptPrintFailed, for: .failure(error))
99+
try await assertAnalyticLogged(.receiptPrintFailed, for: .failure(error))
71100
}
72101
}
73102

74103
extension ReceiptActionCoordinatorTests {
75-
func assertAnalyticLogged(_ analytic: WooAnalyticsStat, for printingResult: PrintingResult) throws {
104+
func assertAnalyticLogged(_ analytic: WooAnalyticsStat, for printingResult: PrintingResult) async throws {
76105
// Given
77106
let order = MockOrders().makeOrder()
78107
let params = CardPresentReceiptParameters.makeParams()
108+
let countryCode = "CA"
109+
let cardReaderModel = "test_reader"
110+
79111

80-
let storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
81112
let analytics = MockAnalyticsProvider()
82113

114+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
115+
switch action {
116+
case let .print(_, _, completion):
117+
completion(printingResult)
118+
default:
119+
break
120+
}
121+
}
122+
83123
// When
84-
ReceiptActionCoordinator.printReceipt(for: order,
124+
await ReceiptActionCoordinator.printReceipt(for: order,
85125
params: params,
86126
countryCode: "CA",
87-
cardReaderModel: nil,
127+
cardReaderModel: cardReaderModel,
88128
stores: storesManager,
89129
analytics: WooAnalytics(analyticsProvider: analytics))
90130

91-
//Then
92-
let action = try XCTUnwrap(storesManager.receivedActions.first as? ReceiptAction)
93-
switch action {
94-
case .print(order: _, parameters: _, let completion):
95-
completion(printingResult)
96-
97-
let receivedEvents = analytics.receivedEvents
98-
XCTAssert(receivedEvents.contains(analytic.rawValue))
99-
default:
100-
XCTFail("Print Receipt failed to dispatch .print action")
101-
}
131+
// Then
132+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == analytic.rawValue}))
133+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
134+
XCTAssertEqual(eventProperties["card_reader_model"] as? String, cardReaderModel)
135+
XCTAssertEqual(eventProperties["country"] as? String, countryCode)
102136
}
103137
}
104138

0 commit comments

Comments
 (0)