Skip to content

Relax the protocol requirement for Snapshotting extension #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kyle-Ye
Copy link

@Kyle-Ye Kyle-Ye commented Jun 7, 2025

When wrapping a custom assert function for SwiftUI.View, we may want to write the following code.

#if canImport(AppKit)
typealias PlatformHostingController = NSHostingController
#else
typealias PlatformHostingController = UIHostingController
#endif

func viewAssertSnapshot<V: View, Format>(
    of value: @autoclosure () throws -> V,
    as snapshotting: Snapshotting<PlatformHostingController<V>, Format> = .image,
    ...
) {
    assertSnapshot(
        of: PlatformHostingController(rootView: try value()),
        as: snapshotting,
        named: name,
        record: recording,
        timeout: timeout,
        fileID: fileID,
        file: filePath,
        testName: testName,
        line: line,
        column: column
    )
    ...
}

It does not compile now since image extension has a requirement of Value == UIViewController.

And the workaround is change Snapshotting<PlatformHostingController<V>, Format> to Snapshotting<UIViewController, Format>.

I think it is common we define some UIViewController's subclass and use the new subclass as the generic. So I propose we relax the requirement for the common UI extension of Snapshotting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant