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. let React;
    
  11. let DOMAct;
    
  12. let TestRenderer;
    
  13. let TestAct;
    
  14. 
    
  15. global.__DEV__ = process.env.NODE_ENV !== 'production';
    
  16. 
    
  17. expect.extend(require('../toWarnDev'));
    
  18. 
    
  19. describe('unmocked scheduler', () => {
    
  20.   beforeEach(() => {
    
  21.     jest.resetModules();
    
  22.     React = require('react');
    
  23.     DOMAct = require('react-dom/test-utils').act;
    
  24.     TestRenderer = require('react-test-renderer');
    
  25.     TestAct = TestRenderer.act;
    
  26.   });
    
  27. 
    
  28.   it('flushes work only outside the outermost act() corresponding to its own renderer', () => {
    
  29.     let log = [];
    
  30.     function Effecty() {
    
  31.       React.useEffect(() => {
    
  32.         log.push('called');
    
  33.       }, []);
    
  34.       return null;
    
  35.     }
    
  36.     // in legacy mode, this tests whether an act only flushes its own effects
    
  37.     TestAct(() => {
    
  38.       DOMAct(() => {
    
  39.         TestRenderer.create(<Effecty />);
    
  40.       });
    
  41.       expect(log).toEqual([]);
    
  42.     });
    
  43.     expect(log).toEqual(['called']);
    
  44. 
    
  45.     log = [];
    
  46.     // for doublechecking, we flip it inside out, and assert on the outermost
    
  47.     DOMAct(() => {
    
  48.       TestAct(() => {
    
  49.         TestRenderer.create(<Effecty />);
    
  50.       });
    
  51.       expect(log).toEqual([]);
    
  52.     });
    
  53.     expect(log).toEqual(['called']);
    
  54.   });
    
  55. });
    
  56. 
    
  57. describe('mocked scheduler', () => {
    
  58.   beforeEach(() => {
    
  59.     jest.resetModules();
    
  60.     jest.mock('scheduler', () =>
    
  61.       require.requireActual('scheduler/unstable_mock')
    
  62.     );
    
  63.     React = require('react');
    
  64.     DOMAct = require('react-dom/test-utils').act;
    
  65.     TestRenderer = require('react-test-renderer');
    
  66.     TestAct = TestRenderer.act;
    
  67.   });
    
  68. 
    
  69.   afterEach(() => {
    
  70.     jest.unmock('scheduler');
    
  71.   });
    
  72. 
    
  73.   it('flushes work only outside the outermost act()', () => {
    
  74.     let log = [];
    
  75.     function Effecty() {
    
  76.       React.useEffect(() => {
    
  77.         log.push('called');
    
  78.       }, []);
    
  79.       return null;
    
  80.     }
    
  81.     // with a mocked scheduler, this tests whether it flushes all work only on the outermost act
    
  82.     TestAct(() => {
    
  83.       DOMAct(() => {
    
  84.         TestRenderer.create(<Effecty />);
    
  85.       });
    
  86.       expect(log).toEqual([]);
    
  87.     });
    
  88.     expect(log).toEqual(['called']);
    
  89. 
    
  90.     log = [];
    
  91.     // for doublechecking, we flip it inside out, and assert on the outermost
    
  92.     DOMAct(() => {
    
  93.       TestAct(() => {
    
  94.         TestRenderer.create(<Effecty />);
    
  95.       });
    
  96.       expect(log).toEqual([]);
    
  97.     });
    
  98.     expect(log).toEqual(['called']);
    
  99.   });
    
  100. });