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

Wait before capturing a snapshot #28

Open sparafinski opened 11 months ago

sparafinski commented 11 months ago

Context 🕵️‍♀️

While attempting to capture a view that includes AsyncImage or revealing a component after a specified duration, it's been noticed that the resulting snapshot doesn't include images loaded via URL or views that appear with a slight delay. Even when using .snapshot(delay: 1.0), there's no noticeable improvement in this scenario.

What 🌱

To overcome this challenge, it would be helpful to incorporate an option to wait before taking the snapshot. This way, we can ensure that the snapshot accounts for the asynchronous loading of images and views that emerge after a delay.

Example of a view that isn't properly captured in the snapshot:

struct DummyView: View {
    @State var isButtonHidden = true
    var body: some View {
        VStack {
            Text("Some text")
            Text("Some text with delay")
                .opacity(isButtonHidden ? 0 : 1)

        }.onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                self.isButtonHidden = false
            }
        }
    }
}
BarredEwe commented 11 months ago

It seems that this is a problem of the swift-snapshot-tests library. I found a PR in it that should fix this.

I'll try to think later about how to get around this inside the Prefire.

sparafinski commented 11 months ago

Also, it seems that .preferredColorScheme(.dark) is not handled properly. I found a PR that states it should already work 🤔 using swift-snapshot-tests . Do you think it could be the same cause?

BarredEwe commented 11 months ago

It sounds like a bug in the Prefire. I set myself the task to fix it in the 2.0 release 😊