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