Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PaymentOptionsVC race condition where payment method loading race… #1476

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Stripe/STPPaymentOptionsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ - (instancetype)initWithConfiguration:(STPPaymentConfiguration *)configuration
apiAdapter:(id<STPBackendAPIAdapter>)apiAdapter {
STPPromise<STPPaymentOptionTuple *> *promise = [STPPromise new];
[apiAdapter listPaymentMethodsForCustomerWithCompletion:^(NSArray<STPPaymentMethod *> * _Nullable paymentMethods, NSError * _Nullable error) {
stpDispatchToMainThreadIfNecessary(^{
// We don't use stpDispatchToMainThreadIfNecessary here because we want this completion block to always be called asynchronously, so that users can set self.defaultPaymentMethod in time.
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[promise fail:error];
} else {
Expand Down
13 changes: 12 additions & 1 deletion Tests/Tests/STPPaymentOptionsViewControllerLocalizationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#import "STPFixtures.h"
#import "STPMocks.h"
#import "STPLocalizationUtils+STPTestAdditions.h"
#import "STPPromise.h"
#import "STPPaymentOptionTuple.h"

@interface STPPaymentOptionsViewController (Testing)
@property (nonatomic) STPPromise<STPPaymentOptionTuple *> *loadingPromise;
@end


@interface STPPaymentOptionsViewControllerLocalizationTests : FBSnapshotTestCase
@end
Expand All @@ -39,7 +46,11 @@ - (void)performSnapshotTestForLanguage:(NSString *)language {
theme:theme
customerContext:customerContext
delegate:delegate];

XCTestExpectation *didLoadExpectation = [self expectationWithDescription:@"VC did load"];
[paymentOptionsVC.loadingPromise onSuccess:^(STPPaymentOptionTuple * _Nonnull __unused value) {
[didLoadExpectation fulfill];
}];
[self waitForExpectations:@[didLoadExpectation] timeout:2];

UIView *viewToTest = [self stp_preparedAndSizedViewForSnapshotTestFromViewController:paymentOptionsVC];

Expand Down
24 changes: 17 additions & 7 deletions Tests/Tests/STPPaymentOptionsViewControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#import "STPFixtures.h"
#import "STPMocks.h"
#import "STPPaymentOptionsInternalViewController.h"
#import "STPPaymentOptionTuple.h"
#import "STPPromise.h"

@interface STPPaymentOptionsViewController (Testing)
@property(nonatomic, weak)UIViewController *internalViewController;
@property (nonatomic) STPPromise<STPPaymentOptionTuple *> *loadingPromise;
@end

@interface STPPaymentOptionsViewControllerTest : XCTestCase
Expand All @@ -25,12 +28,23 @@ - (STPPaymentOptionsViewController *)buildViewControllerWithCustomer:(STPCustome
paymentMethods:(NSArray<STPPaymentMethod *> *)paymentMethods
configuration:(STPPaymentConfiguration *)config
delegate:(id<STPPaymentOptionsViewControllerDelegate>)delegate {
STPTheme *theme = [STPTheme defaultTheme];
STPCustomerContext *mockCustomerContext = [STPMocks staticCustomerContextWithCustomer:customer paymentMethods:paymentMethods];
return [self buildViewControllerWithCustomerContext:mockCustomerContext configuration:config delegate:delegate];
}

- (STPPaymentOptionsViewController *)buildViewControllerWithCustomerContext:(STPCustomerContext *)customerContext
configuration:(STPPaymentConfiguration *)config
delegate:(id<STPPaymentOptionsViewControllerDelegate>)delegate {
STPTheme *theme = [STPTheme defaultTheme];
STPPaymentOptionsViewController *vc = [[STPPaymentOptionsViewController alloc] initWithConfiguration:config
theme:theme
customerContext:mockCustomerContext
customerContext:customerContext
delegate:delegate];
XCTestExpectation *didLoadExpectation = [self expectationWithDescription:@"VC did load"];
[vc.loadingPromise onSuccess:^(STPPaymentOptionTuple * _Nonnull __unused value) {
[didLoadExpectation fulfill];
}];
[self waitForExpectations:@[didLoadExpectation] timeout:2];
if (vc) {
XCTAssertNotNil(vc.view);
}
Expand Down Expand Up @@ -134,15 +148,11 @@ - (void)testInternalCancelForwardsToDelegate {
customer and the correct delegate methods should be called.
*/
- (void)testAddCardAttachesToCustomerAndFinishes {
STPTheme *theme = [STPTheme defaultTheme];
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration];
STPCustomer *customer = [STPFixtures customerWithNoSources];
STPCustomerContext *mockCustomerContext = [STPMocks staticCustomerContextWithCustomer:customer paymentMethods:@[]];
id<STPPaymentOptionsViewControllerDelegate>delegate = OCMProtocolMock(@protocol(STPPaymentOptionsViewControllerDelegate));
STPPaymentOptionsViewController *sut = [[STPPaymentOptionsViewController alloc] initWithConfiguration:config
theme:theme
customerContext:mockCustomerContext
delegate:delegate];
STPPaymentOptionsViewController *sut = [self buildViewControllerWithCustomerContext:mockCustomerContext configuration:config delegate:delegate];
XCTAssertNotNil(sut.view);
XCTAssertTrue([sut.internalViewController isKindOfClass:[STPAddCardViewController class]]);

Expand Down