|  | 
|  | 1 | +/** | 
|  | 2 | + * Copyright (c) Facebook, Inc. and its 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 | +let React; | 
|  | 13 | +let ReactNoop; | 
|  | 14 | +let act; | 
|  | 15 | +let DiscreteEventPriority; | 
|  | 16 | + | 
|  | 17 | +describe('isomorphic act()', () => { | 
|  | 18 | +  beforeEach(() => { | 
|  | 19 | +    React = require('react'); | 
|  | 20 | +    ReactNoop = require('react-noop-renderer'); | 
|  | 21 | +    DiscreteEventPriority = require('react-reconciler/constants') | 
|  | 22 | +      .DiscreteEventPriority; | 
|  | 23 | +    act = React.unstable_act; | 
|  | 24 | +  }); | 
|  | 25 | + | 
|  | 26 | +  // @gate __DEV__ | 
|  | 27 | +  test('bypasses queueMicrotask', async () => { | 
|  | 28 | +    const root = ReactNoop.createRoot(); | 
|  | 29 | + | 
|  | 30 | +    // First test what happens without wrapping in act. This update would | 
|  | 31 | +    // normally be queued in a microtask. | 
|  | 32 | +    ReactNoop.unstable_runWithPriority(DiscreteEventPriority, () => { | 
|  | 33 | +      root.render('A'); | 
|  | 34 | +    }); | 
|  | 35 | +    // Nothing has rendered yet | 
|  | 36 | +    expect(root).toMatchRenderedOutput(null); | 
|  | 37 | +    // Flush the microtasks by awaiting | 
|  | 38 | +    await null; | 
|  | 39 | +    expect(root).toMatchRenderedOutput('A'); | 
|  | 40 | + | 
|  | 41 | +    // Now do the same thing but wrap the update with `act`. No | 
|  | 42 | +    // `await` necessary. | 
|  | 43 | +    act(() => { | 
|  | 44 | +      ReactNoop.unstable_runWithPriority(DiscreteEventPriority, () => { | 
|  | 45 | +        root.render('B'); | 
|  | 46 | +      }); | 
|  | 47 | +    }); | 
|  | 48 | +    expect(root).toMatchRenderedOutput('B'); | 
|  | 49 | +  }); | 
|  | 50 | +}); | 
0 commit comments