Skip to content

Commit

Permalink
Paywalls: fixed tests on CI (#2872)
Browse files Browse the repository at this point in the history
- Added JUnit test reporting for CircleCI:
<img width="624" alt="Screenshot 2023-07-25 at 11 02 52"
src="https://github.com/RevenueCat/purchases-ios/assets/685609/a6e73b34-6d90-4b88-aa4d-f108bab47d21">

- Fixed `macOS` build
- Fixed snapshots
- Uploading `.xcarchive` to CircleCI to inspect failures (like
Snapshots)
- Simplified snapshot testing
  • Loading branch information
NachoSoto committed Aug 28, 2023
1 parent ebb1dbc commit 96f053f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 60 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ jobs:
no_output_timeout: 5m
environment:
SCAN_DEVICE: iPhone 14,OS=16.4
- compress_result_bundle:
directory: fastlane/test_output
bundle_name: revenuecatui
- store_test_results:
path: fastlane/test_output/revenuecatui/tests.xml
- store_artifacts:
path: fastlane/test_output
destination: scan-test-output
- run:
name: RevenueCatUI API Tests
command: bundle exec fastlane build_revenuecatui_api_tester
Expand Down
2 changes: 2 additions & 0 deletions RevenueCatUI/View+PresentPaywall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RevenueCat
import SwiftUI

@available(iOS 16.0, macOS 13.0, tvOS 16.0, *)
@available(macOS, unavailable, message: "RevenueCatUI does not support macOS yet")
extension View {

/// Presents a ``PaywallView`` if the given entitlement identifier is not active
Expand Down Expand Up @@ -57,6 +58,7 @@ extension View {
}

@available(iOS 16.0, macOS 13.0, tvOS 16.0, *)
@available(macOS, unavailable, message: "RevenueCatUI does not support macOS yet")
private struct PresentingPaywallModifier: ViewModifier {

var shouldDisplay: @Sendable (CustomerInfo) -> Bool
Expand Down
64 changes: 4 additions & 60 deletions Tests/UnitTests/TestHelpers/SnapshotTesting+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,18 @@ extension SwiftUI.View {
) {
UIView.setAnimationsEnabled(false)

// Add test view to the hierarchy to make sure async rendering takes place.
// The tested view is `controller.view` instead of `self` to keep it in memory
// while rendering happens
let controller = UIViewController()
let window = UIWindow()
window.rootViewController = controller

controller.view.addSubview(
self
let controller = UIHostingController(rootView: self
.frame(width: size.width, height: size.height)
.asUIView(container: controller, size: size)
)
controller.view.backgroundColor = .white

expect(
file: file, line: line,
controller.view
controller
).toEventually(
haveValidSnapshot(
as: .wait(
for: initialDelay.seconds,
on: .image(size: size)
),
as: .image(perceptualPrecision: 0.98, size: size),
named: "1", // Force each retry to end in `.1.png`
file: file,
line: line
Expand All @@ -92,8 +81,7 @@ extension SwiftUI.View {
#endif

private let timeout: DispatchTimeInterval = .seconds(5)
private let pollInterval: DispatchTimeInterval = .milliseconds(300)
private let initialDelay: DispatchTimeInterval = .milliseconds(100)
private let pollInterval: DispatchTimeInterval = .milliseconds(100)

// MARK: - Private

Expand Down Expand Up @@ -132,47 +120,3 @@ private let outputFormatting: JSONEncoder.OutputFormatting = {

return result
}()

// MARK: - SwiftUIContainerView

#if !os(watchOS) && !os(macOS)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
@available(watchOS, unavailable)
private final class SwiftUIContainerView<V: SwiftUI.View>: UIView {

private let controller: UIHostingController<V>

init(container: UIViewController, view: V, size: CGSize) {
self.controller = UIHostingController(rootView: view)
self.controller.view.backgroundColor = nil

super.init(frame: .init(origin: .zero, size: size))

container.addChild(self.controller)
self.addSubview(self.controller.view)
self.controller.didMove(toParent: container)
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()

self.controller.view.frame = self.bounds
}

}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)
private extension SwiftUI.View {

func asUIView(container: UIViewController, size: CGSize) -> SwiftUIContainerView<Self> {
return SwiftUIContainerView(container: container, view: self, size: size)
}

}

#endif
4 changes: 4 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ platform :ios do
scheme: 'RevenueCatUI',
destination: "platform=iOS Simulator,name=" + (ENV['SCAN_DEVICE'] || "iPhone 14,OS=16.4"),
sdk: 'iphonesimulator',
output_style: :basic,
result_bundle_path: 'fastlane/test_output/revenuecatui.xcresult',
report_formats: [:junit],
report_path: 'fastlane/test_output/revenuecatui/tests.xml',
test: true
)
end
Expand Down

0 comments on commit 96f053f

Please sign in to comment.