Skip to content

Commit

Permalink
MBL-1338: Re-fetch stored cards after login
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-at-kickstarter committed Apr 4, 2024
1 parent d9fb398 commit 3c13876
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Library/ViewModels/PostCampaignCheckoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,18 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
// MARK: - Validate Existing Cards

/// Capture current users stored credit cards in the case that we need to validate an existing payment method
let storedCardsEvent = initialData.ignoreValues()
.switchMap { _ in
AppEnvironment.current.apiService
.fetchGraphUser(withStoredCards: true)
.ksr_debounce(.seconds(1), on: AppEnvironment.current.scheduler)
.map { envelope in (envelope, false) }
.prefix(value: (nil, true))
.materialize()
}
let storedCardsEvent = Signal.merge(
initialData.ignoreValues(),
self.userSessionStartedSignal.ignoreValues()
)
.switchMap { _ in
AppEnvironment.current.apiService
.fetchGraphUser(withStoredCards: true)
.ksr_debounce(.seconds(1), on: AppEnvironment.current.scheduler)
.map { envelope in (envelope, false) }
.prefix(value: (nil, true))
.materialize()
}

let storedCardsValues = storedCardsEvent.values()
.filter(second >>> isFalse)
Expand Down
55 changes: 55 additions & 0 deletions Library/ViewModels/PostCampaignCheckoutViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -804,4 +804,59 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge])
}
}

func testTapSubmitButton_SignedInAfterPageLoads_canValidateExistingCard() {
let paymentIntent = PaymentIntentEnvelope(clientSecret: "foo")
let validateCheckout = ValidateCheckoutEnvelope(valid: true, messages: ["message"])
let fetchedUser = GraphUser.template

withEnvironment(currentUser: nil) {
let project = Project.cosmicSurgery
let reward = Reward.noReward |> Reward.lens.minimum .~ 5

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
total: 5,
shipping: nil,
refTag: nil,
context: .latePledge,
checkoutId: "0"
)

self.vm.inputs.configure(with: data)
self.vm.inputs.viewDidLoad()

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertLastValue(false)

withEnvironment(
apiService: MockService(
createPaymentIntentResult: .success(paymentIntent),
fetchGraphUserResult: .success(UserEnvelope(me: fetchedUser)),
validateCheckoutResult: .success(validateCheckout)
),
currentUser: .template
) {
self.vm.inputs.userSessionStarted()
self.configurePledgeViewCTAContainerViewIsLoggedIn.assertLastValue(true)

self.vm.inputs.creditCardSelected(
source: .savedCreditCard(UserCreditCards.amex.id),
paymentMethodId: UserCreditCards.amex.id,
isNewPaymentMethod: false
)

self.scheduler.run()

self.vm.inputs.submitButtonTapped()

self.scheduler.run()

self.validateCheckoutSuccess.assertDidEmitValue()
}
}
}
}

0 comments on commit 3c13876

Please sign in to comment.