@@ -18,6 +18,8 @@ let Scheduler;
18
18
let Suspense ;
19
19
let TextResource ;
20
20
let textResourceShouldFail ;
21
+ let waitForAll ;
22
+ let assertLog ;
21
23
22
24
describe ( 'ReactCache' , ( ) => {
23
25
beforeEach ( ( ) => {
@@ -33,6 +35,10 @@ describe('ReactCache', () => {
33
35
ReactTestRenderer = require ( 'react-test-renderer' ) ;
34
36
Scheduler = require ( 'scheduler' ) ;
35
37
38
+ const InternalTestUtils = require ( 'internal-test-utils' ) ;
39
+ waitForAll = InternalTestUtils . waitForAll ;
40
+ assertLog = InternalTestUtils . assertLog ;
41
+
36
42
TextResource = createResource (
37
43
( [ text , ms = 0 ] ) => {
38
44
let listeners = null ;
@@ -105,7 +111,7 @@ describe('ReactCache', () => {
105
111
}
106
112
}
107
113
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 ( ) => {
109
115
function App ( ) {
110
116
return (
111
117
< Suspense fallback = { < Text text = "Loading..." /> } >
@@ -118,11 +124,11 @@ describe('ReactCache', () => {
118
124
unstable_isConcurrent : true ,
119
125
} ) ;
120
126
121
- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
127
+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
122
128
123
129
jest . advanceTimersByTime ( 100 ) ;
124
- expect ( Scheduler ) . toHaveYielded ( [ 'Promise resolved [Hi]' ] ) ;
125
- expect ( Scheduler ) . toFlushAndYield ( [ 'Hi' ] ) ;
130
+ assertLog ( [ 'Promise resolved [Hi]' ] ) ;
131
+ await waitForAll ( [ 'Hi' ] ) ;
126
132
} ) ;
127
133
128
134
it ( 'throws an error on the subsequent read if the promise is rejected' , async ( ) => {
@@ -138,22 +144,22 @@ describe('ReactCache', () => {
138
144
unstable_isConcurrent : true ,
139
145
} ) ;
140
146
141
- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
147
+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
142
148
143
149
textResourceShouldFail = true ;
144
150
jest . advanceTimersByTime ( 100 ) ;
145
- expect ( Scheduler ) . toHaveYielded ( [ 'Promise rejected [Hi]' ] ) ;
151
+ assertLog ( [ 'Promise rejected [Hi]' ] ) ;
146
152
147
153
expect ( Scheduler ) . toFlushAndThrow ( 'Failed to load: Hi' ) ;
148
- expect ( Scheduler ) . toHaveYielded ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
154
+ assertLog ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
149
155
150
156
// Should throw again on a subsequent read
151
157
root . update ( < App /> ) ;
152
158
expect ( Scheduler ) . toFlushAndThrow ( 'Failed to load: Hi' ) ;
153
- expect ( Scheduler ) . toHaveYielded ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
159
+ assertLog ( [ 'Error! [Hi]' , 'Error! [Hi]' ] ) ;
154
160
} ) ;
155
161
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 ( ) => {
157
163
const BadTextResource = createResource ( ( [ text , ms = 0 ] ) => {
158
164
return new Promise ( ( resolve , reject ) =>
159
165
setTimeout ( ( ) => {
@@ -177,16 +183,16 @@ describe('ReactCache', () => {
177
183
) ;
178
184
179
185
if ( __DEV__ ) {
180
- expect ( ( ) => {
181
- expect ( Scheduler ) . toFlushAndYield ( [ 'App' , 'Loading...' ] ) ;
186
+ expect ( async ( ) => {
187
+ await waitForAll ( [ 'App' , 'Loading...' ] ) ;
182
188
} ) . toErrorDev ( [
183
189
'Invalid key type. Expected a string, number, symbol, or ' +
184
190
'boolean, but instead received: Hi,100\n\n' +
185
191
'To use non-primitive values as keys, you must pass a hash ' +
186
192
'function as the second argument to createResource().' ,
187
193
] ) ;
188
194
} else {
189
- expect ( Scheduler ) . toFlushAndYield ( [ 'App' , 'Loading...' ] ) ;
195
+ await waitForAll ( [ 'App' , 'Loading...' ] ) ;
190
196
}
191
197
} ) ;
192
198
@@ -204,19 +210,19 @@ describe('ReactCache', () => {
204
210
unstable_isConcurrent : true ,
205
211
} ,
206
212
) ;
207
- expect ( Scheduler ) . toFlushAndYield ( [
213
+ await waitForAll ( [
208
214
'Suspend! [1]' ,
209
215
'Suspend! [2]' ,
210
216
'Suspend! [3]' ,
211
217
'Loading...' ,
212
218
] ) ;
213
219
jest . advanceTimersByTime ( 100 ) ;
214
- expect ( Scheduler ) . toHaveYielded ( [
220
+ assertLog ( [
215
221
'Promise resolved [1]' ,
216
222
'Promise resolved [2]' ,
217
223
'Promise resolved [3]' ,
218
224
] ) ;
219
- expect ( Scheduler ) . toFlushAndYield ( [ 1 , 2 , 3 ] ) ;
225
+ await waitForAll ( [ 1 , 2 , 3 ] ) ;
220
226
expect ( root ) . toMatchRenderedOutput ( '123' ) ;
221
227
222
228
// Render 1, 4, 5
@@ -228,18 +234,10 @@ describe('ReactCache', () => {
228
234
</ Suspense > ,
229
235
) ;
230
236
231
- expect ( Scheduler ) . toFlushAndYield ( [
232
- 1 ,
233
- 'Suspend! [4]' ,
234
- 'Suspend! [5]' ,
235
- 'Loading...' ,
236
- ] ) ;
237
+ await waitForAll ( [ 1 , 'Suspend! [4]' , 'Suspend! [5]' , 'Loading...' ] ) ;
237
238
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 ] ) ;
243
241
expect ( root ) . toMatchRenderedOutput ( '145' ) ;
244
242
245
243
// We've now rendered values 1, 2, 3, 4, 5, over our limit of 3. The least
@@ -253,7 +251,7 @@ describe('ReactCache', () => {
253
251
</ Suspense > ,
254
252
) ;
255
253
256
- expect ( Scheduler ) . toFlushAndYield ( [
254
+ await waitForAll ( [
257
255
// 1 is still cached
258
256
1 ,
259
257
// 2 and 3 suspend because they were evicted from the cache
@@ -262,11 +260,8 @@ describe('ReactCache', () => {
262
260
'Loading...' ,
263
261
] ) ;
264
262
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 ] ) ;
270
265
expect ( root ) . toMatchRenderedOutput ( '123' ) ;
271
266
} ) ;
272
267
@@ -287,18 +282,15 @@ describe('ReactCache', () => {
287
282
} ,
288
283
) ;
289
284
290
- expect ( Scheduler ) . toFlushAndYield ( [ 'Loading...' ] ) ;
285
+ await waitForAll ( [ 'Loading...' ] ) ;
291
286
292
287
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' ] ) ;
298
290
expect ( root ) . toMatchRenderedOutput ( 'Result' ) ;
299
291
} ) ;
300
292
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 ( ) => {
302
294
let resolveThenable ;
303
295
const BadTextResource = createResource (
304
296
( [ text , ms = 0 ] ) => {
@@ -349,7 +341,7 @@ describe('ReactCache', () => {
349
341
} ,
350
342
) ;
351
343
352
- expect ( Scheduler ) . toFlushAndYield ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
344
+ await waitForAll ( [ 'Suspend! [Hi]' , 'Loading...' ] ) ;
353
345
354
346
resolveThenable ( 'Hi' ) ;
355
347
// This thenable improperly resolves twice. We should not update the
@@ -365,8 +357,8 @@ describe('ReactCache', () => {
365
357
} ,
366
358
) ;
367
359
368
- expect ( Scheduler ) . toHaveYielded ( [ ] ) ;
369
- expect ( Scheduler ) . toFlushAndYield ( [ 'Hi' ] ) ;
360
+ assertLog ( [ ] ) ;
361
+ await waitForAll ( [ 'Hi' ] ) ;
370
362
expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
371
363
} ) ;
372
364
0 commit comments