Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SnapshotTestCase/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Snapshot {
let filePath: URL
let name: String
let renderDelay: TimeInterval
let preAppear: @MainActor () async throws -> Void
let postAppear: @MainActor () async throws -> Void
let viewControllerBuilder: @MainActor () -> UIViewController
}

Expand Down Expand Up @@ -231,7 +233,7 @@ private extension Snapshot.TestCase {
@MainActor
private func takeSnapshot(with config: SnapshotConfig.Config) async throws -> UIImage {
let size = config.size + CGSize(width: 0, height: Snapshot.renderOffsetY)
let (viewController, view) = try create(with: config, in: size)
let (viewController, view) = try await create(with: config, in: size)
var snapshot = try await SnapshotWindow.shared.new()
.frame(CGRect(origin: .zero, size: size))
.rootViewController(viewController)
Expand All @@ -249,6 +251,7 @@ private extension Snapshot.TestCase {
}

try await Task.sleep(for: .seconds(renderDelay))
try await postAppear()
view.layer.render(in: context)

let image = UIGraphicsGetImageFromCurrentImageContext()
Expand Down Expand Up @@ -280,7 +283,8 @@ private extension Snapshot.TestCase {
private func create(
with config: SnapshotConfig.Config,
in size: CGSize
) throws -> (UIViewController, UIView) {
) async throws -> (UIViewController, UIView) {
try await preAppear()
let viewController = viewControllerBuilder().interfaceStyle(config.interfaceStyle)
viewController.beginAppearanceTransition(true, animated: false)
viewController.endAppearanceTransition()
Expand Down
8 changes: 8 additions & 0 deletions Sources/SnapshotTestCase/SnapshotTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public extension SnapshotTestCase {
renderDelay: TimeInterval = .snapshotRenderDelay,
file: String = #file,
function: String = #function,
preAppear: @MainActor @escaping () async throws -> Void = { },
postAppear: @MainActor @escaping () async throws -> Void = { },
viewBuilder: @escaping @MainActor () -> some View
) async throws {
try await verifySnapshot(
Expand All @@ -21,6 +23,8 @@ public extension SnapshotTestCase {
renderDelay: renderDelay,
file: file,
function: function,
preAppear: preAppear,
postAppear: postAppear,
viewControllerBuilder: { UIHostingController(rootView: viewBuilder()) }
)
}
Expand All @@ -31,12 +35,16 @@ public extension SnapshotTestCase {
renderDelay: TimeInterval = .snapshotRenderDelay,
file: String = #file,
function: String = #function,
preAppear: @MainActor @escaping () async throws -> Void = { },
postAppear: @MainActor @escaping () async throws -> Void = { },
viewControllerBuilder: @escaping @MainActor () -> some UIViewController
) async throws {
let testCase = Snapshot.TestCase(
filePath: getFilePath(file: file),
name: name ?? getTestCaseName(file: file, function: function) ?? "Test",
renderDelay: renderDelay,
preAppear: preAppear,
postAppear: postAppear,
viewControllerBuilder: viewControllerBuilder
)
try await snapshot.verify(testCase: testCase, with: config)
Expand Down