Skip to content

Commit

Permalink
Paywalls: added tests for PackageType filtering (#2810)
Browse files Browse the repository at this point in the history
Some missing tests for the logic added in #2798.
I've also moved this function to be fined in `PaywallData` instead of
the `TemplateViewType` protocol, which makes more sense.
  • Loading branch information
NachoSoto committed Sep 6, 2023
1 parent e8c525d commit ab4ede9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion RevenueCatUI/Templates/Example1Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Example1Template: TemplateViewType {
self.data = .failure(.noPackages)
} else {
let allPackages = paywall.config.packages
let packages = Self.filter(packages: packages, with: allPackages)
let packages = PaywallData.filter(packages: packages, with: allPackages)

if let package = packages.first {
self.data = .success(.init(
Expand Down
4 changes: 0 additions & 4 deletions RevenueCatUI/Templates/TemplateViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ extension PaywallData {
)
}
}
}

@available(iOS 16.0, macOS 13.0, tvOS 16.0, *)
extension TemplateViewType {

static func filter(packages: [Package], with list: [PackageType]) -> [Package] {
// Only subscriptions are supported at the moment
Expand Down
92 changes: 92 additions & 0 deletions Tests/RevenueCatUITests/PackageFilteringTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// PackageFilteringTests.swift
//
//
// Created by Nacho Soto on 7/13/23.
//

import Nimble
import RevenueCat
@testable import RevenueCatUI
import XCTest

@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
class PackageFilteringTests: TestCase {

func testFilterNoPackages() {
expect(PaywallData.filter(packages: [], with: [.monthly])) == []
}

func testFilterPackagesWithEmptyList() {
expect(PaywallData.filter(packages: [Self.monthly], with: [])) == []
}

func testFilterOutSinglePackge() {
expect(PaywallData.filter(packages: [Self.monthly], with: [.annual])) == []
}

func testFilterOutNonSubscriptions() {
expect(PaywallData.filter(packages: [Self.consumable], with: [.custom])) == []
}

func testFilterByPackageType() {
expect(PaywallData.filter(packages: [Self.monthly, Self.annual], with: [.monthly])) == [Self.monthly]
}

func testFilterWithDuplicatedPackageTypes() {
expect(PaywallData.filter(packages: [Self.monthly, Self.annual], with: [.monthly, .monthly])) == [
Self.monthly,
Self.monthly
]
}

func testFilterReturningMultiplePackages() {
expect(PaywallData.filter(packages: [Self.weekly, Self.monthly, Self.annual], with: [.weekly, .monthly])) == [
Self.weekly,
Self.monthly
]
}

}

@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
private extension PackageFilteringTests {

static let weekly = Package(
identifier: "weekly",
packageType: .weekly,
storeProduct: TestData.productWithIntroOffer.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)
static let monthly = Package(
identifier: "monthly",
packageType: .monthly,
storeProduct: TestData.productWithIntroOffer.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)
static let annual = Package(
identifier: "annual",
packageType: .annual,
storeProduct: TestData.productWithNoIntroOffer.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)

static let consumable = Package(
identifier: "consumable",
packageType: .custom,
storeProduct: consumableProduct.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)

private static let consumableProduct = TestStoreProduct(
localizedTitle: "Coins",
price: 199.99,
localizedPriceString: "$199.99",
productIdentifier: "com.revenuecat.coins",
productType: .consumable,
localizedDescription: "Coins"
)

private static let offeringIdentifier = "offering"

}

0 comments on commit ab4ede9

Please sign in to comment.