uber / ios-snapshot-test-case

Snapshot view unit tests for iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FBSnapshotVerifyView doesn't seem to work well with SwiftUI

Czajnikowski opened this issue · comments

The issue happens in Xcode 11 beta 2 (11M337n) and beta 3 (11M362v). I know it's an early moment and maybe it's a bug in the SDK, but currently when I invoke this with recordMode = true I get a blank snapshot:

let viewController = UIHostingController(
    rootView: Text("yo")
)
viewController.view?.frame = CGRect(
    origin: CGPoint.zero,
    size: CGSize(width: 300, height: 500)
)
        
FBSnapshotVerifyView(viewController.view!)

This has nothing to do with the library, if you check viewController.view this is empty (white), that happens to me too

In this way, the image is well generated.

Option 1

    let vc = UIHostingController(rootView: ContentView())

    let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    window.rootViewController = vc
    window.makeKeyAndVisible()

    sut = vc.view

    FBSnapshotVerifyView(sut)

Option 2

    let vc = UIHostingController(rootView: ContentView())
    vc.view.frame = CGRect(x: 0, y: -1, width: 100, height: 100)
    vc.view.drawHierarchy(in: vc.view.bounds, afterScreenUpdates: true)
    sut = vc.view

Yes, works well! Thanks for your help @leohidalgo 🍻😊

I would like to discuss why this should be done now and not before. The both option run, why?, I don't know 😬

I checked briefly transcriptions from WWDC vids (searching for „window”), but didn’t find any clue 🤷🏼‍♂️

I think I have the answer,

https://developer.apple.com/documentation/swiftui/color
image

The view is only resolved to concrete when it is displayed, In our case in a UIWindow.