@@ -21,6 +21,10 @@ describe('ReactLazy', () => {
2121 return props . text ;
2222 }
2323
24+ function delay ( ms ) {
25+ return new Promise ( resolve => setTimeout ( ( ) => resolve ( ) , ms ) ) ;
26+ }
27+
2428 async function fakeImport ( result ) {
2529 return { default : result } ;
2630 }
@@ -40,7 +44,7 @@ describe('ReactLazy', () => {
4044 expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
4145 expect ( root ) . toMatchRenderedOutput ( null ) ;
4246
43- await LazyText ;
47+ await Promise . resolve ( ) ;
4448
4549 expect ( root ) . toFlushAndYield ( [ 'Hi' ] ) ;
4650 expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
@@ -55,6 +59,47 @@ describe('ReactLazy', () => {
5559 expect ( root ) . toMatchRenderedOutput ( 'Hi again' ) ;
5660 } ) ;
5761
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+
58103 it ( 'does not support arbitrary promises, only module objects' , async ( ) => {
59104 spyOnDev ( console , 'error' ) ;
60105
@@ -71,7 +116,7 @@ describe('ReactLazy', () => {
71116 expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
72117 expect ( root ) . toMatchRenderedOutput ( null ) ;
73118
74- await LazyText ;
119+ await Promise . resolve ( ) ;
75120
76121 if ( __DEV__ ) {
77122 expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -100,7 +145,7 @@ describe('ReactLazy', () => {
100145 expect ( root ) . toMatchRenderedOutput ( null ) ;
101146
102147 try {
103- await LazyText ;
148+ await Promise . resolve ( ) ;
104149 } catch ( e ) { }
105150
106151 expect ( root ) . toFlushAndThrow ( 'Bad network' ) ;
@@ -176,7 +221,7 @@ describe('ReactLazy', () => {
176221 expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
177222 expect ( root ) . toMatchRenderedOutput ( null ) ;
178223
179- await LazyText ;
224+ await Promise . resolve ( ) ;
180225
181226 expect ( root ) . toFlushAndYield ( [ 'Hi' ] ) ;
182227 expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
@@ -226,7 +271,7 @@ describe('ReactLazy', () => {
226271 expect ( root ) . toFlushAndYield ( [ 'Loading...' ] ) ;
227272 expect ( root ) . toMatchRenderedOutput ( null ) ;
228273
229- await Lazy ;
274+ await Promise . resolve ( ) ;
230275
231276 expect ( root ) . toFlushAndYield ( [ 'Lazy' , 'Sibling' , 'A' ] ) ;
232277 expect ( root ) . toMatchRenderedOutput ( 'SiblingA' ) ;
@@ -256,7 +301,7 @@ describe('ReactLazy', () => {
256301 expect ( root ) . toFlushAndYield ( [ 'Started loading' , 'Loading...' ] ) ;
257302 expect ( root ) . toMatchRenderedOutput ( null ) ;
258303
259- await LazyFoo ;
304+ await Promise . resolve ( ) ;
260305
261306 expect ( ( ) => {
262307 expect ( root ) . toFlushAndYield ( [ 'A' , 'B' ] ) ;
@@ -303,8 +348,7 @@ describe('ReactLazy', () => {
303348 expect ( root ) . toMatchRenderedOutput ( null ) ;
304349 expect ( ref . current ) . toBe ( null ) ;
305350
306- await LazyClass ;
307- await LazyForwardRef ;
351+ await Promise . resolve ( ) ;
308352
309353 expect ( root ) . toFlushAndYield ( [ 'Foo' , 'forwardRef' , 'Bar' ] ) ;
310354 expect ( root ) . toMatchRenderedOutput ( 'FooBar' ) ;
0 commit comments