Skip to content

Commit

Permalink
Support SwiftUI snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
younata committed Sep 6, 2024
1 parent 6aba4ac commit 24ee4b0
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Nimble_Snapshots/HaveValidSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
import Nimble
import QuartzCore
import UIKit
import SwiftUI

@objc public protocol Snapshotable {
var snapshotObject: UIView? { get }
Expand Down Expand Up @@ -129,6 +130,14 @@ public func recordAllSnapshots() {
switchChecksWithRecords = true
}

public func setNimbleSwiftUISize(_ size: CGSize?) {
swiftUIDefaultSize = size
}

public func getNimbleSwiftUISize() -> CGSize? {
swiftUIDefaultSize
}

func getDefaultReferenceDirectory(_ sourceFileName: FileString) -> String {
if let globalReference = FBSnapshotTest.sharedInstance.referenceImagesDirectory {
return globalReference
Expand Down Expand Up @@ -275,6 +284,23 @@ private func currentTestName() -> String? {

internal var switchChecksWithRecords = false

internal var swiftUIDefaultSize: CGSize? = nil

extension SwiftUI.View {
fileprivate func snapshotable(size: CGSize? = nil) -> Snapshotable {
let window = UIWindow()
let controller = UIHostingController(rootView: self)

window.rootViewController = UIHostingController(rootView: self)
window.makeKeyAndVisible()
if let size = size ?? swiftUIDefaultSize {
window.frame = CGRect(origin: .zero, size: size)
}
window.sizeToFit()
return window
}
}

public func haveValidSnapshot<T: Snapshotable>(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
Expand All @@ -301,6 +327,28 @@ public func haveValidSnapshot<T: Snapshotable>(named name: String? = nil,
}
}

public func haveValidSnapshot<T: SwiftUI.View>(named name: String? = nil,
size: CGSize? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat? = nil,
shouldIgnoreScale: Bool = false) -> Nimble.Matcher<T> {

return Matcher { expression in
try haveValidSnapshot(
named: name,
identifier: identifier,
usesDrawRect: usesDrawRect,
pixelTolerance: pixelTolerance,
tolerance: tolerance,
shouldIgnoreScale: shouldIgnoreScale
).satisfies(Expression(expression: {
try expression.evaluate()?.snapshotable(size: size)
}, location: expression.location))
}
}

public func haveValidDeviceAgnosticSnapshot<T: Snapshotable>(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
Expand Down Expand Up @@ -329,6 +377,28 @@ public func haveValidDeviceAgnosticSnapshot<T: Snapshotable>(named name: String?
}
}

public func haveValidDeviceAgnosticSnapshot<T: SwiftUI.View>(named name: String? = nil,
size: CGSize? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat? = nil,
shouldIgnoreScale: Bool = false) -> Nimble.Matcher<T> {

return Matcher { expression in
try haveValidDeviceAgnosticSnapshot(
named: name,
identifier: identifier,
usesDrawRect: usesDrawRect,
pixelTolerance: pixelTolerance,
tolerance: tolerance,
shouldIgnoreScale: shouldIgnoreScale
).satisfies(Expression(expression: {
try expression.evaluate()?.snapshotable(size: size)
}, location: expression.location))
}
}

public func recordSnapshot<T: Snapshotable>(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
Expand All @@ -341,6 +411,23 @@ public func recordSnapshot<T: Snapshotable>(named name: String? = nil,
}
}

public func recordSnapshot<T: SwiftUI.View>(named name: String? = nil,
size: CGSize? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
shouldIgnoreScale: Bool = false) -> Nimble.Matcher<T> {
return Matcher { expression in
try recordSnapshot(
named: name,
identifier: identifier,
usesDrawRect: usesDrawRect,
shouldIgnoreScale: shouldIgnoreScale
).satisfies(Expression(expression: {
try expression.evaluate()?.snapshotable(size: size)
}, location: expression.location))
}
}

public func recordDeviceAgnosticSnapshot<T: Snapshotable>(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
Expand All @@ -352,3 +439,20 @@ public func recordDeviceAgnosticSnapshot<T: Snapshotable>(named name: String? =
shouldIgnoreScale: shouldIgnoreScale)
}
}

public func recordDeviceAgnosticSnapshot<T: SwiftUI.View>(named name: String? = nil,
size: CGSize? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
shouldIgnoreScale: Bool = false) -> Nimble.Matcher<T> {
return Matcher { expression in
try recordDeviceAgnosticSnapshot(
named: name,
identifier: identifier,
usesDrawRect: usesDrawRect,
shouldIgnoreScale: shouldIgnoreScale
).satisfies(Expression(expression: {
try expression.evaluate()?.snapshotable(size: size)
}, location: expression.location))
}
}

0 comments on commit 24ee4b0

Please sign in to comment.