Skip to content

Commit

Permalink
UI preview mode/always fetch offerings (#4754)
Browse files Browse the repository at this point in the history
* feat: force fetch Offerings from network when in UI Preview mode

* test: add unit test for force fetching Offerings from network when in UI Preview mode
  • Loading branch information
ajpallares authored Feb 3, 2025
1 parent fa3ea32 commit 7558e50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Purchasing/OfferingsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OfferingsManager {
fetchCurrent: Bool = false,
completion: (@MainActor @Sendable (Result<Offerings, Error>) -> Void)?
) {
guard !fetchCurrent else {
guard !fetchCurrent && !self.systemInfo.dangerousSettings.uiPreviewMode else {
self.fetchFromNetwork(appUserID: appUserID, fetchPolicy: fetchPolicy, completion: completion)
return
}
Expand Down
38 changes: 38 additions & 0 deletions Tests/UnitTests/Purchasing/OfferingsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,44 @@ extension OfferingsManagerTests {
expect(self.mockProductsManager.invokedProducts) == false
}

func testOfferingsForAppUserIdForcesNetworkRequestWhenUIPreviewModeIsTrueAndFetchCurrentIsFalse() throws {
// given
let mockSystemInfoWithPreviewMode = MockSystemInfo(
platformInfo: .init(flavor: "iOS", version: "3.2.1"),
finishTransactions: true,
dangerousSettings: DangerousSettings(uiPreviewMode: true)
)

self.offeringsManager = OfferingsManager(
deviceCache: self.mockDeviceCache,
operationDispatcher: self.mockOperationDispatcher,
systemInfo: mockSystemInfoWithPreviewMode,
backend: self.mockBackend,
offeringsFactory: self.mockOfferingsFactory,
productsManager: self.mockProductsManager
)

self.mockOfferings.stubbedGetOfferingsCompletionResult = .success(MockData.anyBackendOfferingsResponse)
self.mockDeviceCache.stubbedOfferings = MockData.sampleOfferings

// when
let result = waitUntilValue { completed in
self.offeringsManager.offerings(appUserID: MockData.anyAppUserID, fetchCurrent: false) {
completed($0)
}
}

// then
expect(result).to(beSuccess())
expect(result?.value) !== MockData.sampleOfferings
expect(result?.value?["base"]).toNot(beNil())
expect(result?.value?["base"]!.monthly).toNot(beNil())
expect(result?.value?["base"]!.monthly?.storeProduct).toNot(beNil())

expect(self.mockOfferings.invokedGetOfferingsForAppUserID) == true
expect(self.mockDeviceCache.cacheOfferingsCount) == 1
}

}

private extension OfferingsManagerTests {
Expand Down

0 comments on commit 7558e50

Please sign in to comment.