Skip to content

Commit

Permalink
Add SwiftUI support for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
lunij committed Jun 18, 2024
1 parent 1ec2011 commit 7350d3c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,53 @@
}
}
#endif

#if os(macOS)
@available(macOS 10.15, *)
extension Snapshotting where Value: SwiftUI.View, Format == NSImage {

/// A snapshot strategy for comparing SwiftUI Views based on pixel equality.
public static var image: Snapshotting {
.image()
}

/// A snapshot strategy for comparing SwiftUI Views based on pixel equality.
///
/// - Parameters:
/// - precision: The percentage of pixels that must match.
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match.
/// 98-99% mimics [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the human eye.
/// - layout: A view layout override.
public static func image(
precision: Float = 0.99,
perceptualPrecision: Float = 0.99,
layout: SwiftUISnapshotLayout = .sizeThatFits
) -> Snapshotting {
SimplySnapshotting
.image(precision: precision, perceptualPrecision: perceptualPrecision)
.asyncPullback { view in
let controller = NSHostingController(rootView: view)
let initialFrame = controller.view.frame

let size = switch layout {

Check failure on line 121 in Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

View workflow job for this annotation

GitHub Actions / iOS 16.4 (Xcode 14.3.1)

expected initial value after '='

Check failure on line 121 in Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

View workflow job for this annotation

GitHub Actions / iOS 16.4 (Xcode 14.3.1)

consecutive statements on a line must be separated by ';'

Check failure on line 121 in Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

View workflow job for this annotation

GitHub Actions / macOS 13 (Xcode 14.3.1)

expected initial value after '='

Check failure on line 121 in Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

View workflow job for this annotation

GitHub Actions / macOS 13 (Xcode 14.3.1)

consecutive statements on a line must be separated by ';'
case let .fixed(width, height):
CGSize(width: width, height: height)
case .sizeThatFits:
controller.sizeThatFits(in: .zero)
}

let view = controller.view
view.frame.size = size

return Async { callback in
addImagesForRenderedViews(view).sequence().run { views in
callback(view.convertToImage())
views.forEach { $0.removeFromSuperview() }
view.frame = initialFrame
}
}
}
}
}
#endif
#endif
20 changes: 20 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,26 @@ final class SnapshotTestingTests: XCTestCase {
}
#endif

#if os(macOS)
@available(macOS 10.15, *)
func testSwiftUIView() {
struct SwiftUIView: View {
var body: some View {
ZStack {
Color.green
Color.yellow.padding()
Color.red.padding().padding()
}
}
}

let view = SwiftUIView()

assertSnapshot(of: view, as: .image(layout: .fixed(width: 100, height: 100)), named: "fixed")
assertSnapshot(of: view, as: .image(layout: .sizeThatFits), named: "size-that-fits")
}
#endif

#if os(iOS)
@available(iOS 13.0, *)
func testSwiftUIView_iOS() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7350d3c

Please sign in to comment.