BarredEwe / Prefire

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

Provide means to delay snapshot #11

Closed markst closed 1 year ago

markst commented 1 year ago

In our views we have UIImageView which fetches from an image url.

For testing purposes these test assets are loaded from load bundle:

public enum TestResource {
    public static func imageURL(named: String, ext: String) -> URL? {
        Bundle.module.url(
            forResource: named,
            withExtension: ext,
            subdirectory: nil

But for snapshots to work there needs to be a minor delay for completion closures to be called in which to display the image.

i.e:

let failure = verifySnapshot(
    matching: try value(),
    as: .wait(
        for: wait,
        on: .image(
            drawHierarchyInKeyWindow: false,
            precision: defaultPerceptualPrecision,
            size: size
        )
    ),
markst commented 1 year ago

Happy to work on this, but not sure how we might conditionally use a delay. Perhaps PrefireProvider could expect a config to be returned which could have some additional attributes?

BarredEwe commented 1 year ago

Yes, I think we can make a configuration that will provide some custom settings for Snapshots. For example, add delay and accuracy there:

struct TestView_Previews: PrefireProvider {
    static var previews: some View {
        TestView()
            .snapshot(delay: 0.1, precision: 1)
    }
}
markst commented 1 year ago

@BarredEwe not sure view modifier will work given we can't capture the preference synchronously. unless we triggered a draw of the view prior to assertSnapshot?

BarredEwe commented 1 year ago

I made this improvement and posted a pull request 🥳