-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathWait.swift
27 lines (26 loc) · 1.1 KB
/
Wait.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Foundation
import XCTest
extension Snapshotting {
/// Transforms an existing snapshot strategy into one that waits for some amount of time before taking the snapshot. This can be useful for waiting for animations to complete or for UIKit events to finish (i.e. waiting for a UINavigationController to push a child onto the stack).
/// - Parameters:
/// - duration: The amount of time to wait before taking the snapshot.
/// - strategy: The snapshot to invoke after the specified amount of time has passed.
public static func wait(
for duration: TimeInterval,
on strategy: Snapshotting
) -> Snapshotting {
return Snapshotting(
pathExtension: strategy.pathExtension,
diffing: strategy.diffing,
asyncSnapshot: { value in
Async { callback in
let expectation = XCTestExpectation(description: "Wait")
DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
expectation.fulfill()
}
_ = XCTWaiter.wait(for: [expectation], timeout: duration + 1)
strategy.snapshot(value).run(callback)
}
})
}
}