forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ReactDOMSuspensePlaceholder-test.internal.js
269 lines (235 loc) · 6.91 KB
/
ReactDOMSuspensePlaceholder-test.internal.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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 ReactFeatureFlags;
let React;
let ReactDOM;
let Suspense;
let ReactCache;
let TextResource;
describe('ReactDOMSuspensePlaceholder', () => {
let container;
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactDOM = require('react-dom');
ReactCache = require('react-cache');
Suspense = React.Suspense;
container = document.createElement('div');
document.body.appendChild(container);
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
return new Promise((resolve, reject) =>
setTimeout(() => {
resolve(text);
}, ms),
);
}, ([text, ms]) => text);
});
afterEach(() => {
document.body.removeChild(container);
});
function advanceTimers(ms) {
// Note: This advances Jest's virtual time but not React's. Use
// ReactNoop.expire for that.
if (typeof ms !== 'number') {
throw new Error('Must specify ms');
}
jest.advanceTimersByTime(ms);
// Wait until the end of the current tick
// We cannot use a timer since we're faking them
return Promise.resolve().then(() => {});
}
function Text(props) {
return props.text;
}
function AsyncText(props) {
const text = props.text;
TextResource.read([props.text, props.ms]);
return text;
}
it('hides and unhides timed out DOM elements', async () => {
let divs = [
React.createRef(null),
React.createRef(null),
React.createRef(null),
];
function App() {
return (
<Suspense maxDuration={500} fallback={<Text text="Loading..." />}>
<div ref={divs[0]}>
<Text text="A" />
</div>
<div ref={divs[1]}>
<AsyncText ms={1000} text="B" />
</div>
<div style={{display: 'block'}} ref={divs[2]}>
<Text text="C" />
</div>
</Suspense>
);
}
ReactDOM.render(<App />, container);
expect(divs[0].current.style.display).toEqual('none');
expect(divs[1].current.style.display).toEqual('none');
expect(divs[2].current.style.display).toEqual('none');
await advanceTimers(1000);
expect(divs[0].current.style.display).toEqual('');
expect(divs[1].current.style.display).toEqual('');
// This div's display was set with a prop.
expect(divs[2].current.style.display).toEqual('block');
});
it('hides and unhides timed out text nodes', async () => {
function App() {
return (
<Suspense maxDuration={500} fallback={<Text text="Loading..." />}>
<Text text="A" />
<AsyncText ms={1000} text="B" />
<Text text="C" />
</Suspense>
);
}
ReactDOM.render(<App />, container);
expect(container.textContent).toEqual('Loading...');
await advanceTimers(1000);
expect(container.textContent).toEqual('ABC');
});
it(
'outside concurrent mode, re-hides children if their display is updated ' +
'but the boundary is still showing the fallback',
async () => {
const {useState} = React;
let setIsVisible;
function Sibling({children}) {
const [isVisible, _setIsVisible] = useState(false);
setIsVisible = _setIsVisible;
return (
<span style={{display: isVisible ? 'inline' : 'none'}}>
{children}
</span>
);
}
function App() {
return (
<Suspense maxDuration={500} fallback={<Text text="Loading..." />}>
<Sibling>Sibling</Sibling>
<span>
<AsyncText ms={1000} text="Async" />
</span>
</Suspense>
);
}
ReactDOM.render(<App />, container);
expect(container.innerHTML).toEqual(
'<span style="display: none;">Sibling</span><span style="display: none;"></span>Loading...',
);
setIsVisible(true);
expect(container.innerHTML).toEqual(
'<span style="display: none;">Sibling</span><span style="display: none;"></span>Loading...',
);
await advanceTimers(1000);
expect(container.innerHTML).toEqual(
'<span style="display: inline;">Sibling</span><span style="">Async</span>',
);
},
);
// Regression test for https://github.com/facebook/react/issues/14188
it('can call findDOMNode() in a suspended component commit phase', async () => {
const log = [];
const Lazy = React.lazy(
() =>
new Promise(resolve =>
resolve({
default() {
return 'lazy';
},
}),
),
);
class Child extends React.Component {
componentDidMount() {
log.push('cDM ' + this.props.id);
ReactDOM.findDOMNode(this);
}
componentDidUpdate() {
log.push('cDU ' + this.props.id);
ReactDOM.findDOMNode(this);
}
render() {
return 'child';
}
}
const buttonRef = React.createRef();
class App extends React.Component {
state = {
suspend: false,
};
handleClick = () => {
this.setState({suspend: true});
};
render() {
return (
<React.Suspense fallback="Loading">
<Child id="first" />
<button ref={buttonRef} onClick={this.handleClick}>
Suspend
</button>
<Child id="second" />
{this.state.suspend && <Lazy />}
</React.Suspense>
);
}
}
ReactDOM.render(<App />, container);
expect(log).toEqual(['cDM first', 'cDM second']);
log.length = 0;
buttonRef.current.dispatchEvent(new MouseEvent('click', {bubbles: true}));
await Lazy;
expect(log).toEqual(['cDU first', 'cDU second']);
});
// Regression test for https://github.com/facebook/react/issues/14188
it('can call findDOMNode() in a suspended component commit phase (#2)', () => {
let suspendOnce = Promise.resolve();
function Suspend() {
if (suspendOnce) {
let promise = suspendOnce;
suspendOnce = null;
throw promise;
}
return null;
}
const log = [];
class Child extends React.Component {
componentDidMount() {
log.push('cDM');
ReactDOM.findDOMNode(this);
}
componentDidUpdate() {
log.push('cDU');
ReactDOM.findDOMNode(this);
}
render() {
return null;
}
}
function App() {
return (
<Suspense fallback="Loading">
<Suspend />
<Child />
</Suspense>
);
}
ReactDOM.render(<App />, container);
expect(log).toEqual(['cDM']);
ReactDOM.render(<App />, container);
expect(log).toEqual(['cDM', 'cDU']);
});
});