BarredEwe / Prefire

🔥 A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
290 stars 18 forks source link

#Preview only supports SwiftUI and not other view types #59

Closed aduuub closed 2 months ago

aduuub commented 5 months ago

Context 🕵️‍♀️

As per Apple's documentation - https://developer.apple.com/documentation/xcode/previewing-your-apps-interface-in-xcode #Preview supports other forms of views such as UIViewController, NSView, UIView. Right now, unless you exclude those the generated tests from the plugin won't compile.

What 🌱

It would be amazing if we can add support for more view types other than just SwiftUI views.

Proposal 🎉

In the PreviewLoader, it assumes that it's a SwiftUI view and always wraps it in AnyView

I wonder if we could add another function assertSnapshots to the stencil that takes in the other view types like:

private func assertSnapshots(matching view: UIView,
                                 name: String?, isScreen: Bool,
                                 device: ViewImageConfig,
                                 testName: String = #function,
                                 traits: UITraitCollection = .init()) -> String? {
    // ... 
}

private func assertSnapshots(matching view: UIViewController,
                                 name: String?, isScreen: Bool,
                                 device: ViewImageConfig,
                                 testName: String = #function,
                                 traits: UITraitCollection = .init()) -> String? {
    // ... 
}

And then instead of always casting to AnyView, use a more generic any View parameter for the assertSnapshots function and then allow it to dynamically call the appropriate function based on the classes implementation type?

Happy to help further with an implementation but I think this would be a great addition!

BarredEwe commented 2 months ago

Hi, did a PR with this tweak https://github.com/BarredEwe/Prefire/pull/70