Skip to content

Commit 3edd299

Browse files
committed
Relay Networking.CreateCartError to Yosemite.CreateCartError.
1 parent cafd3d3 commit 3edd299

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Yosemite/Yosemite/Actions/PaymentAction.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public enum PaymentAction: Action {
1212

1313
/// Creates a cart with a WPCOM plan.
1414
/// - Parameters:
15-
/// - productID: The ID of the WPCOM plan product.
15+
/// - productID: The ID of the WPCOM plan product. It is of string type to integrate with `InAppPurchasesForWPComPlansProtocol`.
16+
/// If the value is not a string of integer value, an error `CreateCartError.invalidProductID` is returned.
1617
/// - siteID: The site ID for the WPCOM plan to be attached to.
1718
/// - completion: The result of cart creation.
1819
case createCart(productID: String,

Yosemite/Yosemite/Stores/PaymentStore.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ private extension PaymentStore {
7070
_ = try await remote.createCart(siteID: siteID, productID: productID)
7171
completion(.success(()))
7272
} catch {
73-
completion(.failure(error))
73+
switch error {
74+
case let networkError as Networking.CreateCartError:
75+
switch networkError {
76+
case .productNotInCart:
77+
completion(.failure(CreateCartError.productNotInCart))
78+
}
79+
default:
80+
completion(.failure(error))
81+
}
7482
}
7583
}
7684
}
@@ -80,4 +88,6 @@ private extension PaymentStore {
8088
public enum CreateCartError: Error, Equatable {
8189
/// Product ID is not in the correct format for WPCOM plans.
8290
case invalidProductID
91+
/// The expected product is not in the created cart.
92+
case productNotInCart
8393
}

Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ final class PaymentStoreTests: XCTestCase {
104104
XCTAssertEqual(error as? Yosemite.CreateCartError, .invalidProductID)
105105
}
106106

107+
func test_createCart_relays_networking_CreateCartError_failure() throws {
108+
// Given
109+
remote.whenCreatingCart(thenReturn: .failure(Networking.CreateCartError.productNotInCart))
110+
111+
// When
112+
let result = waitFor { promise in
113+
self.store.onAction(PaymentAction.createCart(productID: "12", siteID: 62) { result in
114+
promise(result)
115+
})
116+
}
117+
118+
// Then
119+
XCTAssertTrue(result.isFailure)
120+
let error = try XCTUnwrap(result.failure)
121+
XCTAssertEqual(error as? Yosemite.CreateCartError, .productNotInCart)
122+
}
123+
107124
func test_createCart_returns_failure_on_error() throws {
108125
// Given
109126
remote.whenCreatingCart(thenReturn: .failure(NetworkError.timeout))

0 commit comments

Comments
 (0)