Skip to content

Commit

Permalink
Implements a wait timeInterval (#59)
Browse files Browse the repository at this point in the history
* Implements a wait timeInterval which gets passed through to make use of the wait strategy when e.g. recording snapshots of views after an animation ran through or after an async image loaded

* Introduces overload of assertSnapshotDevices for different snapshotting strategies
  • Loading branch information
LukasLiebl authored Jul 24, 2023
1 parent b3eb108 commit 04a6c81
Showing 1 changed file with 63 additions and 26 deletions.
89 changes: 63 additions & 26 deletions Sources/SnapshotTestingExtensions/SnapshotTestBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import XCTest

open class SnapshotTestBase: XCTestCase {
public var allowAnimations: Bool = false

override open func setUp() {
super.setUp()
UIView.setAnimationsEnabled(allowAnimations)
}

open var defaultDevices: [(name: String, device: ViewImageConfig)] {
[
("SE", .iPhoneSe),
Expand All @@ -22,7 +22,7 @@ open class SnapshotTestBase: XCTestCase {
("iPadPro", .iPadPro12_9(.portrait))
]
}

open func assertSnapshotDevices<V: View>(
_ view: V,
devices: [(name: String, device: ViewImageConfig)]? = nil,
Expand All @@ -33,30 +33,67 @@ open class SnapshotTestBase: XCTestCase {
line: UInt = #line
) {
(devices ?? defaultDevices).forEach { config in
style.forEach { uiStyle in
let vc = UIHostingController(rootView: view)
vc.overrideUserInterfaceStyle = uiStyle

let suffix: String
switch uiStyle {
case .unspecified:
suffix = ""
case .light:
suffix = "-light"
case .dark:
suffix = "-dark"
@unknown default:
fatalError("Unhandled UIUserInterfaceStyle \(uiStyle)")
}

assertSnapshot(
matching: vc,
as: .image(on: config.device, precision: imageDiffPrecision),
file: file,
testName: "\(testName)-\(config.name)\(suffix)",
line: line
)
assertSnapshotDevices(
view,
as: .image(on: config.device, precision: imageDiffPrecision),
style: style,
config: config
)
}
}

open func assertSnapshotDevices<V: View>(
_ view: V,
devices: [(name: String, device: ViewImageConfig)]? = nil,
style: [UIUserInterfaceStyle] = [.unspecified],
imageDiffPrecision: Float = 1.0,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line,
wait: TimeInterval
) {
(devices ?? defaultDevices).forEach { config in
assertSnapshotDevices(
view,
as: .wait(for: wait, on: .image(on: config.device, precision: imageDiffPrecision)),
style: style,
config: config
)
}
}

private func assertSnapshotDevices<V: View>(
_ view: V,
as snapshotting: Snapshotting<UIViewController, UIImage>,
style: [UIUserInterfaceStyle] = [.unspecified],
config: (name: String, device: ViewImageConfig),
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
style.forEach { uiStyle in
let vc = UIHostingController(rootView: view)
vc.overrideUserInterfaceStyle = uiStyle

let suffix: String
switch uiStyle {
case .unspecified:
suffix = ""
case .light:
suffix = "-light"
case .dark:
suffix = "-dark"
@unknown default:
fatalError("Unhandled UIUserInterfaceStyle \(uiStyle)")
}

assertSnapshot(
matching: vc,
as: snapshotting,
file: file,
testName: "\(testName)-\(config.name)\(suffix)",
line: line
)
}
}
}
Expand Down

0 comments on commit 04a6c81

Please sign in to comment.