1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @emails react-core
    
  8.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. let ReactDOM;
    
  13. let React;
    
  14. let ReactCache;
    
  15. let ReactTestRenderer;
    
  16. let waitForAll;
    
  17. 
    
  18. describe('ReactTestRenderer', () => {
    
  19.   beforeEach(() => {
    
  20.     jest.resetModules();
    
  21.     ReactDOM = require('react-dom');
    
  22. 
    
  23.     // Isolate test renderer.
    
  24.     jest.resetModules();
    
  25.     React = require('react');
    
  26.     ReactCache = require('react-cache');
    
  27.     ReactTestRenderer = require('react-test-renderer');
    
  28.     const InternalTestUtils = require('internal-test-utils');
    
  29.     waitForAll = InternalTestUtils.waitForAll;
    
  30.   });
    
  31. 
    
  32.   it('should warn if used to render a ReactDOM portal', () => {
    
  33.     const container = document.createElement('div');
    
  34.     expect(() => {
    
  35.       let error;
    
  36.       try {
    
  37.         ReactTestRenderer.create(ReactDOM.createPortal('foo', container));
    
  38.       } catch (e) {
    
  39.         error = e;
    
  40.       }
    
  41.       // After the update throws, a subsequent render is scheduled to
    
  42.       // unmount the whole tree. This update also causes an error, so React
    
  43.       // throws an AggregateError.
    
  44.       const errors = error.errors;
    
  45.       expect(errors.length).toBe(2);
    
  46.       expect(errors[0].message.includes('indexOf is not a function')).toBe(
    
  47.         true,
    
  48.       );
    
  49.       expect(errors[1].message.includes('indexOf is not a function')).toBe(
    
  50.         true,
    
  51.       );
    
  52.     }).toErrorDev('An invalid container has been provided.', {
    
  53.       withoutStack: true,
    
  54.     });
    
  55.   });
    
  56. 
    
  57.   describe('timed out Suspense hidden subtrees should not be observable via toJSON', () => {
    
  58.     let AsyncText;
    
  59.     let PendingResources;
    
  60.     let TextResource;
    
  61. 
    
  62.     beforeEach(() => {
    
  63.       PendingResources = {};
    
  64.       TextResource = ReactCache.unstable_createResource(
    
  65.         text =>
    
  66.           new Promise(resolve => {
    
  67.             PendingResources[text] = resolve;
    
  68.           }),
    
  69.         text => text,
    
  70.       );
    
  71. 
    
  72.       AsyncText = ({text}) => {
    
  73.         const value = TextResource.read(text);
    
  74.         return value;
    
  75.       };
    
  76.     });
    
  77. 
    
  78.     it('for root Suspense components', async () => {
    
  79.       const App = ({text}) => {
    
  80.         return (
    
  81.           <React.Suspense fallback="fallback">
    
  82.             <AsyncText text={text} />
    
  83.           </React.Suspense>
    
  84.         );
    
  85.       };
    
  86. 
    
  87.       const root = ReactTestRenderer.create(<App text="initial" />);
    
  88.       PendingResources.initial('initial');
    
  89.       await waitForAll([]);
    
  90.       expect(root.toJSON()).toEqual('initial');
    
  91. 
    
  92.       root.update(<App text="dynamic" />);
    
  93.       expect(root.toJSON()).toEqual('fallback');
    
  94. 
    
  95.       PendingResources.dynamic('dynamic');
    
  96.       await waitForAll([]);
    
  97.       expect(root.toJSON()).toEqual('dynamic');
    
  98.     });
    
  99. 
    
  100.     it('for nested Suspense components', async () => {
    
  101.       const App = ({text}) => {
    
  102.         return (
    
  103.           <div>
    
  104.             <React.Suspense fallback="fallback">
    
  105.               <AsyncText text={text} />
    
  106.             </React.Suspense>
    
  107.           </div>
    
  108.         );
    
  109.       };
    
  110. 
    
  111.       const root = ReactTestRenderer.create(<App text="initial" />);
    
  112.       PendingResources.initial('initial');
    
  113.       await waitForAll([]);
    
  114.       expect(root.toJSON().children).toEqual(['initial']);
    
  115. 
    
  116.       root.update(<App text="dynamic" />);
    
  117.       expect(root.toJSON().children).toEqual(['fallback']);
    
  118. 
    
  119.       PendingResources.dynamic('dynamic');
    
  120.       await waitForAll([]);
    
  121.       expect(root.toJSON().children).toEqual(['dynamic']);
    
  122.     });
    
  123.   });
    
  124. });