|
1 | 1 | import XCTest |
2 | 2 | import Fakes |
3 | 3 | import Yosemite |
| 4 | +import enum Alamofire.AFError |
4 | 5 | @testable import WooCommerce |
5 | 6 |
|
6 | 7 | class CardPresentPaymentsOnboardingUseCaseTests: XCTestCase { |
@@ -1226,6 +1227,62 @@ class CardPresentPaymentsOnboardingUseCaseTests: XCTestCase { |
1226 | 1227 | XCTAssertEqual(CardPresentPaymentsPlugin.stripe.plugin, .stripe) |
1227 | 1228 | XCTAssertEqual(CardPresentPaymentsPlugin.stripe.fileNameWithPathExtension, "woocommerce-gateway-stripe/woocommerce-gateway-stripe") |
1228 | 1229 | } |
| 1230 | + |
| 1231 | + // MARK: - loadAccounts error handling tests |
| 1232 | + |
| 1233 | + func test_onboarding_handles_network_error_when_loading_accounts() { |
| 1234 | + // Given |
| 1235 | + setupCountry(country: .us) |
| 1236 | + setupWCPayPlugin(status: .active, version: WCPayPluginVersion.minimumSupportedVersion) |
| 1237 | + |
| 1238 | + // Use AFError.sessionTaskFailed which is what Alamofire returns for network errors |
| 1239 | + let urlError = URLError(.notConnectedToInternet) |
| 1240 | + let networkError = AFError.sessionTaskFailed(error: urlError) |
| 1241 | + stores.whenReceivingAction(ofType: CardPresentPaymentAction.self) { action in |
| 1242 | + switch action { |
| 1243 | + case let .loadAccounts(_, onCompletion): |
| 1244 | + onCompletion(.failure(networkError)) |
| 1245 | + default: |
| 1246 | + break |
| 1247 | + } |
| 1248 | + } |
| 1249 | + |
| 1250 | + // When |
| 1251 | + let useCase = CardPresentPaymentsOnboardingUseCase(storageManager: storageManager, |
| 1252 | + stores: stores, |
| 1253 | + cardPresentPaymentOnboardingStateCache: onboardingStateCache) |
| 1254 | + useCase.updateAccounts() |
| 1255 | + |
| 1256 | + // Then - Should show no connection error |
| 1257 | + XCTAssertEqual(useCase.state, .noConnectionError) |
| 1258 | + } |
| 1259 | + |
| 1260 | + func test_onboarding_handles_generic_error_when_both_accounts_fail_to_load() { |
| 1261 | + // Given |
| 1262 | + setupCountry(country: .us) |
| 1263 | + setupWCPayPlugin(status: .active, version: WCPayPluginVersion.minimumSupportedVersion) |
| 1264 | + setupStripePlugin(status: .active, version: StripePluginVersion.minimumSupportedVersion) |
| 1265 | + |
| 1266 | + let genericError = NSError(domain: "test.error", code: 500, userInfo: nil) |
| 1267 | + stores.whenReceivingAction(ofType: CardPresentPaymentAction.self) { action in |
| 1268 | + switch action { |
| 1269 | + case let .loadAccounts(_, onCompletion): |
| 1270 | + // Both accounts fail to load |
| 1271 | + onCompletion(.failure(genericError)) |
| 1272 | + default: |
| 1273 | + break |
| 1274 | + } |
| 1275 | + } |
| 1276 | + |
| 1277 | + // When |
| 1278 | + let useCase = CardPresentPaymentsOnboardingUseCase(storageManager: storageManager, |
| 1279 | + stores: stores, |
| 1280 | + cardPresentPaymentOnboardingStateCache: onboardingStateCache) |
| 1281 | + useCase.updateAccounts() |
| 1282 | + |
| 1283 | + // Then - Should show generic error |
| 1284 | + XCTAssertEqual(useCase.state, .genericError) |
| 1285 | + } |
1229 | 1286 | } |
1230 | 1287 |
|
1231 | 1288 | // MARK: - Settings helpers |
|
0 commit comments