-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
ReactDOMHooks-test.js
154 lines (125 loc) · 4.18 KB
/
ReactDOMHooks-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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* 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.
*
* @emails react-core
*/
'use strict';
let React;
let ReactDOM;
let act;
let Scheduler;
describe('ReactDOMHooks', () => {
let container;
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
act = require('react-dom/test-utils').act;
Scheduler = require('scheduler');
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
});
it('can ReactDOM.render() from useEffect', () => {
let container2 = document.createElement('div');
let container3 = document.createElement('div');
function Example1({n}) {
React.useEffect(() => {
ReactDOM.render(<Example2 n={n} />, container2);
});
return 1 * n;
}
function Example2({n}) {
React.useEffect(() => {
ReactDOM.render(<Example3 n={n} />, container3);
});
return 2 * n;
}
function Example3({n}) {
return 3 * n;
}
// we explicitly catch the missing act() warnings
// to simulate this tricky repro
// todo - is this ok?
expect(() => {
ReactDOM.render(<Example1 n={1} />, container);
expect(container.textContent).toBe('1');
expect(container2.textContent).toBe('');
expect(container3.textContent).toBe('');
Scheduler.flushAll();
expect(container.textContent).toBe('1');
expect(container2.textContent).toBe('2');
expect(container3.textContent).toBe('3');
ReactDOM.render(<Example1 n={2} />, container);
expect(container.textContent).toBe('2');
expect(container2.textContent).toBe('2'); // Not flushed yet
expect(container3.textContent).toBe('3'); // Not flushed yet
Scheduler.flushAll();
expect(container.textContent).toBe('2');
expect(container2.textContent).toBe('4');
expect(container3.textContent).toBe('6');
}).toWarnDev([
'Your test just caused an effect from Example1',
'Your test just caused an effect from Example2',
'Your test just caused an effect from Example1',
'Your test just caused an effect from Example2',
]);
});
it('should not bail out when an update is scheduled from within an event handler', () => {
const {createRef, useCallback, useState} = React;
const Example = ({inputRef, labelRef}) => {
const [text, setText] = useState('');
const handleInput = useCallback(event => {
setText(event.target.value);
});
return (
<React.Fragment>
<input ref={inputRef} onInput={handleInput} />
<label ref={labelRef}>{text}</label>
</React.Fragment>
);
};
const inputRef = createRef();
const labelRef = createRef();
ReactDOM.render(
<Example inputRef={inputRef} labelRef={labelRef} />,
container,
);
inputRef.current.value = 'abc';
inputRef.current.dispatchEvent(
new Event('input', {bubbles: true, cancelable: true}),
);
expect(labelRef.current.innerHTML).toBe('abc');
});
it('should not bail out when an update is scheduled from within an event handler in Concurrent Mode', () => {
const {createRef, useCallback, useState} = React;
const Example = ({inputRef, labelRef}) => {
const [text, setText] = useState('');
const handleInput = useCallback(event => {
setText(event.target.value);
});
return (
<React.Fragment>
<input ref={inputRef} onInput={handleInput} />
<label ref={labelRef}>{text}</label>
</React.Fragment>
);
};
const inputRef = createRef();
const labelRef = createRef();
const root = ReactDOM.unstable_createRoot(container);
root.render(<Example inputRef={inputRef} labelRef={labelRef} />);
Scheduler.flushAll();
inputRef.current.value = 'abc';
inputRef.current.dispatchEvent(
new Event('input', {bubbles: true, cancelable: true}),
);
Scheduler.flushAll();
expect(labelRef.current.innerHTML).toBe('abc');
});
});