Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9dba218

Browse files
authored
[Mock Scheduler] Mimic browser's advanceTime (facebook#17967)
The mock Scheduler that we use in our tests has its own fake timer implementation. The `unstable_advanceTime` method advances the timeline. Currently, a call to `unstable_advanceTime` will also flush any pending expired work. But that's not how it works in the browser: when a timer fires, the corresponding task is added to the Scheduler queue. However, we will still wait until the next message event before flushing it. This commit changes `unstable_advanceTime` to more closely resemble the browser behavior, by removing the automatic flushing of expired work. ```js // Before this commit Scheduler.unstable_advanceTime(ms); // Equivalent behavior after this commit Scheduler.unstable_advanceTime(ms); Scheduler.unstable_flushExpired(); ``` The general principle is to prefer separate APIs for scheduling tasks and flushing them. This change does not affect any public APIs. `unstable_advanceTime` is only used by our own test suite. It is not used by `act`. However, we may need to update tests in www, like Relay's.
1 parent cddde45 commit 9dba218

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

packages/react-dom/src/events/__tests__/ChangeEventPlugin-test.internal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ describe('ChangeEventPlugin', () => {
779779

780780
// 3s should be enough to expire the updates
781781
Scheduler.unstable_advanceTime(3000);
782+
expect(Scheduler).toFlushExpired([]);
782783
expect(container.textContent).toEqual('hovered');
783784
});
784785
},

packages/react-reconciler/src/__tests__/ReactIncrementalUpdates-test.internal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ describe('ReactIncrementalUpdates', () => {
540540

541541
// All the updates should render and commit in a single batch.
542542
Scheduler.unstable_advanceTime(10000);
543-
expect(Scheduler).toHaveYielded(['Render: goodbye']);
543+
expect(Scheduler).toFlushExpired(['Render: goodbye']);
544544
// Passive effect
545545
expect(Scheduler).toFlushAndYield(['Commit: goodbye']);
546546
});
@@ -645,7 +645,7 @@ describe('ReactIncrementalUpdates', () => {
645645

646646
// All the updates should render and commit in a single batch.
647647
Scheduler.unstable_advanceTime(10000);
648-
expect(Scheduler).toHaveYielded([
648+
expect(Scheduler).toFlushExpired([
649649
'Render: goodbye',
650650
'Commit: goodbye',
651651
'Render: goodbye',

packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
848848
</Suspense>,
849849
);
850850
Scheduler.unstable_advanceTime(10000);
851-
expect(Scheduler).toHaveYielded([
851+
expect(Scheduler).toFlushExpired([
852852
'Suspend! [A]',
853853
'Suspend! [B]',
854854
'Loading...',
@@ -987,7 +987,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
987987
Scheduler.unstable_advanceTime(10000);
988988
jest.advanceTimersByTime(10000);
989989

990-
expect(Scheduler).toHaveYielded([
990+
expect(Scheduler).toFlushExpired([
991991
'Suspend! [goodbye]',
992992
'Loading...',
993993
'Commit: goodbye',

packages/scheduler/src/__tests__/Scheduler-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ describe('Scheduler', () => {
117117

118118
// Advance by just a bit more to expire the user blocking callbacks
119119
Scheduler.unstable_advanceTime(1);
120-
expect(Scheduler).toHaveYielded([
120+
expect(Scheduler).toFlushExpired([
121121
'B (did timeout: true)',
122122
'C (did timeout: true)',
123123
]);
124124

125125
// Expire A
126126
Scheduler.unstable_advanceTime(4600);
127-
expect(Scheduler).toHaveYielded(['A (did timeout: true)']);
127+
expect(Scheduler).toFlushExpired(['A (did timeout: true)']);
128128

129129
// Flush the rest without expiring
130130
expect(Scheduler).toFlushAndYield([
@@ -140,7 +140,7 @@ describe('Scheduler', () => {
140140
expect(Scheduler).toHaveYielded([]);
141141

142142
Scheduler.unstable_advanceTime(1);
143-
expect(Scheduler).toHaveYielded(['A']);
143+
expect(Scheduler).toFlushExpired(['A']);
144144
});
145145

146146
it('continues working on same task after yielding', () => {
@@ -217,7 +217,7 @@ describe('Scheduler', () => {
217217

218218
// Advance time by just a bit more. This should expire all the remaining work.
219219
Scheduler.unstable_advanceTime(1);
220-
expect(Scheduler).toHaveYielded(['C', 'D']);
220+
expect(Scheduler).toFlushExpired(['C', 'D']);
221221
});
222222

223223
it('continuations are interrupted by higher priority work', () => {
@@ -705,7 +705,7 @@ describe('Scheduler', () => {
705705

706706
// Now it expires
707707
Scheduler.unstable_advanceTime(1);
708-
expect(Scheduler).toHaveYielded(['A']);
708+
expect(Scheduler).toFlushExpired(['A']);
709709
});
710710

711711
it('cancels a delayed task', () => {

packages/scheduler/src/forks/SchedulerHostConfig.mock.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,10 @@ export function unstable_yieldValue(value: mixed): void {
201201

202202
export function unstable_advanceTime(ms: number) {
203203
currentTime += ms;
204-
if (!isFlushing) {
205-
if (scheduledTimeout !== null && timeoutTime <= currentTime) {
206-
scheduledTimeout(currentTime);
207-
timeoutTime = -1;
208-
scheduledTimeout = null;
209-
}
210-
unstable_flushExpired();
204+
if (scheduledTimeout !== null && timeoutTime <= currentTime) {
205+
scheduledTimeout(currentTime);
206+
timeoutTime = -1;
207+
scheduledTimeout = null;
211208
}
212209
}
213210

0 commit comments

Comments
 (0)