TestScheduler to have advance(to:) #55
Replies: 4 comments 1 reply
-
Someone told me that |
Beta Was this translation helpful? Give feedback.
-
@ytyubox We're definitely down to add an |
Beta Was this translation helpful? Give feedback.
-
Modified from the TCA's
final class EffectDebounceTests: XCTestCase {
var cancellables: Set<AnyCancellable> = []
func testDebounce() {
let mainQueue = DispatchQueue.test
var values: [Int] = []
func runDebouncedEffect(value: Int) {
struct CancelToken: Hashable {}
Just(value)
.eraseToEffect()
.debounce(id: CancelToken(), for: 1, scheduler: mainQueue)
.sink { values.append($0) }
.store(in: &self.cancellables)
}
runDebouncedEffect(value: 1)
/**
// comment in comment, I usually place keyframes details somewhere above the advance(to:) operation.
// This can be a block comment or anything.
Six keyframes
0.0s run effect of 1
0.5s no value happen and run effect of 2
1.0s emit nothing, and run effect of 3
1.5s emit nothing
2.0s emit the final effect value
any point of the future, emit nothing
*/
// Nothing emits right away.
XCTAssertNoDifference(values, [])
// Waiting half the time also emits nothing
mainQueue.advance(to: 0.5)
XCTAssertNoDifference(values, [])
// Run another debounced effect.
runDebouncedEffect(value: 2)
// Waiting half the time emits nothing because the first debounced effect has been canceled.
mainQueue.advance(to: 1.0)
XCTAssertNoDifference(values, [])
// Run another debounced effect.
runDebouncedEffect(value: 3)
// Waiting half the time emits nothing because the second debounced effect has been canceled.
mainQueue.advance(to: 1.5)
XCTAssertNoDifference(values, [])
// Waiting the rest of the time emits the final effect value.
mainQueue.advance(to: 2.0)
XCTAssertNoDifference(values, [3])
// Running out the scheduler
mainQueue.run()
XCTAssertNoDifference(values, [3])
}
} Moreover, if I really care the timing of it, I use for timestamp in stride(from: 1.0, to:2.0, by 0.01) {
XCTAssertTrue(values == expect, "found modification at \(timestamp)"
} |
Beta Was this translation helpful? Give feedback.
-
Hi @stephencelis, hope that wasn't too long but here it is, #55 |
Beta Was this translation helpful? Give feedback.
-
I have a great time using TestScheduler, but when I tried to write a specification about time, I always tried to use
keyframe
to illustrate. Especially come to a complex scenario that combines throttle and debounce and delay.Therefore, I came up with this helper.
Beta Was this translation helpful? Give feedback.
All reactions