Skip to content

Commit

Permalink
Improved logic and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 20, 2023
1 parent 9c141a4 commit 4c20917
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
61 changes: 54 additions & 7 deletions RevenueCatUI/Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ internal enum TestData {
#endif

static let customerInfo: CustomerInfo = {
let json = """
return .decode(
"""
{
"schema_version": "4",
"request_date": "2022-03-08T17:42:58Z",
Expand All @@ -463,13 +464,46 @@ internal enum TestData {
}
}
"""
)
}()

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601

// swiftlint:disable:next force_try
return try! decoder.decode(CustomerInfo.self, from: Data(json.utf8))
static let customerInfoWithSubscriptions: CustomerInfo = {
return .decode(
"""
{
"schema_version": "4",
"request_date": "2022-03-08T17:42:58Z",
"request_date_ms": 1646761378845,
"subscriber": {
"first_seen": "2022-03-08T17:42:58Z",
"last_seen": "2022-03-08T17:42:58Z",
"management_url": "https://apps.apple.com/account/subscriptions",
"non_subscriptions": {
},
"original_app_user_id": "$RCAnonymousID:5b6fdbac3a0c4f879e43d269ecdf9ba1",
"original_application_version": "1.0",
"original_purchase_date": "2022-04-12T00:03:24Z",
"other_purchases": {
},
"subscriptions": {
"com.revenuecat.product": {
"billing_issues_detected_at": null,
"expires_date": "2062-04-12T00:03:35Z",
"grace_period_expires_date": null,
"is_sandbox": true,
"original_purchase_date": "2022-04-12T00:03:28Z",
"period_type": "intro",
"purchase_date": "2022-04-12T00:03:28Z",
"store": "app_store",
"unsubscribe_detected_at": null
},
},
"entitlements": {
}
}
}
"""
)
}()

static let localization1: PaywallData.LocalizedConfiguration = .init(
Expand Down Expand Up @@ -540,4 +574,17 @@ extension PackageType {

}

private extension CustomerInfo {

static func decode(_ json: String) -> Self {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601

// swiftlint:disable:next force_try
return try! decoder.decode(Self.self, from: Data(json.utf8))
}

}

#endif
6 changes: 3 additions & 3 deletions RevenueCatUI/Purchasing/PurchaseHandler+TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import RevenueCat
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
extension PurchaseHandler {

static func mock() -> Self {
static func mock(customerInfo: CustomerInfo = TestData.customerInfo) -> Self {
return self.init(
purchases: MockPurchases { _ in
return (
transaction: nil,
customerInfo: TestData.customerInfo,
customerInfo: customerInfo,
userCancelled: false
)
} restorePurchases: {
return TestData.customerInfo
return customerInfo
} trackEvent: { event in
Logger.debug("Tracking event: \(event)")
}
Expand Down
7 changes: 5 additions & 2 deletions RevenueCatUI/Purchasing/PurchaseHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ extension PurchaseHandler {
return result
}

/// - Returns: `success` is `true` only when the resulting `CustomerInfo`
/// had any transactions
@MainActor
func restorePurchases() async throws -> CustomerInfo {
func restorePurchases() async throws -> (info: CustomerInfo, success: Bool) {
self.actionInProgress = true
defer { self.actionInProgress = false }

Expand All @@ -103,7 +105,8 @@ extension PurchaseHandler {
self.restoredCustomerInfo = customerInfo
}

return customerInfo
return (customerInfo,
success: !customerInfo.activeSubscriptions.isEmpty)
}

func trackPaywallImpression(_ eventData: PaywallEvent.Data) {
Expand Down
5 changes: 2 additions & 3 deletions RevenueCatUI/Views/FooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ private struct RestorePurchasesButton: View {

var body: some View {
AsyncButton {
let customerInfo = try await self.purchaseHandler.restorePurchases()

if !customerInfo.entitlements.active.isEmpty {
let success = try await self.purchaseHandler.restorePurchases().success
if success {
self.displayRestoredAlert = true
}
} label: {
Expand Down
13 changes: 12 additions & 1 deletion Tests/RevenueCatUITests/Purchasing/PurchaseHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,24 @@ class PurchaseHandlerTests: TestCase {
func testRestorePurchases() async throws {
let handler: PurchaseHandler = .mock()

_ = try await handler.restorePurchases()
let result = try await handler.restorePurchases()

expect(result.info) === TestData.customerInfo
expect(result.success) == false
expect(handler.restored) == true
expect(handler.restoredCustomerInfo) === TestData.customerInfo
expect(handler.purchasedCustomerInfo).to(beNil())
expect(handler.actionInProgress) == false
}

func testRestorePurchasesWithNoTransactions() async throws {
let handler: PurchaseHandler = .mock(customerInfo: TestData.customerInfoWithSubscriptions)

let result = try await handler.restorePurchases()
expect(result.info) === TestData.customerInfoWithSubscriptions
expect(result.success) == true
}

}

#endif

0 comments on commit 4c20917

Please sign in to comment.