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-1338: Re-fetch stored cards after login #2023

Merged
merged 2 commits into from
Apr 4, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
}
}
}
}