-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathReactNoopRendererAct-test.js
60 lines (55 loc) · 1.54 KB
/
ReactNoopRendererAct-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment node
*/
// sanity tests for ReactNoop.act()
jest.useRealTimers();
const React = require('react');
const ReactNoop = require('react-noop-renderer');
const Scheduler = require('scheduler');
describe('ReactNoop.act()', () => {
it('can use act to flush effects', async () => {
function App(props) {
React.useEffect(props.callback);
return null;
}
const calledLog = [];
ReactNoop.act(() => {
ReactNoop.render(
<App
callback={() => {
calledLog.push(calledLog.length);
}}
/>,
);
});
expect(Scheduler).toFlushWithoutYielding();
expect(calledLog).toEqual([0]);
});
it('should work with async/await', async () => {
function App() {
const [ctr, setCtr] = React.useState(0);
async function someAsyncFunction() {
Scheduler.unstable_yieldValue('stage 1');
await null;
Scheduler.unstable_yieldValue('stage 2');
await null;
setCtr(1);
}
React.useEffect(() => {
someAsyncFunction();
}, []);
return ctr;
}
await ReactNoop.act(async () => {
ReactNoop.render(<App />);
});
expect(Scheduler).toHaveYielded(['stage 1', 'stage 2']);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([{text: '1', hidden: false}]);
});
});