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.  * @jest-environment node
    
  8.  */
    
  9. 
    
  10. // sanity tests for act()
    
  11. 
    
  12. const React = require('react');
    
  13. const ReactNoop = require('react-noop-renderer');
    
  14. const Scheduler = require('scheduler');
    
  15. const act = require('internal-test-utils').act;
    
  16. const {assertLog, waitForAll} = require('internal-test-utils');
    
  17. 
    
  18. // TODO: These tests are no longer specific to the noop renderer
    
  19. // implementation. They test the internal implementation we use in the React
    
  20. // test suite.
    
  21. describe('internal act()', () => {
    
  22.   it('can use act to flush effects', async () => {
    
  23.     function App(props) {
    
  24.       React.useEffect(props.callback);
    
  25.       return null;
    
  26.     }
    
  27. 
    
  28.     const calledLog = [];
    
  29.     await act(() => {
    
  30.       ReactNoop.render(
    
  31.         <App
    
  32.           callback={() => {
    
  33.             calledLog.push(calledLog.length);
    
  34.           }}
    
  35.         />,
    
  36.       );
    
  37.     });
    
  38.     await waitForAll([]);
    
  39.     expect(calledLog).toEqual([0]);
    
  40.   });
    
  41. 
    
  42.   it('should work with async/await', async () => {
    
  43.     function App() {
    
  44.       const [ctr, setCtr] = React.useState(0);
    
  45.       async function someAsyncFunction() {
    
  46.         Scheduler.log('stage 1');
    
  47.         await null;
    
  48.         Scheduler.log('stage 2');
    
  49.         await null;
    
  50.         setCtr(1);
    
  51.       }
    
  52.       React.useEffect(() => {
    
  53.         someAsyncFunction();
    
  54.       }, []);
    
  55.       return ctr;
    
  56.     }
    
  57.     await act(() => {
    
  58.       ReactNoop.render(<App />);
    
  59.     });
    
  60.     assertLog(['stage 1', 'stage 2']);
    
  61.     await waitForAll([]);
    
  62.     expect(ReactNoop).toMatchRenderedOutput('1');
    
  63.   });
    
  64. });