41
ReactScheduler = require('react-scheduler');
42
});
43
44
- describe('rIC', () => {
44
+ describe('scheduleWork', () => {
45
it('calls the callback within the frame when not blocked', () => {
46
- const {rIC} = ReactScheduler;
46
+ const {scheduleWork} = ReactScheduler;
47
const cb = jest.fn();
48
- rIC(cb);
48
+ scheduleWork(cb);
49
jest.runAllTimers();
50
expect(cb.mock.calls.length).toBe(1);
51
// should not have timed out and should include a timeRemaining method
55
56
describe('with multiple callbacks', () => {
57
it('accepts multiple callbacks and calls within frame when not blocked', () => {
58
- const {rIC} = ReactScheduler;
58
+ const {scheduleWork} = ReactScheduler;
59
const callbackLog = [];
60
const callbackA = jest.fn(() => callbackLog.push('A'));
61
const callbackB = jest.fn(() => callbackLog.push('B'));
62
- rIC(callbackA);
62
+ scheduleWork(callbackA);
63
// initially waits to call the callback
64
expect(callbackLog).toEqual([]);
65
// waits while second callback is passed
66
- rIC(callbackB);
66
+ scheduleWork(callbackB);
67
expect(callbackLog).toEqual([]);
68
// after a delay, calls as many callbacks as it has time for
69
jest.runAllTimers();
84
'schedules callbacks in correct order and' +
85
'keeps calling them if there is time',
86
() => {
87
- const {rIC} = ReactScheduler;
87
+ const {scheduleWork} = ReactScheduler;
88
const callbackLog = [];
89
const callbackA = jest.fn(() => {
90
callbackLog.push('A');
91
- rIC(callbackC);
91
+ scheduleWork(callbackC);
92
});
93
const callbackB = jest.fn(() => {
94
callbackLog.push('B');
97
callbackLog.push('C');
98
});
99
100
- rIC(callbackA);
100
+ scheduleWork(callbackA);
101
// initially waits to call the callback
102
expect(callbackLog).toEqual([]);
103
// continues waiting while B is scheduled
104
- rIC(callbackB);
104
+ scheduleWork(callbackB);
105
expect(callbackLog).toEqual([]);
106
// after a delay, calls the scheduled callbacks,
107
// and also calls new callbacks scheduled by current callbacks
110
},
111
);
112
113
- it('schedules callbacks in correct order when callbacks have many nested rIC calls', () => {
114
- const {rIC} = ReactScheduler;
113
+ it('schedules callbacks in correct order when callbacks have many nested scheduleWork calls', () => {
114
+ const {scheduleWork} = ReactScheduler;
115
const callbackLog = [];
116
const callbackA = jest.fn(() => {
117
callbackLog.push('A');
118
- rIC(callbackC);
119
- rIC(callbackD);
118
+ scheduleWork(callbackC);
119
+ scheduleWork(callbackD);
120
});
121
const callbackB = jest.fn(() => {
122
callbackLog.push('B');
123
- rIC(callbackE);
124
- rIC(callbackF);
123
+ scheduleWork(callbackE);
124
+ scheduleWork(callbackF);
125
});
126
const callbackC = jest.fn(() => {
127
callbackLog.push('C');
136
callbackLog.push('F');
137
});
138
139
- rIC(callbackA);
140
- rIC(callbackB);
139
+ scheduleWork(callbackA);
140
+ scheduleWork(callbackB);
141
// initially waits to call the callback
142
expect(callbackLog).toEqual([]);
143
// while flushing callbacks, calls as many as it has time for
145
expect(callbackLog).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
146
});
147
148
- it('schedules callbacks in correct order when they use rIC to schedule themselves', () => {
149
- const {rIC} = ReactScheduler;
148
+ it('schedules callbacks in correct order when they use scheduleWork to schedule themselves', () => {
149
+ const {scheduleWork} = ReactScheduler;
150
const callbackLog = [];
151
let callbackAIterations = 0;
152
const callbackA = jest.fn(() => {
153
if (callbackAIterations < 1) {
154
- rIC(callbackA);
154
+ scheduleWork(callbackA);
155
}
156
callbackLog.push('A' + callbackAIterations);
157
callbackAIterations++;
158
});
159
const callbackB = jest.fn(() => callbackLog.push('B'));
160
161
- rIC(callbackA);
161
+ scheduleWork(callbackA);
162
// initially waits to call the callback
163
expect(callbackLog).toEqual([]);
164
- rIC(callbackB);
164
+ scheduleWork(callbackB);
165
expect(callbackLog).toEqual([]);
166
// after a delay, calls the latest callback passed
167
jest.runAllTimers();
170
});
171
});
172
173
- describe('cIC', () => {
173
+ describe('cancelScheduledWork', () => {
174
it('cancels the scheduled callback', () => {
175
- const {rIC, cIC} = ReactScheduler;
175
+ const {scheduleWork, cancelScheduledWork} = ReactScheduler;
176
const cb = jest.fn();
177
- const callbackId = rIC(cb);
177
+ const callbackId = scheduleWork(cb);
178
expect(cb.mock.calls.length).toBe(0);
179
- cIC(callbackId);
179
+ cancelScheduledWork(callbackId);
180
jest.runAllTimers();
181
expect(cb.mock.calls.length).toBe(0);
182
});
183
184
describe('with multiple callbacks', () => {
185
it('when one callback cancels the next one', () => {
186
- const {rIC, cIC} = ReactScheduler;
186
+ const {scheduleWork, cancelScheduledWork} = ReactScheduler;
187
const callbackLog = [];
188
let callbackBId;
189
const callbackA = jest.fn(() => {
190
callbackLog.push('A');
191
- cIC(callbackBId);
191
+ cancelScheduledWork(callbackBId);
192
});
193
const callbackB = jest.fn(() => callbackLog.push('B'));
194
- rIC(callbackA);
195
- callbackBId = rIC(callbackB);
194
+ scheduleWork(callbackA);
195
+ callbackBId = scheduleWork(callbackB);
196
// Initially doesn't call anything
197
expect(callbackLog).toEqual([]);
198
jest.runAllTimers();