Skip to content

Commit d412eec

Browse files
author
Sunil Pai
authored
[act] flush work correctly without a mocked scheduler (#16223)
Not returning the value of flushPassiveEffects() in flushWork() meant that with async act, we wouldn't flush all work with cascading effects. This PR fixes that oversight, and adds some tests to catch this in the future.
1 parent b43785e commit d412eec

5 files changed

Lines changed: 197 additions & 3 deletions

File tree

packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ function runActTests(label, render, unmount) {
130130
container = document.createElement('div');
131131
document.body.appendChild(container);
132132
});
133+
133134
afterEach(() => {
134135
unmount(container);
135136
document.body.removeChild(container);
136137
});
138+
137139
describe('sync', () => {
138140
it('can use act to flush effects', () => {
139141
function App() {
@@ -240,13 +242,16 @@ function runActTests(label, render, unmount) {
240242
'An update to App inside a test was not wrapped in act(...).',
241243
]);
242244
});
245+
243246
describe('fake timers', () => {
244247
beforeEach(() => {
245248
jest.useFakeTimers();
246249
});
250+
247251
afterEach(() => {
248252
jest.useRealTimers();
249253
});
254+
250255
it('lets a ticker update', () => {
251256
function App() {
252257
let [toggle, setToggle] = React.useState(0);
@@ -268,6 +273,7 @@ function runActTests(label, render, unmount) {
268273

269274
expect(container.innerHTML).toBe('1');
270275
});
276+
271277
it('can use the async version to catch microtasks', async () => {
272278
function App() {
273279
let [toggle, setToggle] = React.useState(0);
@@ -289,6 +295,7 @@ function runActTests(label, render, unmount) {
289295

290296
expect(container.innerHTML).toBe('1');
291297
});
298+
292299
it('can handle cascading promises with fake timers', async () => {
293300
// this component triggers an effect, that waits a tick,
294301
// then sets state. repeats this 5 times.
@@ -314,6 +321,7 @@ function runActTests(label, render, unmount) {
314321
// all 5 ticks present and accounted for
315322
expect(container.innerHTML).toBe('5');
316323
});
324+
317325
it('flushes immediate re-renders with act', () => {
318326
function App() {
319327
let [ctr, setCtr] = React.useState(0);
@@ -367,6 +375,7 @@ function runActTests(label, render, unmount) {
367375
);
368376
});
369377
});
378+
370379
describe('asynchronous tests', () => {
371380
it('works with timeouts', async () => {
372381
function App() {
@@ -577,6 +586,7 @@ function runActTests(label, render, unmount) {
577586
});
578587
}
579588
});
589+
580590
describe('error propagation', () => {
581591
it('propagates errors - sync', () => {
582592
let err;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
// sanity tests to make sure act() works without a mocked scheduler
11+
12+
let React;
13+
let ReactDOM;
14+
let act;
15+
let container;
16+
let yields;
17+
18+
function clearYields() {
19+
try {
20+
return yields;
21+
} finally {
22+
yields = [];
23+
}
24+
}
25+
26+
function render(el, dom) {
27+
ReactDOM.render(el, dom);
28+
}
29+
30+
function unmount(dom) {
31+
ReactDOM.unmountComponentAtNode(dom);
32+
}
33+
34+
beforeEach(() => {
35+
jest.resetModules();
36+
jest.unmock('scheduler');
37+
yields = [];
38+
React = require('react');
39+
ReactDOM = require('react-dom');
40+
act = require('react-dom/test-utils').act;
41+
container = document.createElement('div');
42+
document.body.appendChild(container);
43+
});
44+
45+
afterEach(() => {
46+
unmount(container);
47+
document.body.removeChild(container);
48+
});
49+
50+
it('can use act to flush effects', () => {
51+
function App() {
52+
React.useEffect(() => {
53+
yields.push(100);
54+
});
55+
return null;
56+
}
57+
58+
act(() => {
59+
render(<App />, container);
60+
});
61+
62+
expect(clearYields()).toEqual([100]);
63+
});
64+
65+
it('flushes effects on every call', () => {
66+
function App() {
67+
let [ctr, setCtr] = React.useState(0);
68+
React.useEffect(() => {
69+
yields.push(ctr);
70+
});
71+
return (
72+
<button id="button" onClick={() => setCtr(x => x + 1)}>
73+
{ctr}
74+
</button>
75+
);
76+
}
77+
78+
act(() => {
79+
render(<App />, container);
80+
});
81+
82+
expect(clearYields()).toEqual([0]);
83+
84+
const button = container.querySelector('#button');
85+
function click() {
86+
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
87+
}
88+
89+
act(() => {
90+
click();
91+
click();
92+
click();
93+
});
94+
// it consolidates the 3 updates, then fires the effect
95+
expect(clearYields()).toEqual([3]);
96+
act(click);
97+
expect(clearYields()).toEqual([4]);
98+
act(click);
99+
expect(clearYields()).toEqual([5]);
100+
expect(button.innerHTML).toEqual('5');
101+
});
102+
103+
it("should keep flushing effects until the're done", () => {
104+
function App() {
105+
let [ctr, setCtr] = React.useState(0);
106+
React.useEffect(() => {
107+
if (ctr < 5) {
108+
setCtr(x => x + 1);
109+
}
110+
});
111+
return ctr;
112+
}
113+
114+
act(() => {
115+
render(<App />, container);
116+
});
117+
118+
expect(container.innerHTML).toEqual('5');
119+
});
120+
121+
it('should flush effects only on exiting the outermost act', () => {
122+
function App() {
123+
React.useEffect(() => {
124+
yields.push(0);
125+
});
126+
return null;
127+
}
128+
// let's nest a couple of act() calls
129+
act(() => {
130+
act(() => {
131+
render(<App />, container);
132+
});
133+
// the effect wouldn't have yielded yet because
134+
// we're still inside an act() scope
135+
expect(clearYields()).toEqual([]);
136+
});
137+
// but after exiting the last one, effects get flushed
138+
expect(clearYields()).toEqual([0]);
139+
});
140+
141+
it('can handle cascading promises', async () => {
142+
// this component triggers an effect, that waits a tick,
143+
// then sets state. repeats this 5 times.
144+
function App() {
145+
let [state, setState] = React.useState(0);
146+
async function ticker() {
147+
await null;
148+
setState(x => x + 1);
149+
}
150+
React.useEffect(
151+
() => {
152+
yields.push(state);
153+
ticker();
154+
},
155+
[Math.min(state, 4)],
156+
);
157+
return state;
158+
}
159+
160+
await act(async () => {
161+
render(<App />, container);
162+
});
163+
// all 5 ticks present and accounted for
164+
expect(clearYields()).toEqual([0, 1, 2, 3, 4]);
165+
expect(container.innerHTML).toBe('5');
166+
});

packages/react-dom/src/test-utils/ReactTestUtilsAct.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ const flushWork =
6363
hasWarnedAboutMissingMockScheduler = true;
6464
}
6565
}
66-
while (flushPassiveEffects()) {}
66+
67+
let didFlushWork = false;
68+
while (flushPassiveEffects()) {
69+
didFlushWork = true;
70+
}
71+
72+
return didFlushWork;
6773
};
6874

6975
function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) {

packages/react-noop-renderer/src/createReactNoop.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,13 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
619619
hasWarnedAboutMissingMockScheduler = true;
620620
}
621621
}
622-
while (flushPassiveEffects()) {}
622+
623+
let didFlushWork = false;
624+
while (flushPassiveEffects()) {
625+
didFlushWork = true;
626+
}
627+
628+
return didFlushWork;
623629
};
624630

625631
function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) {

packages/react-test-renderer/src/ReactTestRendererAct.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ const flushWork =
4444
hasWarnedAboutMissingMockScheduler = true;
4545
}
4646
}
47-
while (flushPassiveEffects()) {}
47+
48+
let didFlushWork = false;
49+
while (flushPassiveEffects()) {
50+
didFlushWork = true;
51+
}
52+
53+
return didFlushWork;
4854
};
4955

5056
function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) {

0 commit comments

Comments
 (0)