diff --git a/Sources/SnapshotTestCase/Snapshot.swift b/Sources/SnapshotTestCase/Snapshot.swift index 9b951c2..44c3263 100644 --- a/Sources/SnapshotTestCase/Snapshot.swift +++ b/Sources/SnapshotTestCase/Snapshot.swift @@ -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 } @@ -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) @@ -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() @@ -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() diff --git a/Sources/SnapshotTestCase/SnapshotTestCase.swift b/Sources/SnapshotTestCase/SnapshotTestCase.swift index 9ce332d..a8a28f7 100644 --- a/Sources/SnapshotTestCase/SnapshotTestCase.swift +++ b/Sources/SnapshotTestCase/SnapshotTestCase.swift @@ -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( @@ -21,6 +23,8 @@ public extension SnapshotTestCase { renderDelay: renderDelay, file: file, function: function, + preAppear: preAppear, + postAppear: postAppear, viewControllerBuilder: { UIHostingController(rootView: viewBuilder()) } ) } @@ -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)