Add regression test for #18497 (#18538)
Sophie Alpert committed
Jun 30, 2020 at 04:09 UTC
e9c1445ba09ac0ae73465f28514f9aa45074b7c7
2 files changed
+43
-1
packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
+40
@@ -3314,4 +3314,44 @@ describe('ReactHooksWithNoopRenderer', () => {
3314
});
3315
expect(ReactNoop).toMatchRenderedOutput('ABC');
3316
});
3317
+
3318
+ it('keeps intermediate state updates (issue #18497)', () => {
3319
+ let _dispatch;
3320
+ function Counter() {
3321
+ const [list, dispatch] = React.useReducer((l, c) => l.concat([c]), []);
3322
+ _dispatch = dispatch;
3323
+
3324
+ const json = JSON.stringify(list);
3325
+ Scheduler.unstable_yieldValue('Render ' + json);
3326
+ useLayoutEffect(() => {
3327
+ Scheduler.unstable_yieldValue('Commit ' + json);
3328
+ });
3329
+
3330
+ return json;
3331
+ }
3332
+
3333
+ act(() => {
3334
+ ReactNoop.render(<Counter />);
3335
+ expect(Scheduler).toFlushAndYieldThrough(['Render []', 'Commit []']);
3336
+ expect(ReactNoop).toMatchRenderedOutput('[]');
3337
+ });
3338
+
3339
+ act(() => {
3340
+ _dispatch(1);
3341
+ expect(Scheduler).toFlushAndYieldThrough(['Render [1]']);
3342
+
3343
+ _dispatch(2);
3344
+ expect(Scheduler).toFlushAndYieldThrough(['Commit [1]']);
3345
+ expect(ReactNoop).toMatchRenderedOutput('[1]');
3346
+
3347
+ expect(Scheduler).toFlushAndYieldThrough(['Render [1,2]']);
3348
+ _dispatch(3);
3349
+
3350
+ expect(Scheduler).toFlushAndYieldThrough([
3351
+ 'Render [1,2,3]',
3352
+ 'Commit [1,2,3]',
3353
+ ]);
3354
+ expect(ReactNoop).toMatchRenderedOutput('[1,2,3]');
3355
+ });
3356
+ });
3357
});
scripts/jest/matchers/schedulerTestMatchers.js
+3
-1
@@ -20,7 +20,9 @@ function assertYieldsWereCleared(Scheduler) {
20
const actualYields = Scheduler.unstable_clearYields();
21
if (actualYields.length !== 0) {
22
throw new Error(
23
- 'Log of yielded values is not empty. ' +
23
+ 'Log of yielded values is not empty: ' +
24
+ JSON.stringify(actualYields) +
25
+ '. ' +
26
'Call expect(Scheduler).toHaveYielded(...) first.'
27
);
28
}