@@ -21,6 +21,10 @@ describe('ReactLazy', () => {
21
21
return props . text ;
22
22
}
23
23
24
+ function delay ( ms ) {
25
+ return new Promise ( resolve => setTimeout ( ( ) => resolve ( ) , ms ) ) ;
26
+ }
27
+
24
28
async function fakeImport ( result ) {
25
29
return { default : result } ;
26
30
}
@@ -40,7 +44,7 @@ describe('ReactLazy', () => {
40
44
expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
41
45
expect ( root ) . toMatchRenderedOutput ( null ) ;
42
46
43
- await LazyText ;
47
+ await Promise . resolve ( ) ;
44
48
45
49
expect ( root ) . toFlushAndYield ( [ 'Hi' ] ) ;
46
50
expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
@@ -55,6 +59,47 @@ describe('ReactLazy', () => {
55
59
expect ( root ) . toMatchRenderedOutput ( 'Hi again' ) ;
56
60
} ) ;
57
61
62
+ it ( 'multiple lazy components' , async ( ) => {
63
+ function Foo ( ) {
64
+ return < Text text = "Foo" /> ;
65
+ }
66
+
67
+ function Bar ( ) {
68
+ return < Text text = "Bar" /> ;
69
+ }
70
+
71
+ const promiseForFoo = delay ( 1000 ) . then ( ( ) => fakeImport ( Foo ) ) ;
72
+ const promiseForBar = delay ( 2000 ) . then ( ( ) => fakeImport ( Bar ) ) ;
73
+
74
+ const LazyFoo = lazy ( ( ) => promiseForFoo ) ;
75
+ const LazyBar = lazy ( ( ) => promiseForBar ) ;
76
+
77
+ const root = ReactTestRenderer . create (
78
+ < Suspense fallback = { < Text text = "Loading..." /> } >
79
+ < LazyFoo />
80
+ < LazyBar />
81
+ </ Suspense > ,
82
+ {
83
+ unstable_isConcurrent : true ,
84
+ } ,
85
+ ) ;
86
+
87
+ expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
88
+ expect ( root ) . toMatchRenderedOutput ( null ) ;
89
+
90
+ jest . advanceTimersByTime ( 1000 ) ;
91
+ await promiseForFoo ;
92
+
93
+ expect ( root ) . toFlushAndYield ( [ 'Foo' , 'Loading...' ] ) ;
94
+ expect ( root ) . toMatchRenderedOutput ( null ) ;
95
+
96
+ jest . advanceTimersByTime ( 1000 ) ;
97
+ await promiseForBar ;
98
+
99
+ expect ( root ) . toFlushAndYield ( [ 'Foo' , 'Bar' ] ) ;
100
+ expect ( root ) . toMatchRenderedOutput ( 'FooBar' ) ;
101
+ } ) ;
102
+
58
103
it ( 'does not support arbitrary promises, only module objects' , async ( ) => {
59
104
spyOnDev ( console , 'error' ) ;
60
105
@@ -71,7 +116,7 @@ describe('ReactLazy', () => {
71
116
expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
72
117
expect ( root ) . toMatchRenderedOutput ( null ) ;
73
118
74
- await LazyText ;
119
+ await Promise . resolve ( ) ;
75
120
76
121
if ( __DEV__ ) {
77
122
expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -100,7 +145,7 @@ describe('ReactLazy', () => {
100
145
expect ( root ) . toMatchRenderedOutput ( null ) ;
101
146
102
147
try {
103
- await LazyText ;
148
+ await Promise . resolve ( ) ;
104
149
} catch ( e ) { }
105
150
106
151
expect ( root ) . toFlushAndThrow ( 'Bad network' ) ;
@@ -176,7 +221,7 @@ describe('ReactLazy', () => {
176
221
expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
177
222
expect ( root ) . toMatchRenderedOutput ( null ) ;
178
223
179
- await LazyText ;
224
+ await Promise . resolve ( ) ;
180
225
181
226
expect ( root ) . toFlushAndYield ( [ 'Hi' ] ) ;
182
227
expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
@@ -226,7 +271,7 @@ describe('ReactLazy', () => {
226
271
expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
227
272
expect ( root ) . toMatchRenderedOutput ( null ) ;
228
273
229
- await Lazy ;
274
+ await Promise . resolve ( ) ;
230
275
231
276
expect ( root ) . toFlushAndYield ( [ 'Lazy' , 'Sibling' , 'A' ] ) ;
232
277
expect ( root ) . toMatchRenderedOutput ( 'SiblingA' ) ;
@@ -256,7 +301,7 @@ describe('ReactLazy', () => {
256
301
expect ( root ) . toFlushAndYield ( [ 'Started loading' , 'Loading...' ] ) ;
257
302
expect ( root ) . toMatchRenderedOutput ( null ) ;
258
303
259
- await LazyFoo ;
304
+ await Promise . resolve ( ) ;
260
305
261
306
expect ( ( ) => {
262
307
expect ( root ) . toFlushAndYield ( [ 'A' , 'B' ] ) ;
@@ -303,8 +348,7 @@ describe('ReactLazy', () => {
303
348
expect ( root ) . toMatchRenderedOutput ( null ) ;
304
349
expect ( ref . current ) . toBe ( null ) ;
305
350
306
- await LazyClass ;
307
- await LazyForwardRef ;
351
+ await Promise . resolve ( ) ;
308
352
309
353
expect ( root ) . toFlushAndYield ( [ 'Foo' , 'forwardRef' , 'Bar' ] ) ;
310
354
expect ( root ) . toMatchRenderedOutput ( 'FooBar' ) ;
0 commit comments