Skip to content

Commit

Permalink
Fix RevenueCatUI snapshot tests (#3526)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Dec 20, 2023
1 parent 275e21b commit d672dae
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ jobs:

spm-revenuecat-ui-ios-15:
<<: *base-job
resource_class: macos.m1.medium.gen1
steps:
- checkout
- update-spm-installation-commit
Expand All @@ -376,6 +377,7 @@ jobs:

spm-revenuecat-ui-ios-16:
<<: *base-job
resource_class: macos.m1.medium.gen1
steps:
- checkout
- update-spm-installation-commit
Expand Down Expand Up @@ -407,6 +409,7 @@ jobs:

spm-revenuecat-ui-ios-17:
<<: *base-job
resource_class: macos.m1.medium.gen1
steps:
- checkout
- update-spm-installation-commit
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ let shouldIncludeDocCPlugin = environmentVariables["INCLUDE_DOCC_PLUGIN"] == "tr

var dependencies: [Package.Dependency] = [
.package(url: "git@github.com:Quick/Nimble.git", from: "10.0.0"),
.package(url: "git@github.com:pointfreeco/swift-snapshot-testing.git", from: "1.11.0")
// SST requires iOS 13 starting from version 1.13.0
.package(url: "git@github.com:pointfreeco/swift-snapshot-testing.git", .upToNextMinor(from: "1.12.0"))
]
if shouldIncludeDocCPlugin {
dependencies.append(.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"))
Expand Down
30 changes: 24 additions & 6 deletions RevenueCatUI/Data/UserInterfaceIdiom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,39 @@ extension UserInterfaceIdiom {
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
struct UserInterfaceIdiomEnvironmentKey: EnvironmentKey {
extension EnvironmentValues {

var userInterfaceIdiom: UserInterfaceIdiom {
get { self[UserInterfaceIdiomEnvironmentKey.self] }
set { self[UserInterfaceIdiomEnvironmentKey.self] = newValue }
}

#if DEBUG
var isRunningSnapshots: Bool {
get { self[RunningSnapshotsEnvironmentKey.self] }
set { self[RunningSnapshotsEnvironmentKey.self] = newValue }
}
#endif

}

// MARK: -

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
private struct UserInterfaceIdiomEnvironmentKey: EnvironmentKey {

static var defaultValue: UserInterfaceIdiom = .default

}

#if DEBUG
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
extension EnvironmentValues {
private struct RunningSnapshotsEnvironmentKey: EnvironmentKey {

var userInterfaceIdiom: UserInterfaceIdiom {
get { self[UserInterfaceIdiomEnvironmentKey.self] }
set { self[UserInterfaceIdiomEnvironmentKey.self] = newValue }
}
static var defaultValue: Bool = false

}
#endif

// MARK: - UIKit

Expand Down
32 changes: 32 additions & 0 deletions RevenueCatUI/Views/ProgressView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ProgressView.swift
//
//
// Created by Nacho Soto on 12/20/23.
//

import Foundation
import SwiftUI

/// `SwiftUI.ProgressView` overload that becomes non-animated during snapshots.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct ProgressView: View {

#if DEBUG
@Environment(\.isRunningSnapshots)
private var isRunningSnapshots
#endif

var body: some View {
#if DEBUG
if self.isRunningSnapshots {
SwiftUI.ProgressView(value: 0.5)
} else {
SwiftUI.ProgressView()
}
#else
SwiftUI.ProgressView()
#endif
}

}
9 changes: 8 additions & 1 deletion Tests/RevenueCatUITests/BaseSnapshotTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ import XCTest
@MainActor
class BaseSnapshotTest: TestCase {

// swiftlint:disable:next unneeded_override
override class func setUp() {
super.setUp()

// See https://github.com/pointfreeco/swift-snapshot-testing/pull/702 and
// https://github.com/pointfreeco/swift-snapshot-testing/pull/666
expect(MTLCreateSystemDefaultDevice()).toNot(
beNil(),
description: "Metal is required for perceptuallyCompare, but not available on this machine."
)

// Uncomment this line to manually record snapshots:
// isRecording = true
}
Expand All @@ -60,6 +66,7 @@ class BaseSnapshotTest: TestCase {
fonts: fonts,
introEligibility: introEligibility,
purchaseHandler: purchaseHandler)
.environment(\.isRunningSnapshots, true)
}

private static func skipTestIfNeeded() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "git@github.com:pointfreeco/swift-snapshot-testing.git",
"state" : {
"revision" : "dc46eeb3928a75390651fac6c1ef7f93ad59a73b",
"version" : "1.11.1"
"revision" : "26ed3a2b4a2df47917ca9b790a57f91285b923fb",
"version" : "1.12.0"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTests/TestHelpers/SnapshotTesting+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ private let traits: UITraitCollection = .init(displayScale: 1)

#endif

private let perceptualPrecision: Float = 0.97
private let timeout: DispatchTimeInterval = .seconds(5)
private let perceptualPrecision: Float = 0.94
private let timeout: DispatchTimeInterval = .seconds(3)
private let pollInterval: DispatchTimeInterval = .milliseconds(100)

// MARK: - Private
Expand Down
2 changes: 1 addition & 1 deletion Tests/purchases-ios-snapshots-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
38eb67f4697144bb842c1e7ac45f71329887526a
70a3aed03c02fb2a3d2c47cee3377fe0c097ec48

0 comments on commit d672dae

Please sign in to comment.