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-1352] Backfill Tests In PostCampaignViewModel #2025

Merged
merged 3 commits into from
Apr 4, 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
228 changes: 225 additions & 3 deletions Library/ViewModels/PostCampaignCheckoutViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,251 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
PostCampaignPaymentAuthorizationData,
Never
>()
fileprivate let checkoutComplete = TestObserver<ThanksPageData, Never>()
fileprivate let processingViewIsHidden = TestObserver<Bool, Never>()
fileprivate let validateCheckoutSuccess = TestObserver<PaymentSourceValidation, Never>()
private let checkoutComplete = TestObserver<ThanksPageData, Never>()
private let paymentMethodsViewHidden = TestObserver<Bool, Never>()
private let processingViewIsHidden = TestObserver<Bool, Never>()
private let validateCheckoutSuccess = TestObserver<PaymentSourceValidation, Never>()

private let goToLoginSignup = TestObserver<(LoginIntent, Project, Reward?), Never>()

private let configurePaymentMethodsViewControllerWithUser = TestObserver<User, Never>()
private let configurePaymentMethodsViewControllerWithProject = TestObserver<Project, Never>()
private let configurePaymentMethodsViewControllerWithReward = TestObserver<Reward, Never>()
private let configurePaymentMethodsViewControllerWithContext = TestObserver<PledgeViewContext, Never>()

private let configurePledgeViewCTAContainerViewIsLoggedIn = TestObserver<Bool, Never>()
private let configurePledgeViewCTAContainerViewIsEnabled = TestObserver<Bool, Never>()
private let configurePledgeViewCTAContainerViewContext = TestObserver<PledgeViewContext, Never>()

private let configureStripeIntegrationMerchantId = TestObserver<String, Never>()
private let configureStripeIntegrationPublishableKey = TestObserver<String, Never>()

private let showWebHelp = TestObserver<HelpType, Never>()

override func setUp() {
super.setUp()
self.vm.goToApplePayPaymentAuthorization.observe(self.goToApplePayPaymentAuthorization.observer)
self.vm.checkoutComplete.observe(self.checkoutComplete.observer)
self.vm.processingViewIsHidden.observe(self.processingViewIsHidden.observer)
self.vm.validateCheckoutSuccess.observe(self.validateCheckoutSuccess.observer)

self.vm.outputs.goToLoginSignup.observe(self.goToLoginSignup.observer)

self.vm.outputs.paymentMethodsViewHidden.observe(self.paymentMethodsViewHidden.observer)

self.vm.outputs.configurePaymentMethodsViewControllerWithValue.map { $0.0 }
.observe(self.configurePaymentMethodsViewControllerWithUser.observer)
self.vm.outputs.configurePaymentMethodsViewControllerWithValue.map { $0.1 }
.observe(self.configurePaymentMethodsViewControllerWithProject.observer)
self.vm.outputs.configurePaymentMethodsViewControllerWithValue.map { $0.2 }
.observe(self.configurePaymentMethodsViewControllerWithReward.observer)
self.vm.outputs.configurePaymentMethodsViewControllerWithValue.map { $0.3 }
.observe(self.configurePaymentMethodsViewControllerWithContext.observer)

self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.0 }
.observe(self.configurePledgeViewCTAContainerViewIsLoggedIn.observer)
self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.1 }
.observe(self.configurePledgeViewCTAContainerViewIsEnabled.observer)
self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.2 }
.observe(self.configurePledgeViewCTAContainerViewContext.observer)

self.vm.outputs.configureStripeIntegration.map(first)
.observe(self.configureStripeIntegrationMerchantId.observer)
self.vm.outputs.configureStripeIntegration.map(second)
.observe(self.configureStripeIntegrationPublishableKey.observer)

self.vm.outputs.showWebHelp.observe(self.showWebHelp.observer)
}

// MARK: - Login/Signup

func testLoginSignup_NoCardSelected_CTADisabled() {
let project = Project.template
let reward = Reward.template
let user = User.template

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

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

self.configurePaymentMethodsViewControllerWithUser.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithProject.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithReward.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithContext.assertDidNotEmitValue()

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge])

self.paymentMethodsViewHidden.assertValues([true])

withEnvironment(currentUser: user) {
self.vm.inputs.userSessionStarted()

self.configurePaymentMethodsViewControllerWithUser.assertValues([user])
self.configurePaymentMethodsViewControllerWithProject.assertValues([project])
self.configurePaymentMethodsViewControllerWithReward.assertValues([reward])
self.configurePaymentMethodsViewControllerWithContext.assertValues([.latePledge])

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false, true])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, false])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge])

self.paymentMethodsViewHidden.assertValues([true, false])
}
}
}

func testLoginSignup_CardSelected_CTAEnabled() {
let project = Project.template
let reward = Reward.template
let user = User.template

withEnvironment(currentUser: nil) {
let data = PostCampaignCheckoutData(
project: .template,
baseReward: .template,
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.configurePaymentMethodsViewControllerWithUser.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithProject.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithReward.assertDidNotEmitValue()
self.configurePaymentMethodsViewControllerWithContext.assertDidNotEmitValue()

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge])

self.paymentMethodsViewHidden.assertValues([true])

withEnvironment(currentUser: user) {
self.vm.inputs.userSessionStarted()

self.configurePaymentMethodsViewControllerWithUser.assertValues([user])
self.configurePaymentMethodsViewControllerWithProject.assertValues([project])
self.configurePaymentMethodsViewControllerWithReward.assertValues([reward])
self.configurePaymentMethodsViewControllerWithContext.assertValues([.latePledge])

let paymentSource = PaymentSourceSelected.paymentIntentClientSecret("123")
self.vm.inputs
.creditCardSelected(source: paymentSource, paymentMethodId: "123", isNewPaymentMethod: true)

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false, true, true])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, false, true])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge, .latePledge])

self.paymentMethodsViewHidden.assertValues([true, false])
}
}
}

// MARK: - Web Help

func testShowWebHelp() {
self.vm.inputs.viewDidLoad()

self.vm.inputs.termsOfUseTapped(with: .terms)

self.showWebHelp.assertValues([HelpType.terms])
}

func testShowWebHelpLearnMore() {
self.vm.inputs.viewDidLoad()

self.vm.inputs.pledgeDisclaimerViewDidTapLearnMore()

self.showWebHelp.assertValues([HelpType.trust])
}

// MARK: - Stripe

func testStripeConfiguration_StagingEnvironment() {
let mockService = MockService(serverConfig: ServerConfig.staging)

withEnvironment(apiService: mockService) {
self.configureStripeIntegrationMerchantId.assertDidNotEmitValue()
self.configureStripeIntegrationPublishableKey.assertDidNotEmitValue()

let project = Project.template
let reward = Reward.template

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

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

self.configureStripeIntegrationMerchantId.assertValues([Secrets.ApplePay.merchantIdentifier])
self.configureStripeIntegrationPublishableKey.assertValues([Secrets.StripePublishableKey.staging])
}
}

func testStripeConfiguration_ProductionEnvironment() {
let mockService = MockService(serverConfig: ServerConfig.production)

withEnvironment(apiService: mockService) {
self.configureStripeIntegrationMerchantId.assertDidNotEmitValue()

let project = Project.template
let reward = Reward.template

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

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

self.configureStripeIntegrationMerchantId.assertValues([Secrets.ApplePay.merchantIdentifier])
self.configureStripeIntegrationPublishableKey.assertValues([Secrets.StripePublishableKey.production])
}
}

// MARK: - Apple Pay

func testApplePayAuthorization_noReward_isCorrect() {
let project = Project.cosmicSurgery
let reward = Reward.noReward |> Reward.lens.minimum .~ 5
Expand Down