Skip to content

Commit eb5c1b9

Browse files
committed
Update tests to use waitFor helper
1 parent 5c17b78 commit eb5c1b9

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

Modules/Tests/YosemiteTests/Stores/CardPresentPaymentStoreTests.swift

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -252,38 +252,40 @@ final class CardPresentPaymentStoreTests: XCTestCase {
252252
/// Verifies that the PaymentGatewayAccountStore hits the network when loading a WCPay Account and places nothing in storage in case of error.
253253
///
254254
func test_loadAccounts_handles_failure() throws {
255-
let expectation = self.expectation(description: "Load Account error response")
256255
network.simulateResponse(requestUrlSuffix: "payments/accounts",
257256
filename: "generic_error")
258257
network.simulateResponse(requestUrlSuffix: "wc_stripe/account/summary",
259258
filename: "generic_error")
260259

261-
let action = CardPresentPaymentAction.loadAccounts(siteID: sampleSiteID, onCompletion: { result in
262-
XCTAssertTrue(result.isFailure)
263-
expectation.fulfill()
264-
})
260+
// When
261+
let result = waitFor { promise in
262+
self.cardPresentStore.onAction(CardPresentPaymentAction.loadAccounts(siteID: self.sampleSiteID, onCompletion: { result in
263+
promise(result)
264+
}))
265+
}
265266

266-
cardPresentStore.onAction(action)
267-
wait(for: [expectation], timeout: Constants.expectationTimeout)
267+
// Then
268+
XCTAssertTrue(result.isFailure)
268269

269270
XCTAssertNil(viewStorage.firstObject(ofType: Storage.PaymentGatewayAccount.self, matching: nil))
270271
}
271272

272273
/// Verifies that the PaymentGatewayAccountStore hits the network when loading a WCPay Account, propagates success and upserts the account into storage.
273274
///
274275
func test_loadAccounts_returns_expected_data() throws {
275-
let expectation = self.expectation(description: "Load Account fetch response")
276276
network.simulateResponse(requestUrlSuffix: "payments/accounts",
277277
filename: "wcpay-account-complete")
278278
network.simulateResponse(requestUrlSuffix: "wc_stripe/account/summary",
279279
filename: "stripe-account-complete")
280-
let action = CardPresentPaymentAction.loadAccounts(siteID: sampleSiteID, onCompletion: { result in
281-
XCTAssertTrue(result.isSuccess)
282-
expectation.fulfill()
283-
})
280+
// When
281+
let result = waitFor { promise in
282+
self.cardPresentStore.onAction(CardPresentPaymentAction.loadAccounts(siteID: self.sampleSiteID, onCompletion: { result in
283+
promise(result)
284+
}))
285+
}
284286

285-
cardPresentStore.onAction(action)
286-
wait(for: [expectation], timeout: Constants.expectationTimeout)
287+
// Then
288+
XCTAssertTrue(result.isSuccess)
287289

288290
XCTAssert(viewStorage.countObjects(ofType: Storage.PaymentGatewayAccount.self, matching: nil) == 2)
289291

@@ -300,19 +302,21 @@ final class CardPresentPaymentStoreTests: XCTestCase {
300302
/// Verifies that loadAccounts succeeds when only WCPay succeeds and Stripe fails.
301303
///
302304
func test_loadAccounts_succeeds_when_only_wcpay_succeeds() throws {
303-
let expectation = self.expectation(description: "Load Account succeeds with WCPay only")
305+
// Given
304306
network.simulateResponse(requestUrlSuffix: "payments/accounts",
305307
filename: "wcpay-account-complete")
306308
network.simulateResponse(requestUrlSuffix: "wc_stripe/account/summary",
307309
filename: "generic_error")
308310

309-
let action = CardPresentPaymentAction.loadAccounts(siteID: sampleSiteID, onCompletion: { result in
310-
XCTAssertTrue(result.isSuccess)
311-
expectation.fulfill()
312-
})
311+
// When
312+
let result = waitFor { promise in
313+
self.cardPresentStore.onAction(CardPresentPaymentAction.loadAccounts(siteID: self.sampleSiteID, onCompletion: { result in
314+
promise(result)
315+
}))
316+
}
313317

314-
cardPresentStore.onAction(action)
315-
wait(for: [expectation], timeout: Constants.expectationTimeout)
318+
// Then
319+
XCTAssertTrue(result.isSuccess)
316320

317321
XCTAssert(viewStorage.countObjects(ofType: Storage.PaymentGatewayAccount.self, matching: nil) == 1)
318322

@@ -329,19 +333,21 @@ final class CardPresentPaymentStoreTests: XCTestCase {
329333
/// Verifies that loadAccounts succeeds when only Stripe succeeds and WCPay fails.
330334
///
331335
func test_loadAccounts_succeeds_when_only_stripe_succeeds() throws {
332-
let expectation = self.expectation(description: "Load Account succeeds with Stripe only")
336+
// Given
333337
network.simulateResponse(requestUrlSuffix: "payments/accounts",
334338
filename: "generic_error")
335339
network.simulateResponse(requestUrlSuffix: "wc_stripe/account/summary",
336340
filename: "stripe-account-complete")
337341

338-
let action = CardPresentPaymentAction.loadAccounts(siteID: sampleSiteID, onCompletion: { result in
339-
XCTAssertTrue(result.isSuccess)
340-
expectation.fulfill()
341-
})
342+
// When
343+
let result = waitFor { promise in
344+
self.cardPresentStore.onAction(CardPresentPaymentAction.loadAccounts(siteID: self.sampleSiteID, onCompletion: { result in
345+
promise(result)
346+
}))
347+
}
342348

343-
cardPresentStore.onAction(action)
344-
wait(for: [expectation], timeout: Constants.expectationTimeout)
349+
// Then
350+
XCTAssertTrue(result.isSuccess)
345351

346352
XCTAssert(viewStorage.countObjects(ofType: Storage.PaymentGatewayAccount.self, matching: nil) == 1)
347353

@@ -358,19 +364,20 @@ final class CardPresentPaymentStoreTests: XCTestCase {
358364
/// Verifies that loadAccounts returns an error when both WCPay and Stripe fail.
359365
///
360366
func test_loadAccounts_fails_when_both_wcpay_and_stripe_fail() throws {
361-
let expectation = self.expectation(description: "Load Account fails when both fail")
367+
// Given
362368
network.simulateResponse(requestUrlSuffix: "payments/accounts",
363369
filename: "generic_error")
364370
network.simulateResponse(requestUrlSuffix: "wc_stripe/account/summary",
365371
filename: "generic_error")
372+
// When
373+
let result = waitFor { promise in
374+
self.cardPresentStore.onAction(CardPresentPaymentAction.loadAccounts(siteID: self.sampleSiteID, onCompletion: { result in
375+
promise(result)
376+
}))
377+
}
366378

367-
let action = CardPresentPaymentAction.loadAccounts(siteID: sampleSiteID, onCompletion: { result in
368-
XCTAssertTrue(result.isFailure)
369-
expectation.fulfill()
370-
})
371-
372-
cardPresentStore.onAction(action)
373-
wait(for: [expectation], timeout: Constants.expectationTimeout)
379+
// Then
380+
XCTAssertTrue(result.isFailure)
374381

375382
XCTAssertNil(viewStorage.firstObject(ofType: Storage.PaymentGatewayAccount.self, matching: nil))
376383
}

0 commit comments

Comments
 (0)