-
Notifications
You must be signed in to change notification settings - Fork 43
/
schedulerTestMatchers.js
95 lines (84 loc) · 2.58 KB
/
schedulerTestMatchers.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// https://github.com/facebook/react/blob/master/scripts/jest/matchers/schedulerTestMatchers.js
'use strict'
function captureAssertion(fn) {
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
// assertion; if it throws, we capture the error and return it, so the stack
// trace presented to the user points to the original assertion in the
// test file.
try {
fn()
} catch (error) {
return {
pass: false,
message: () => error.message
}
}
return { pass: true }
}
function assertYieldsWereCleared(Scheduler) {
const actualYields = Scheduler.unstable_clearYields()
if (actualYields.length !== 0) {
throw new Error(
'Log of yielded values is not empty. ' +
'Call expect(Scheduler).toHaveYielded(...) first.'
)
}
}
function toFlushAndYield(Scheduler, expectedYields) {
assertYieldsWereCleared(Scheduler)
Scheduler.unstable_flushAllWithoutAsserting()
const actualYields = Scheduler.unstable_clearYields()
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields)
})
}
function toFlushAndYieldThrough(Scheduler, expectedYields) {
assertYieldsWereCleared(Scheduler)
Scheduler.unstable_flushNumberOfYields(expectedYields.length)
const actualYields = Scheduler.unstable_clearYields()
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields)
})
}
function toFlushUntilNextPaint(Scheduler, expectedYields) {
assertYieldsWereCleared(Scheduler)
Scheduler.unstable_flushUntilNextPaint()
const actualYields = Scheduler.unstable_clearYields()
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields)
})
}
function toFlushWithoutYielding(Scheduler) {
return toFlushAndYield(Scheduler, [])
}
function toFlushExpired(Scheduler, expectedYields) {
assertYieldsWereCleared(Scheduler)
Scheduler.unstable_flushExpired()
const actualYields = Scheduler.unstable_clearYields()
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields)
})
}
function toHaveYielded(Scheduler, expectedYields) {
return captureAssertion(() => {
const actualYields = Scheduler.unstable_clearYields()
expect(actualYields).toEqual(expectedYields)
})
}
function toFlushAndThrow(Scheduler, ...rest) {
assertYieldsWereCleared(Scheduler)
return captureAssertion(() => {
expect(() => {
Scheduler.unstable_flushAllWithoutAsserting()
}).toThrow(...rest)
})
}
module.exports = {
toFlushAndYield,
toFlushAndYieldThrough,
toFlushUntilNextPaint,
toFlushWithoutYielding,
toFlushExpired,
toHaveYielded,
toFlushAndThrow
}