1273
1274
expect(componentStackMessage).toContain('in Lazy');
1275
});
1276
+
1277
+ // @gate enableLazyElements && enableNewReconciler
1278
+ it('mount and reorder lazy elements', async () => {
1279
+ class Child extends React.Component {
1280
+ componentDidMount() {
1281
+ Scheduler.unstable_yieldValue('Did mount: ' + this.props.label);
1282
+ }
1283
+ componentDidUpdate() {
1284
+ Scheduler.unstable_yieldValue('Did update: ' + this.props.label);
1285
+ }
1286
+ render() {
1287
+ return <Text text={this.props.label} />;
1288
+ }
1289
+ }
1290
+
1291
+ const lazyChildA = lazy(() => {
1292
+ Scheduler.unstable_yieldValue('Init A');
1293
+ return fakeImport(<Child key="A" label="A" />);
1294
+ });
1295
+ const lazyChildB = lazy(() => {
1296
+ Scheduler.unstable_yieldValue('Init B');
1297
+ return fakeImport(<Child key="B" label="B" />);
1298
+ });
1299
+ const lazyChildA2 = lazy(() => {
1300
+ Scheduler.unstable_yieldValue('Init A2');
1301
+ return fakeImport(<Child key="A" label="a" />);
1302
+ });
1303
+ const lazyChildB2 = lazy(() => {
1304
+ Scheduler.unstable_yieldValue('Init B2');
1305
+ return fakeImport(<Child key="B" label="b" />);
1306
+ });
1307
+
1308
+ function Parent({swap}) {
1309
+ return (
1310
+ <Suspense fallback={<Text text="Loading..." />}>
1311
+ {swap ? [lazyChildB2, lazyChildA2] : [lazyChildA, lazyChildB]}
1312
+ </Suspense>
1313
+ );
1314
+ }
1315
+
1316
+ const root = ReactTestRenderer.create(<Parent swap={false} />, {
1317
+ unstable_isConcurrent: true,
1318
+ });
1319
+
1320
+ expect(Scheduler).toFlushAndYield(['Init A', 'Loading...']);
1321
+ expect(root).not.toMatchRenderedOutput('AB');
1322
+
1323
+ await lazyChildA;
1324
+ // We need to flush to trigger the B to load.
1325
+ expect(Scheduler).toFlushAndYield(['Init B']);
1326
+ await lazyChildB;
1327
+
1328
+ expect(Scheduler).toFlushAndYield([
1329
+ 'A',
1330
+ 'B',
1331
+ 'Did mount: A',
1332
+ 'Did mount: B',
1333
+ ]);
1334
+ expect(root).toMatchRenderedOutput('AB');
1335
+
1336
+ // Swap the position of A and B
1337
+ root.update(<Parent swap={true} />);
1338
+ expect(Scheduler).toFlushAndYield(['Init B2', 'Loading...']);
1339
+ await lazyChildB2;
1340
+ // We need to flush to trigger the second one to load.
1341
+ expect(Scheduler).toFlushAndYield(['Init A2', 'Loading...']);
1342
+ await lazyChildA2;
1343
+
1344
+ expect(Scheduler).toFlushAndYield([
1345
+ 'b',
1346
+ 'a',
1347
+ 'Did update: b',
1348
+ 'Did update: a',
1349
+ ]);
1350
+ expect(root).toMatchRenderedOutput('ba');
1351
+ });
1352
});