-
Notifications
You must be signed in to change notification settings - Fork 338
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
UI Preview Mode: mock CustomerInfo
#4786
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd372ec
feat: mock `CustomerInfo` when in UI Preview mode and avoid caching it
ajpallares 244f417
feat: return mock `CustomerInfo` when in UI Preview mode when request…
ajpallares 74142d8
tests: add unit test for `CustomerInfo` mock in UI Preview mode
ajpallares 22109e2
Merge branch 'main' into uiPreviewMode/mockCustomerInfo
ajpallares 4c2533a
fix linter complaints
ajpallares c5d17f4
Merge branch 'main' into uiPreviewMode/mockCustomerInfo
ajpallares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
Tests/UnitTests/Identity/CustomerInfoManagerUIPreviewModeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// CustomerInfoManagerUIPreviewModeTests.swift | ||
// | ||
// Created by Antonio Pallares on 14/2/25. | ||
|
||
import Foundation | ||
import Nimble | ||
import XCTest | ||
|
||
@_spi(Internal) @testable import RevenueCat | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
class CustomerInfoManagerUIPreviewModeTests: BaseCustomerInfoManagerTests { | ||
|
||
override func setUpWithError() throws { | ||
|
||
// Enable UI Preview Mode | ||
self.mockSystemInfo = MockSystemInfo( | ||
platformInfo: nil, | ||
finishTransactions: true, | ||
dangerousSettings: DangerousSettings(uiPreviewMode: true) | ||
) | ||
|
||
try super.setUpWithError() | ||
} | ||
|
||
func testFetchCustomerInfoDoesNotCallBackendInUIPreviewMode() async throws { | ||
_ = try await self.customerInfoManager.fetchAndCacheCustomerInfo( | ||
appUserID: "any_user", | ||
isAppBackgrounded: false | ||
) | ||
|
||
expect(self.mockBackend.invokedGetSubscriberData) == false | ||
} | ||
|
||
func testCustomerInfoIsNotCachedInUIPreviewMode() async throws { | ||
_ = try await self.customerInfoManager.fetchAndCacheCustomerInfo( | ||
appUserID: "any_user", | ||
isAppBackgrounded: false | ||
) | ||
|
||
expect(self.mockDeviceCache.cacheCustomerInfoCount) == 0 | ||
} | ||
|
||
func testCachedCustomerInfoInUIPreviewModeReturnsMockCustomerInfo() async throws { | ||
let info = try XCTUnwrap(self.customerInfoManager.cachedCustomerInfo(appUserID: "any_user")) | ||
|
||
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID | ||
} | ||
|
||
func testUIPreviewModeCustomerInfoHasExpectedValues() async throws { | ||
let info = try await self.customerInfoManager.fetchAndCacheCustomerInfo( | ||
appUserID: "any_user", | ||
isAppBackgrounded: false | ||
) | ||
|
||
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID | ||
} | ||
|
||
func testCustomerInfoReturnsMockInUIPreviewModeForAllFetchPolicies() async throws { | ||
let policies: [CacheFetchPolicy] = [ | ||
.fromCacheOnly, | ||
.fetchCurrent, | ||
.cachedOrFetched, | ||
.notStaleCachedOrFetched | ||
] | ||
|
||
for policy in policies { | ||
let info = try await self.customerInfoManager.customerInfo( | ||
appUserID: "any_user", | ||
fetchPolicy: policy | ||
) | ||
|
||
expect(self.mockBackend.invokedGetSubscriberData) == false | ||
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID | ||
expect(self.mockDeviceCache.cacheCustomerInfoCount) == 0 | ||
} | ||
} | ||
|
||
func testUnfinishedTransactionsIgnoredInUIPreviewMode() async throws { | ||
self.mockTransationFetcher.stubbedUnfinishedTransactions = [ | ||
Self.createTransaction(), | ||
Self.createTransaction() | ||
] | ||
|
||
mockBackend.stubbedGetCustomerInfoResult = .success(mockCustomerInfo) | ||
|
||
let info = try await self.customerInfoManager.fetchAndCacheCustomerInfo( | ||
appUserID: "any_user", | ||
isAppBackgrounded: false | ||
) | ||
|
||
expect(self.mockBackend.invokedGetSubscriberData) == false | ||
expect(self.mockTransactionPoster.invokedHandlePurchasedTransaction.value) == false | ||
expect(info.originalAppUserId) == IdentityManager.uiPreviewModeAppUserID | ||
} | ||
} | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
private extension CustomerInfoManagerUIPreviewModeTests { | ||
|
||
static func createTransaction() -> StoreTransaction { | ||
return .init(sk1Transaction: MockTransaction()) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I guess if we don't cache it, this would always be null and this might not be needed... But I guess I'm ok keeping it just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you say makes total sense. The main reason why I also put this there is to always receive a nonnil
CustomerInfo
object, even when usingCacheFetchPolicy.fromCacheOnly
. Like: in UI Preview mode, you consistently get the mockedCustomerInfo
every time and in any case (e.g. for anyCacheFetchPolicy
).