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

MBL-1290: Add tests for PaymentIntent pathway in PledgePaymentMethodsViewModel #2037

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
97 changes: 97 additions & 0 deletions Library/ViewModels/PledgePaymentMethodsViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,54 @@ final class PledgePaymentMethodsViewModelTests: TestCase {
}
}

func testPaymentSheetCardPaymentIntent_UsedToNotifyDelegate_WhenPaymentSheetCardAdded_Success() {
let userTemplateWithCards = self.userTemplate
|> \.storedCards .~ UserCreditCards.withCards([UserCreditCards.visa])
let response = UserEnvelope<GraphUser>(me: userTemplateWithCards)
let mockService = MockService(fetchGraphUserResult: .success(response))

withEnvironment(apiService: mockService, currentUser: User.template) {
self.vm.inputs
.configure(with: (
User.template,
Project.template,
Reward.template,
.pledge,
.discovery,
100,
.paymentIntent
))
self.vm.inputs.viewDidLoad()

self.scheduler.run()

self.notifyDelegateCreditCardSelected.assertValues(
[PaymentSourceSelected.savedCreditCard(UserCreditCards.visa.id)],
"First card selected by default"
)

guard let paymentMethod = STPPaymentMethod.visaStripePaymentMethod else {
XCTFail("Should've created payment method.")

return
}
let paymentOption = STPPaymentMethod.sampleStringPaymentOption(paymentMethod)
let paymentOptionsDisplayData = STPPaymentMethod.samplePaymentOptionsDisplayData(paymentOption)

self.vm.inputs
.paymentSheetDidAdd(
newCard: paymentOptionsDisplayData,
clientSecret: "fake_payment_intent"
)

self.notifyDelegateCreditCardSelected.assertValues([
PaymentSourceSelected.savedCreditCard(UserCreditCards.visa.id),
PaymentSourceSelected
.paymentIntentClientSecret("fake_payment_intent")
])
}
}

func testCantSelectUnavailableCards() {
let cards = UserCreditCards.withCards([
UserCreditCards.visa,
Expand Down Expand Up @@ -1361,4 +1409,53 @@ final class PledgePaymentMethodsViewModelTests: TestCase {
XCTAssertTrue(allowedDelayedPaymentMethods)
}
}

func testGoToAddNewStripeCardScreen_LatePledgeContext_Success() {
let project = Project.template
let addNewCardIndexPath = IndexPath(
row: 0,
section: PaymentMethodsTableViewSection.addNewCard.rawValue
)
let envelope = PaymentIntentEnvelope(clientSecret: "test")
let mockService = MockService(createPaymentIntentResult: .success(envelope))
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = Strings.general_accessibility_kickstarter()
configuration.allowsDelayedPaymentMethods = true

withEnvironment(
apiService: mockService,
currentUser: User.template
) {
self.vm.inputs.viewDidLoad()
self.vm.inputs
.configure(with: (
User.template,
project,
Reward.template,
.pledge,
.discovery,
100,
.paymentIntent
))
self.vm.inputs.didSelectRowAtIndexPath(addNewCardIndexPath)

self.scheduler.run()

XCTAssertEqual(self.goToAddStripeCardIntent.values.count, 1)
XCTAssertEqual(self.goToAddStripeCardIntent.lastValue?.clientSecret, "test")
XCTAssertEqual(
self.goToAddStripeCardIntent.lastValue?.configuration.merchantDisplayName,
Strings.general_accessibility_kickstarter()
)

guard let allowedDelayedPaymentMethods = self.goToAddStripeCardIntent.lastValue?.configuration
.allowsDelayedPaymentMethods else {
XCTFail()

return
}

XCTAssertFalse(allowedDelayedPaymentMethods)
}
}
}