Replies: 3 comments
-
Hi @grennis, you currently have an uncontrolled dependency in your feature which is what is causing the problems: .receive(on: DispatchQueue.main) That is what is causing the thread hop. Ideally you wouldn't need that class ViewController: UIViewController, UITableViewDataSource {
let mainQueue: AnySchedulerOf<DispatchQueue>
…
} And then use that dependency instead of reaching out to publisher
.receive(on: mainQueue)
.sink { value in switchView.isOn = value }
.store(in: &cancellables) And then you can use an Another way to fix the problem in an ad hoc manner is to force the view to render and then wait a small amount of time to have the thread hop happen: let vc = ViewController(nibName: nil, bundle: nil)
_ = vc.view.snapshotView(afterScreenUpdates: true)
try await Task.sleep(for: .seconds(0.1))
assertSnapshot(
of: vc,
as: .image(size: .init(width: 200, height: 800))
) The So, for the time being I recommend either controlling your dependencies, or using the other technique described above. |
Beta Was this translation helpful? Give feedback.
-
Great, thank you |
Beta Was this translation helpful? Give feedback.
-
if you are using class ViewController: UIViewController, UITableViewDataSource {
@Dependency(\.mainQueue) var mainQueue
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let switchView = UISwitch()
publisher
.receive(on: mainQueue)
.sink { value in switchView.isOn = value }
.store(in: &cancellables)
cell.addSubview(switchView)
return cell
}
} then in the test you use the immediate scheduler like this: let vc = withDependencies {
$0.mainQueue = .immediate
} operation: {
ViewController(nibName: nil, bundle: nil)
}
assertSnapshot(
of: vc,
as: .image(size: .init(width: 200, height: 800))
) |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
I'm not able to take a snapshot of a UITableView that has cells that receive and update values using a Combine publisher. Using the
wait
strategy does not help.To Reproduce
I attached the sample project, but the relevant code is pasted below. The snapshot taken shows the switch OFF, but I want the snapshot to happen just a tick later and capture the snapshot after the UI had a chance to update (when the switch is ON). Using the
wait
strategy doesn't help, because the wait occurs before layout.Expected behavior
Is there some way to delay the snapshot even for just a tick to let the child views update after they are created by the table view?
Environment
SnapshotTester.zip
Beta Was this translation helpful? Give feedback.
All reactions