9
9
10
10
'use strict' ;
11
11
12
+ let ReactFeatureFlags ;
13
+ let enableNewScheduler ;
12
14
let React ;
13
15
let ReactDOM ;
14
16
let Scheduler ;
@@ -19,6 +21,8 @@ describe('ReactDOMHooks', () => {
19
21
beforeEach ( ( ) => {
20
22
jest . resetModules ( ) ;
21
23
24
+ ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
25
+ enableNewScheduler = ReactFeatureFlags . enableNewScheduler ;
22
26
React = require ( 'react' ) ;
23
27
ReactDOM = require ( 'react-dom' ) ;
24
28
Scheduler = require ( 'scheduler' ) ;
@@ -72,8 +76,7 @@ describe('ReactDOMHooks', () => {
72
76
expect ( container3 . textContent ) . toBe ( '6' ) ;
73
77
} ) ;
74
78
75
- // TODO: This behavior is wrong. Fix this in the old implementation.
76
- it . skip ( 'can batch synchronous work inside effects with other work' , ( ) => {
79
+ it ( 'can batch synchronous work inside effects with other work' , ( ) => {
77
80
let otherContainer = document . createElement ( 'div' ) ;
78
81
79
82
let calledA = false ;
@@ -98,15 +101,30 @@ describe('ReactDOMHooks', () => {
98
101
}
99
102
100
103
ReactDOM . render ( < Foo /> , container ) ;
101
- ReactDOM . unstable_batchedUpdates ( ( ) => {
102
- _set ( 0 ) ; // Forces the effect to be flushed
103
- expect ( otherContainer . textContent ) . toBe ( '' ) ;
104
- ReactDOM . render ( < B /> , otherContainer ) ;
105
- expect ( otherContainer . textContent ) . toBe ( '' ) ;
106
- } ) ;
107
- expect ( otherContainer . textContent ) . toBe ( 'B' ) ;
108
- expect ( calledA ) . toBe ( false ) ; // It was in a batch
109
- expect ( calledB ) . toBe ( true ) ;
104
+
105
+ if ( enableNewScheduler ) {
106
+ // The old behavior was accidental; in the new scheduler, flushing passive
107
+ // effects also flushes synchronous work, even inside batchedUpdates.
108
+ ReactDOM . unstable_batchedUpdates ( ( ) => {
109
+ _set ( 0 ) ; // Forces the effect to be flushed
110
+ expect ( otherContainer . textContent ) . toBe ( 'A' ) ;
111
+ ReactDOM . render ( < B /> , otherContainer ) ;
112
+ expect ( otherContainer . textContent ) . toBe ( 'A' ) ;
113
+ } ) ;
114
+ expect ( otherContainer . textContent ) . toBe ( 'B' ) ;
115
+ expect ( calledA ) . toBe ( true ) ;
116
+ expect ( calledB ) . toBe ( true ) ;
117
+ } else {
118
+ ReactDOM . unstable_batchedUpdates ( ( ) => {
119
+ _set ( 0 ) ; // Forces the effect to be flushed
120
+ expect ( otherContainer . textContent ) . toBe ( '' ) ;
121
+ ReactDOM . render ( < B /> , otherContainer ) ;
122
+ expect ( otherContainer . textContent ) . toBe ( '' ) ;
123
+ } ) ;
124
+ expect ( otherContainer . textContent ) . toBe ( 'B' ) ;
125
+ expect ( calledA ) . toBe ( false ) ; // It was in a batch
126
+ expect ( calledB ) . toBe ( true ) ;
127
+ }
110
128
} ) ;
111
129
112
130
it ( 'should not bail out when an update is scheduled from within an event handler' , ( ) => {
0 commit comments