Skip to content

Commit

Permalink
[MBL-1604] Show bonus in add-ons (#2129)
Browse files Browse the repository at this point in the history
* Show (non-functional) bonus support

* Wire up bonus support

* Validate bonus amount

* Use bonus amount in confirm details screen

* Copy add ons snapshot tests for no shipping case

* Add tests

* Update button cta and rerecord snapshots

* Add ticket number in TODO
  • Loading branch information
ifosli authored Aug 22, 2024
1 parent 0986687 commit 4db92f0
Show file tree
Hide file tree
Showing 29 changed files with 450 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ final class RewardAddOnSelectionNoShippingViewController: UIViewController {
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()

/// Bonus support
private lazy var pledgeAmountViewController = {
PledgeAmountViewController.instantiate()
|> \.delegate .~ self
}()

public weak var pledgeViewDelegate: PledgeViewControllerDelegate?
public weak var noShippingPledgeViewDelegate: NoShippingPledgeViewControllerDelegate?

Expand Down Expand Up @@ -94,8 +100,11 @@ final class RewardAddOnSelectionNoShippingViewController: UIViewController {
|> ksr_addSubviewToParent()
|> ksr_constrainViewToEdgesInParent(priority: UILayoutPriority(rawValue: 999))

_ = ([self.headerLabel], self.headerRootStackView)
_ = ([self.headerLabel, self.pledgeAmountViewController.view], self.headerRootStackView)
|> ksr_addArrangedSubviewsToStackView()

self.addChild(self.pledgeAmountViewController)
self.pledgeAmountViewController.didMove(toParent: self)
}

private func setupConstraints() {
Expand Down Expand Up @@ -147,6 +156,12 @@ final class RewardAddOnSelectionNoShippingViewController: UIViewController {
self?.continueCTAView.configure(with: data)
}

self.viewModel.outputs.configurePledgeAmountViewWithData
.observeForUI()
.observeValues { [weak self] data in
self?.pledgeAmountViewController.configureWith(value: data)
}

self.viewModel.outputs.loadAddOnRewardsIntoDataSourceAndReloadTableView
.observeForUI()
.observeValues { [weak self] items in
Expand Down Expand Up @@ -242,6 +257,17 @@ extension RewardAddOnSelectionNoShippingViewController: RewardAddOnCardViewDeleg
}
}

// MARK: - PledgeAmountViewControllerDelegate

extension RewardAddOnSelectionNoShippingViewController: PledgeAmountViewControllerDelegate {
func pledgeAmountViewController(
_: PledgeAmountViewController,
didUpdateWith data: PledgeAmountData
) {
self.viewModel.inputs.pledgeAmountViewControllerDidUpdate(with: data)
}
}

// MARK: - UITableViewDelegate

extension RewardAddOnSelectionNoShippingViewController: UITableViewDelegate {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
@testable import Kickstarter_Framework
@testable import KsApi
@testable import Library
import Prelude
import SnapshotTesting
import UIKit

final class RewardAddOnSelectionNoShippingViewControllerTests: TestCase {
override func setUp() {
super.setUp()

AppEnvironment.pushEnvironment(mainBundle: Bundle.framework)
UIView.setAnimationsEnabled(false)
}

override func tearDown() {
AppEnvironment.popEnvironment()
UIView.setAnimationsEnabled(true)

super.tearDown()
}

func testView() {
let reward = Reward.template
|> Reward.lens.shipping.enabled .~ false
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let noShippingAddOn = Reward.template
|> Reward.lens.shipping.enabled .~ false
|> Reward.lens.shipping.preference .~ Reward.Shipping.Preference.none
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let project = Project.template
|> Project.lens.rewardData.rewards .~ [reward]
|> Project.lens.rewardData.addOns .~ [noShippingAddOn]

let mockService = MockService(fetchRewardAddOnsSelectionViewRewardsResult: .success(project))

let mockRemoteConfigClient = MockRemoteConfigClient()
|> \.features .~ [RemoteConfigFeature.noShippingAtCheckout.rawValue: true]

orthogonalCombos(Language.allLanguages, Device.allCases).forEach { language, device in
withEnvironment(
apiService: mockService,
language: language,
remoteConfigClient: mockRemoteConfigClient
) {
let controller = RewardAddOnSelectionNoShippingViewController.instantiate()

let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: nil,
selectedQuantities: [:],
selectedLocationId: nil,
refTag: nil,
context: .pledge
)
controller.configure(with: data)
let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)
parent.view.frame.size.height = 600

self.scheduler.advance()

assertSnapshot(
matching: parent.view,
as: .image(perceptualPrecision: 0.98),
named: "lang_\(language)_device_\(device)"
)
}
}
}

func testView_EmptyState() {
let shippingRules = [
ShippingRule.template
|> ShippingRule.lens.location .~ .brooklyn,
ShippingRule.template
|> ShippingRule.lens.location .~ .canada,
ShippingRule.template
|> ShippingRule.lens.location .~ .australia
]

let shippingRule = ShippingRule.template
|> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 55)

let reward = Reward.template
|> Reward.lens.shipping.enabled .~ true
|> Reward.lens.shipping.preference .~ .restricted
|> Reward.lens.id .~ 99
|> Reward.lens.shippingRules .~ [shippingRule]
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let shippingAddOn1 = Reward.template
|> Reward.lens.id .~ 2
|> Reward.lens.shipping.enabled .~ true
|> Reward.lens.shippingRules .~ [
shippingRule |> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 99)
]
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let shippingAddOn2 = Reward.template
|> Reward.lens.id .~ 3
|> Reward.lens.shipping.enabled .~ true
|> Reward.lens.shippingRules .~ [
shippingRule |> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 99)
]
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let shippingAddOn3 = Reward.template
|> Reward.lens.id .~ 4
|> Reward.lens.shipping.enabled .~ true
|> Reward.lens.shippingRules .~ [
shippingRule |> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 3)
]
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let shippingAddOn4 = Reward.template
|> Reward.lens.id .~ 5
|> Reward.lens.shipping.enabled .~ true
|> Reward.lens.shippingRules .~ [
shippingRule |> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 3)
]
|> Reward.lens.localPickup .~ nil
|> Reward.lens.isAvailable .~ true

let project = Project.template
|> Project.lens.rewardData.rewards .~ [reward]
|> Project.lens.rewardData.addOns .~ [
shippingAddOn1,
shippingAddOn2,
shippingAddOn3,
shippingAddOn4
]

let mockService = MockService(
fetchShippingRulesResult: .success(shippingRules),
fetchRewardAddOnsSelectionViewRewardsResult: .success(project)
)

let mockRemoteConfigClient = MockRemoteConfigClient()
|> \.features .~ [RemoteConfigFeature.noShippingAtCheckout.rawValue: true]

orthogonalCombos(Language.allLanguages, Device.allCases).forEach { language, device in
withEnvironment(
apiService: mockService,
language: language,
remoteConfigClient: mockRemoteConfigClient
) {
let controller = RewardAddOnSelectionNoShippingViewController.instantiate()
let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: nil,
selectedQuantities: [:],
selectedLocationId: nil,
refTag: nil,
context: .pledge
)
controller.configure(with: data)
let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)
parent.view.frame.size.height = 600

self.scheduler.advance()

self.scheduler.advance(by: .seconds(1))

assertSnapshot(
matching: parent.view,
as: .image(perceptualPrecision: 0.98),
named: "lang_\(language)_device_\(device)"
)
}
}
}

func testView_Error() {
let reward = Reward.template
|> Reward.lens.shipping.enabled .~ false
|> Reward.lens.localPickup .~ nil
let project = Project.template

let mockService = MockService(fetchRewardAddOnsSelectionViewRewardsResult: .failure(.couldNotParseJSON))

let mockRemoteConfigClient = MockRemoteConfigClient()
|> \.features .~ [RemoteConfigFeature.noShippingAtCheckout.rawValue: true]

orthogonalCombos(Language.allLanguages, Device.allCases).forEach { language, device in
withEnvironment(
apiService: mockService,
language: language,
remoteConfigClient: mockRemoteConfigClient
) {
let controller = RewardAddOnSelectionNoShippingViewController.instantiate()

let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: nil,
selectedQuantities: [:],
selectedLocationId: nil,
refTag: nil,
context: .pledge
)
controller.configure(with: data)
let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)
parent.view.frame.size.height = 600

self.scheduler.advance()

assertSnapshot(
matching: parent.view,
as: .image(perceptualPrecision: 0.98),
named: "lang_\(language)_device_\(device)"
)
}
}
}

func testView_NoShippingWithLocalPickup_Success() {
let reward = Reward.template
|> Reward.lens.shipping.enabled .~ false
|> Reward.lens.localPickup .~ .australia
|> Reward.lens.shipping.preference .~ .local
|> Reward.lens.isAvailable .~ true

let noShippingAddOn = Reward.template
|> Reward.lens.shipping.enabled .~ false
|> Reward.lens.shipping.preference .~ .local
|> Reward.lens.localPickup .~ .australia
|> Reward.lens.isAvailable .~ true

let project = Project.template
|> Project.lens.rewardData.rewards .~ [reward]
|> Project.lens.rewardData.addOns .~ [noShippingAddOn]

let mockService = MockService(fetchRewardAddOnsSelectionViewRewardsResult: .success(project))

let mockRemoteConfigClient = MockRemoteConfigClient()
|> \.features .~ [RemoteConfigFeature.noShippingAtCheckout.rawValue: true]

orthogonalCombos(Language.allLanguages, [Device.phone5_8inch, Device.pad]).forEach { language, device in
withEnvironment(
apiService: mockService,
language: language,
remoteConfigClient: mockRemoteConfigClient
) {
let controller = RewardAddOnSelectionNoShippingViewController.instantiate()

let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: nil,
selectedQuantities: [:],
selectedLocationId: nil,
refTag: nil,
context: .pledge
)
controller.configure(with: data)
let (parent, _) = traitControllers(device: device, orientation: .portrait, child: controller)
parent.view.frame.size.height = 600

self.scheduler.advance()

assertSnapshot(
matching: parent.view,
as: .image(perceptualPrecision: 0.98),
named: "lang_\(language)_device_\(device)"
)
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Kickstarter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
37FEFBC8222F1E4F00FCA608 /* ProcessInfoType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FEFBC7222F1E4F00FCA608 /* ProcessInfoType.swift */; };
392BB12A2C3BEB5600A5591B /* PPOEmptyStateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392BB1292C3BEB5500A5591B /* PPOEmptyStateView.swift */; };
392BB12C2C3C095200A5591B /* PPOEmptyStateViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392BB12B2C3C095200A5591B /* PPOEmptyStateViewTests.swift */; };
392E1CC32C7678AD0065016B /* RewardAddOnSelectionNoShippingViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392E1CC12C7678970065016B /* RewardAddOnSelectionNoShippingViewControllerTests.swift */; };
393E8DA12C5BE07C00AEA968 /* PPOBackingFragment.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 393E8DA02C5BE07C00AEA968 /* PPOBackingFragment.graphql */; };
393E8DA32C5BE08500AEA968 /* PPOCardFragment.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 393E8DA22C5BE08500AEA968 /* PPOCardFragment.graphql */; };
393E8DA52C5BE08B00AEA968 /* PPOProjectFragment.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 393E8DA42C5BE08B00AEA968 /* PPOProjectFragment.graphql */; };
Expand Down Expand Up @@ -1969,6 +1970,7 @@
37FEFBC7222F1E4F00FCA608 /* ProcessInfoType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessInfoType.swift; sourceTree = "<group>"; };
392BB1292C3BEB5500A5591B /* PPOEmptyStateView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PPOEmptyStateView.swift; sourceTree = "<group>"; };
392BB12B2C3C095200A5591B /* PPOEmptyStateViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PPOEmptyStateViewTests.swift; sourceTree = "<group>"; };
392E1CC12C7678970065016B /* RewardAddOnSelectionNoShippingViewControllerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RewardAddOnSelectionNoShippingViewControllerTests.swift; sourceTree = "<group>"; };
393E8DA02C5BE07C00AEA968 /* PPOBackingFragment.graphql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = PPOBackingFragment.graphql; sourceTree = "<group>"; };
393E8DA22C5BE08500AEA968 /* PPOCardFragment.graphql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = PPOCardFragment.graphql; sourceTree = "<group>"; };
393E8DA42C5BE08B00AEA968 /* PPOProjectFragment.graphql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = PPOProjectFragment.graphql; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4052,6 +4054,7 @@
isa = PBXGroup;
children = (
6035AFA82C517EDD007E28FC /* RewardAddOnSelectionNoShippingViewController.swift */,
392E1CC12C7678970065016B /* RewardAddOnSelectionNoShippingViewControllerTests.swift */,
8ACB32A724ABC2DB00A03968 /* RewardAddOnSelectionViewController.swift */,
8A64F16524BE6528004917E2 /* RewardAddOnSelectionViewControllerTests.swift */,
);
Expand Down Expand Up @@ -8689,6 +8692,7 @@
A7ED20431E8323E900BFFA01 /* EmptyStatesViewControllerTests.swift in Sources */,
70B1889429A521000004E293 /* FacebookResetPasswordViewControllerTests.swift in Sources */,
A7ED20421E8323E900BFFA01 /* SortPagerViewControllerTests.swift in Sources */,
392E1CC32C7678AD0065016B /* RewardAddOnSelectionNoShippingViewControllerTests.swift in Sources */,
D6B6F91820EE955B00A295F7 /* SettingsNotificationsViewControllerTests.swift in Sources */,
94114D90265488720063E8F6 /* CommentCellHeaderStackViewTests.swift in Sources */,
A7ED20181E83229E00BFFA01 /* FindFriendsDataSourceTests.swift in Sources */,
Expand Down
14 changes: 8 additions & 6 deletions Library/ViewModels/ConfirmDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ public class ConfirmDetailsViewModel: ConfirmDetailsViewModelType, ConfirmDetail
calculatedShippingTotal
)

/// Initial pledge amount is zero if not backed.
let initialPledgeAmount = Signal.merge(
initialData.filter { $0.project.personalization.backing == nil }.mapConst(0.0),
backing.map(\.bonusAmount)
)
.take(first: 1)
/// If initial data includes a custom pledge amount, use that.
/// If not, bonus amount is 0 if there's no backing.
let initialPledgeAmount = Signal.zip(initialData.map(\.bonusSupport), project)
.map { bonusSupport, project in
if let bonusSupport { return bonusSupport }
if let backing = project.personalization.backing { return backing.bonusAmount }
return 0.0
}

/// Called when pledge or bonus is updated by backer
let additionalPledgeAmount = Signal.merge(
Expand Down
Loading

0 comments on commit 4db92f0

Please sign in to comment.