swiftxcodeuikittexturesxcode-ui-testing

Snapshot testing for texture nodes - Swift


I am writing UI tests for a view that was implemented in textures. We are using swift-snapshot-testing module by pointfreeco. This is my first time writing tests as well.

The issue is that when I record the snapshot only the base node is being captured, all the nodes that are inside of the base node are not being displayed.

I tried to follow the flow to my best and I think this is because all the layout is done in layoutSpecThatFits functions and it might be being called at the last moment where snapshot is being recorded before any of this layout logic happens.

I did try adding a delay but it did not work.

I would really grateful if anyone could help me.


Solution

  • Before capturing a snapshot of a node's view, make sure that its layout methods has been called.

    You can do so by calling setNeedsDisplay() method on the layer property of your node. And also call recursivelyEnsureDisplaySynchronously(bool) method and set it to true

    yourNode.layer.setNeedsDisplay()
    yourNode.recursivelyEnsureDisplaySynchronously(true)
    

    Also if you are making any network operations such as downloading an image or waiting for any backed response, then make sure to capture snapshot once all this process is complete, by adding a timer or some other way.

    For timer I use the following way:

    _ = XCTWaiter.wait(for: [expectation(description: "Waiting for network operation to be completed")], timeout: 1)