-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShopPayViewControllerTests.swift
More file actions
300 lines (243 loc) · 10.7 KB
/
Copy pathShopPayViewControllerTests.swift
File metadata and controls
300 lines (243 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
MIT License
Copyright 2023 - Present, Shopify Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
@testable import ShopifyAcceleratedCheckouts
import ShopifyCheckoutKit
import XCTest
@available(iOS 16.0, *)
@MainActor
final class ShopPayViewControllerTests: XCTestCase {
var viewController: MockShopPayViewController!
var mockConfiguration: ShopifyAcceleratedCheckouts.Configuration!
var mockStorefront: TestStorefrontAPI!
override func setUp() async throws {
try await super.setUp()
mockConfiguration = ShopifyAcceleratedCheckouts.Configuration(
storefrontDomain: "test-shop.myshopify.com",
storefrontAccessToken: "test-token"
)
mockStorefront = TestStorefrontAPI()
}
override func tearDown() async throws {
viewController = nil
mockConfiguration = nil
mockStorefront = nil
try await super.tearDown()
}
@available(iOS 16.0, *)
class MockShopPayViewController: ShopPayViewController {
var mockTopViewController: UIViewController?
var mockStorefront: TestStorefrontAPI
var presentCalls: [(url: URL, client: (any CheckoutCommunicationProtocol)?)] = []
override func getTopViewController() -> UIViewController? {
return mockTopViewController
}
@MainActor
override func present(url: URL, client: (any CheckoutCommunicationProtocol)?) async throws {
presentCalls.append((url: url, client: client))
}
init(
identifier: CheckoutIdentifier,
configuration: ShopifyAcceleratedCheckouts.Configuration,
eventHandlers: EventHandlers = EventHandlers(),
storefront: TestStorefrontAPI = TestStorefrontAPI()
) {
mockStorefront = storefront
super.init(
identifier: identifier,
configuration: configuration,
eventHandlers: eventHandlers
)
self.storefront = storefront
}
}
// MARK: - onPress() Tests with Cart Identifier
func test_onPress_withCartIdentifier_shouldCallPresentWithShopPayURL() async {
let mockCart = StorefrontAPI.Cart.testCart()
viewController = MockShopPayViewController(
identifier: .cart(cartID: "gid://Shopify/Cart/test-cart-id"),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartResult = .success(mockCart)
await viewController.onPress()
XCTAssertEqual(viewController.presentCalls.count, 1)
XCTAssertEqual(
viewController.presentCalls[0].url.absoluteString,
"https://test-shop.myshopify.com/checkout?payment=shop_pay"
)
}
func test_onPress_withCartIdentifierCartNotFound_shouldNotCallPresent() async {
viewController = MockShopPayViewController(
identifier: .cart(cartID: "non-existent-cart-id"),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartResult = .success(nil)
await viewController.onPress()
XCTAssertTrue(viewController.presentCalls.isEmpty)
}
// MARK: - onPress() Tests with Variant Identifier
func test_onPress_withVariantIdentifier_shouldCallPresent() async {
let mockCart = StorefrontAPI.Cart.testCart()
viewController = MockShopPayViewController(
identifier: .variant(
variantID: "gid://Shopify/ProductVariant/test-variant-id",
quantity: 2
),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartCreateResult = .success(mockCart)
await viewController.onPress()
XCTAssertEqual(viewController.presentCalls.count, 1)
XCTAssertEqual(
viewController.presentCalls[0].url.absoluteString,
"https://test-shop.myshopify.com/checkout?payment=shop_pay"
)
}
func test_onPress_withVariantIdentifier_whenCartCreateFails_shouldNotCallPresent()
async
{
let cartCreateError = NSError(domain: "CartCreateError", code: 400, userInfo: nil)
viewController = MockShopPayViewController(
identifier: .variant(
variantID: "gid://Shopify/ProductVariant/test-variant-id",
quantity: 2
),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartCreateResult = .failure(cartCreateError)
await viewController.onPress()
XCTAssertTrue(viewController.presentCalls.isEmpty)
}
func test_onPress_withInvalidZeroQuantityVariantIdentifier_shouldNotCreateCheckoutCoordinator()
async
{
let mockCart = StorefrontAPI.Cart.testCart()
viewController = MockShopPayViewController(
identifier: .variant(
variantID: "gid://Shopify/ProductVariant/test-variant-id",
quantity: 0
),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartCreateResult = .success(mockCart)
await viewController.onPress()
XCTAssertNil(viewController.checkoutViewController)
XCTAssertTrue(viewController.presentCalls.isEmpty)
}
// MARK: - onPress() Tests with Invariant Identifier
func test_onPress_withInvariantIdentifier_shouldNotCallPresent() async {
viewController = MockShopPayViewController(
identifier: .invariant(reason: "Invalid checkout data"),
configuration: mockConfiguration,
storefront: mockStorefront
)
await viewController.onPress()
XCTAssertTrue(viewController.presentCalls.isEmpty)
}
// MARK: - Error Handling Tests
func test_onPress_withCartNotFound_shouldCallCheckoutDidFail() async {
let checkoutDidFailExpectation = XCTestExpectation(
description: "checkoutDidFail should be called"
)
viewController = MockShopPayViewController(
identifier: .cart(cartID: "non-existent-cart-id"),
configuration: mockConfiguration,
eventHandlers: EventHandlers(
checkoutDidFail: { _ in checkoutDidFailExpectation.fulfill() }
),
storefront: mockStorefront
)
viewController.mockStorefront.cartResult = .success(nil)
await viewController.onPress()
await fulfillment(of: [checkoutDidFailExpectation], timeout: 1.0)
}
func test_onPress_withVariantIdentifierCartCreateFails_shouldCallCheckoutDidFail() async {
let checkoutDidFailExpectation = XCTestExpectation(
description: "checkoutDidFail should be called"
)
let identifier = CheckoutIdentifier.variant(
variantID: "gid://Shopify/ProductVariant/test-variant-id",
quantity: 2
)
var underlyingError: Error?
viewController = MockShopPayViewController(
identifier: identifier,
configuration: mockConfiguration,
eventHandlers: EventHandlers(
checkoutDidFail: { error in
if case let .sdkError(underlying, _) = error {
underlyingError = underlying
checkoutDidFailExpectation.fulfill()
} else {
XCTFail("Expected sdkError")
}
}
),
storefront: mockStorefront
)
// Now cartCreate errors pass through directly instead of being wrapped
let cartCreateError = NSError(domain: "CartCreateError", code: 400, userInfo: nil)
viewController.mockStorefront.cartCreateResult = .failure(cartCreateError)
await viewController.onPress()
await fulfillment(of: [checkoutDidFailExpectation], timeout: 1.0)
guard let nsError = underlyingError as? NSError else {
XCTFail("Expected NSError, got: \(String(describing: underlyingError))")
return
}
XCTAssertEqual(nsError.domain, "CartCreateError")
XCTAssertEqual(nsError.code, 400)
}
func test_onPress_withInvariantIdentifier_shouldCallCheckoutDidFail() async {
let checkoutDidFailExpectation = XCTestExpectation(
description: "checkoutDidFail should be called"
)
viewController = MockShopPayViewController(
identifier: .invariant(reason: "Invalid checkout data"),
configuration: mockConfiguration,
eventHandlers: EventHandlers(
checkoutDidFail: { _ in checkoutDidFailExpectation.fulfill() }
),
storefront: mockStorefront
)
await viewController.onPress()
await fulfillment(of: [checkoutDidFailExpectation], timeout: 1.0)
}
// MARK: - URL Construction Tests
func test_onPress_withInvalidURL_shouldCallPresentWithModifiedURL() async throws {
let mockCart = try StorefrontAPI.Cart.testCart(
checkoutUrl: XCTUnwrap(URL(string: "invalid-url"))
)
viewController = MockShopPayViewController(
identifier: .cart(cartID: "gid://Shopify/Cart/test-cart-id"),
configuration: mockConfiguration,
storefront: mockStorefront
)
viewController.mockStorefront.cartResult = .success(mockCart)
await viewController.onPress()
// An invalid URL still gets processed, so we should check it was called with the modified URL
XCTAssertEqual(
viewController.presentCalls[0].url.absoluteString,
"invalid-url?payment=shop_pay"
)
}
}