forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSchedulerNoDOM-test.internal.js
More file actions
145 lines (125 loc) · 3.42 KB
/
SchedulerNoDOM-test.internal.js
File metadata and controls
145 lines (125 loc) · 3.42 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let scheduleCallback;
let runWithPriority;
let ImmediatePriority;
let UserBlockingPriority;
describe('SchedulerNoDOM', () => {
// If Scheduler runs in a non-DOM environment, it falls back to a naive
// implementation using setTimeout. This only meant to be used for testing
// purposes, like with jest's fake timer API.
beforeEach(() => {
jest.useFakeTimers();
jest.resetModules();
// Delete addEventListener to force us into the fallback mode.
window.addEventListener = undefined;
const Scheduler = require('scheduler');
scheduleCallback = Scheduler.unstable_scheduleCallback;
runWithPriority = Scheduler.unstable_runWithPriority;
ImmediatePriority = Scheduler.unstable_ImmediatePriority;
UserBlockingPriority = Scheduler.unstable_UserBlockingPriority;
});
it('runAllTimers flushes all scheduled callbacks', () => {
let log = [];
scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
scheduleCallback(() => {
log.push('C');
});
expect(log).toEqual([]);
jest.runAllTimers();
expect(log).toEqual(['A', 'B', 'C']);
});
it('executes callbacks in order of priority', () => {
let log = [];
scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
runWithPriority(UserBlockingPriority, () => {
scheduleCallback(() => {
log.push('C');
});
scheduleCallback(() => {
log.push('D');
});
});
expect(log).toEqual([]);
jest.runAllTimers();
expect(log).toEqual(['C', 'D', 'A', 'B']);
});
it('advanceTimersByTime expires callbacks incrementally', () => {
let log = [];
scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
runWithPriority(UserBlockingPriority, () => {
scheduleCallback(() => {
log.push('C');
});
scheduleCallback(() => {
log.push('D');
});
});
expect(log).toEqual([]);
jest.advanceTimersByTime(249);
expect(log).toEqual([]);
jest.advanceTimersByTime(1);
expect(log).toEqual(['C', 'D']);
log = [];
jest.runAllTimers();
expect(log).toEqual(['A', 'B']);
});
it('calls immediate callbacks immediately', () => {
let log = [];
runWithPriority(ImmediatePriority, () => {
scheduleCallback(() => {
log.push('A');
scheduleCallback(() => {
log.push('B');
});
});
});
expect(log).toEqual(['A', 'B']);
});
it('handles errors', () => {
let log = [];
expect(() => {
runWithPriority(ImmediatePriority, () => {
scheduleCallback(() => {
log.push('A');
throw new Error('Oops A');
});
scheduleCallback(() => {
log.push('B');
});
scheduleCallback(() => {
log.push('C');
throw new Error('Oops C');
});
});
}).toThrow('Oops A');
expect(log).toEqual(['A']);
log = [];
// B and C flush in a subsequent event. That way, the second error is not
// swallowed.
expect(() => jest.runAllTimers()).toThrow('Oops C');
expect(log).toEqual(['B', 'C']);
});
});