@@ -18,6 +18,8 @@ let Scheduler;
1818let Suspense ;
1919let TextResource ;
2020let textResourceShouldFail ;
21+ let waitForAll ;
22+ let assertLog ;
2123
2224describe ( 'ReactCache' , ( ) => {
2325 beforeEach ( ( ) => {
@@ -33,6 +35,10 @@ describe('ReactCache', () => {
3335 ReactTestRenderer = require ( 'react-test-renderer' ) ;
3436 Scheduler = require ( 'scheduler' ) ;
3537
38+ const InternalTestUtils = require ( 'internal-test-utils' ) ;
39+ waitForAll = InternalTestUtils . waitForAll ;
40+ assertLog = InternalTestUtils . assertLog ;
41+
3642 TextResource = createResource (
3743 ( [ text , ms = 0 ] ) => {
3844 let listeners = null ;
@@ -105,7 +111,7 @@ describe('ReactCache', () => {
105111 }
106112 }
107113
108- it ( 'throws a promise if the requested value is not in the cache' , ( ) => {
114+ it ( 'throws a promise if the requested value is not in the cache' , async ( ) => {
109115 function App ( ) {
110116 return (
111117 < Suspense fallback = { < Text text = "Loading..." /> } >
@@ -118,11 +124,11 @@ describe('ReactCache', () => {
118124 unstable_isConcurrent : true ,
119125 } ) ;
120126
121- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
127+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
122128
123129 jest . advanceTimersByTime ( 100 ) ;
124- expect ( Scheduler ) . toHaveYielded ( [ 'Promise resolved [Hi]' ] ) ;
125- expect ( Scheduler ) . toFlushAndYield ( [ 'Hi' ] ) ;
130+ assertLog ( [ 'Promise resolved [Hi]' ] ) ;
131+ await waitForAll ( [ 'Hi' ] ) ;
126132 } ) ;
127133
128134 it ( 'throws an error on the subsequent read if the promise is rejected' , async ( ) => {
@@ -138,22 +144,22 @@ describe('ReactCache', () => {
138144 unstable_isConcurrent : true ,
139145 } ) ;
140146
141- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
147+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
142148
143149 textResourceShouldFail = true ;
144150 jest . advanceTimersByTime ( 100 ) ;
145- expect ( Scheduler ) . toHaveYielded ( [ 'Promise rejected [Hi]' ] ) ;
151+ assertLog ( [ 'Promise rejected [Hi]' ] ) ;
146152
147153 expect ( Scheduler ) . toFlushAndThrow ( 'Failed to load: Hi' ) ;
148- expect ( Scheduler ) . toHaveYielded ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
154+ assertLog ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
149155
150156 // Should throw again on a subsequent read
151157 root . update ( < App /> ) ;
152158 expect ( Scheduler ) . toFlushAndThrow ( 'Failed to load: Hi' ) ;
153- expect ( Scheduler ) . toHaveYielded ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
159+ assertLog ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
154160 } ) ;
155161
156- it ( 'warns if non-primitive key is passed to a resource without a hash function' , ( ) => {
162+ it ( 'warns if non-primitive key is passed to a resource without a hash function' , async ( ) => {
157163 const BadTextResource = createResource ( ( [ text , ms = 0 ] ) => {
158164 return new Promise ( ( resolve , reject ) =>
159165 setTimeout ( ( ) => {
@@ -177,16 +183,16 @@ describe('ReactCache', () => {
177183 ) ;
178184
179185 if ( __DEV__ ) {
180- expect ( ( ) => {
181- expect ( Scheduler ) . toFlushAndYield ( [ 'App' , 'Loading...' ] ) ;
186+ expect ( async ( ) => {
187+ await waitForAll ( [ 'App' , 'Loading...' ] ) ;
182188 } ) . toErrorDev ( [
183189 'Invalid key type. Expected a string, number, symbol, or ' +
184190 'boolean, but instead received: Hi,100\n\n' +
185191 'To use non-primitive values as keys, you must pass a hash ' +
186192 'function as the second argument to createResource().' ,
187193 ] ) ;
188194 } else {
189- expect ( Scheduler ) . toFlushAndYield ( [ 'App' , 'Loading...' ] ) ;
195+ await waitForAll ( [ 'App' , 'Loading...' ] ) ;
190196 }
191197 } ) ;
192198
@@ -204,19 +210,19 @@ describe('ReactCache', () => {
204210 unstable_isConcurrent : true ,
205211 } ,
206212 ) ;
207- expect ( Scheduler ) . toFlushAndYield ( [
213+ await waitForAll ( [
208214 'Suspend! [1]' ,
209215 'Suspend! [2]' ,
210216 'Suspend! [3]' ,
211217 'Loading...' ,
212218 ] ) ;
213219 jest . advanceTimersByTime ( 100 ) ;
214- expect ( Scheduler ) . toHaveYielded ( [
220+ assertLog ( [
215221 'Promise resolved [1]' ,
216222 'Promise resolved [2]' ,
217223 'Promise resolved [3]' ,
218224 ] ) ;
219- expect ( Scheduler ) . toFlushAndYield ( [ 1 , 2 , 3 ] ) ;
225+ await waitForAll ( [ 1 , 2 , 3 ] ) ;
220226 expect ( root ) . toMatchRenderedOutput ( '123' ) ;
221227
222228 // Render 1, 4, 5
@@ -228,18 +234,10 @@ describe('ReactCache', () => {
228234 </ Suspense > ,
229235 ) ;
230236
231- expect ( Scheduler ) . toFlushAndYield ( [
232- 1 ,
233- 'Suspend! [4]' ,
234- 'Suspend! [5]' ,
235- 'Loading...' ,
236- ] ) ;
237+ await waitForAll ( [ 1 , 'Suspend! [4]' , 'Suspend! [5]' , 'Loading...' ] ) ;
237238 jest . advanceTimersByTime ( 100 ) ;
238- expect ( Scheduler ) . toHaveYielded ( [
239- 'Promise resolved [4]' ,
240- 'Promise resolved [5]' ,
241- ] ) ;
242- expect ( Scheduler ) . toFlushAndYield ( [ 1 , 4 , 5 ] ) ;
239+ assertLog ( [ 'Promise resolved [4]' , 'Promise resolved [5]' ] ) ;
240+ await waitForAll ( [ 1 , 4 , 5 ] ) ;
243241 expect ( root ) . toMatchRenderedOutput ( '145' ) ;
244242
245243 // We've now rendered values 1, 2, 3, 4, 5, over our limit of 3. The least
@@ -253,7 +251,7 @@ describe('ReactCache', () => {
253251 </ Suspense > ,
254252 ) ;
255253
256- expect ( Scheduler ) . toFlushAndYield ( [
254+ await waitForAll ( [
257255 // 1 is still cached
258256 1 ,
259257 // 2 and 3 suspend because they were evicted from the cache
@@ -262,11 +260,8 @@ describe('ReactCache', () => {
262260 'Loading...' ,
263261 ] ) ;
264262 jest . advanceTimersByTime ( 100 ) ;
265- expect ( Scheduler ) . toHaveYielded ( [
266- 'Promise resolved [2]' ,
267- 'Promise resolved [3]' ,
268- ] ) ;
269- expect ( Scheduler ) . toFlushAndYield ( [ 1 , 2 , 3 ] ) ;
263+ assertLog ( [ 'Promise resolved [2]' , 'Promise resolved [3]' ] ) ;
264+ await waitForAll ( [ 1 , 2 , 3 ] ) ;
270265 expect ( root ) . toMatchRenderedOutput ( '123' ) ;
271266 } ) ;
272267
@@ -287,18 +282,15 @@ describe('ReactCache', () => {
287282 } ,
288283 ) ;
289284
290- expect ( Scheduler ) . toFlushAndYield ( [ 'Loading...' ] ) ;
285+ await waitForAll ( [ 'Loading...' ] ) ;
291286
292287 jest . advanceTimersByTime ( 1000 ) ;
293- expect ( Scheduler ) . toHaveYielded ( [
294- 'Promise resolved [B]' ,
295- 'Promise resolved [A]' ,
296- ] ) ;
297- expect ( Scheduler ) . toFlushAndYield ( [ 'Result' ] ) ;
288+ assertLog ( [ 'Promise resolved [B]' , 'Promise resolved [A]' ] ) ;
289+ await waitForAll ( [ 'Result' ] ) ;
298290 expect ( root ) . toMatchRenderedOutput ( 'Result' ) ;
299291 } ) ;
300292
301- it ( 'if a thenable resolves multiple times, does not update the first cached value' , ( ) => {
293+ it ( 'if a thenable resolves multiple times, does not update the first cached value' , async ( ) => {
302294 let resolveThenable ;
303295 const BadTextResource = createResource (
304296 ( [ text , ms = 0 ] ) => {
@@ -349,7 +341,7 @@ describe('ReactCache', () => {
349341 } ,
350342 ) ;
351343
352- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
344+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
353345
354346 resolveThenable ( 'Hi' ) ;
355347 // This thenable improperly resolves twice. We should not update the
@@ -365,8 +357,8 @@ describe('ReactCache', () => {
365357 } ,
366358 ) ;
367359
368- expect ( Scheduler ) . toHaveYielded ( [ ] ) ;
369- expect ( Scheduler ) . toFlushAndYield ( [ 'Hi' ] ) ;
360+ assertLog ( [ ] ) ;
361+ await waitForAll ( [ 'Hi' ] ) ;
370362 expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
371363 } ) ;
372364
0 commit comments