Skip to content

Commit

Permalink
TrialOrIntroPriceEligibilityChecker: fixed integration test to ensu…
Browse files Browse the repository at this point in the history
…re receipt is always loaded (#1726)

The reason the receipt data is required, is because if it's `nil`, it ends up becoming empty because of `data ?? Data()`, but `GetIntroEligibilityOperation` never actually runs because that's empty.

To make sure the test doesn't fail, we begin by ensuring the receipt data is loaded first.

Also documented the reason for `ReceiptRefreshPolicy.never`.
  • Loading branch information
NachoSoto authored Jun 23, 2022
1 parent c040617 commit e1d1a2b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Sources/Purchasing/TrialOrIntroPriceEligibilityChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class TrialOrIntroPriceEligibilityChecker {

func sk1CheckEligibility(_ productIdentifiers: [String],
completion: @escaping ReceiveIntroEligibilityBlock) {
receiptFetcher.receiptData(refreshPolicy: .never) { data in
// We don't want to refresh receipts because it will likely prompt the user for their credentials,
// and intro eligibility is triggered programmatically.
self.receiptFetcher.receiptData(refreshPolicy: .never) { data in
if #available(iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 6.2, *),
let data = data {
self.sk1CheckEligibility(with: data,
Expand Down
6 changes: 6 additions & 0 deletions Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ class StoreKit1IntegrationTests: BaseBackendIntegrationTests {
func testEligibleForIntroBeforePurchase() async throws {
try AvailabilityChecks.iOS13APIAvailableOrSkipTest()

if Self.storeKit2Setting == .disabled {
// SK1 implementation relies on the receipt being loaded already.
// See `TrialOrIntroPriceEligibilityChecker.sk1CheckEligibility`
_ = try await Purchases.shared.restorePurchases()
}

let product = try await self.monthlyPackage.storeProduct

let eligibility = await Purchases.shared.checkTrialOrIntroDiscountEligibility(product: product)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TrialOrIntroPriceEligibilityCheckerSK1Tests: StoreKitConfigTestCase {

expect(self.receiptFetcher.receiptDataCalled) == false

trialOrIntroPriceEligibilityChecker!.sk1CheckEligibility([]) { _ in }
self.trialOrIntroPriceEligibilityChecker.sk1CheckEligibility([]) { _ in }

expect(self.receiptFetcher.receiptDataCalled) == true
expect(self.receiptFetcher.receiptDataReceivedRefreshPolicy) == .never
Expand Down

0 comments on commit e1d1a2b

Please sign in to comment.