/
ReactEmptyComponent-test.js
339 lines (284 loc) · 8.87 KB
/
ReactEmptyComponent-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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* 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 ReactTestUtils;
let TogglingComponent;
let log;
describe('ReactEmptyComponent', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
log = jest.fn();
TogglingComponent = class extends React.Component {
state = {component: this.props.firstComponent};
componentDidMount() {
log(ReactDOM.findDOMNode(this));
this.setState({component: this.props.secondComponent});
}
componentDidUpdate() {
log(ReactDOM.findDOMNode(this));
}
render() {
const Component = this.state.component;
return Component ? <Component /> : null;
}
};
});
it('should not produce child DOM nodes for null and false', () => {
class Component1 extends React.Component {
render() {
return null;
}
}
class Component2 extends React.Component {
render() {
return false;
}
}
const container1 = document.createElement('div');
ReactDOM.render(<Component1 />, container1);
expect(container1.children.length).toBe(0);
const container2 = document.createElement('div');
ReactDOM.render(<Component2 />, container2);
expect(container2.children.length).toBe(0);
});
it('should still throw when rendering to undefined', () => {
class Component extends React.Component {
render() {}
}
expect(function() {
ReactTestUtils.renderIntoDocument(<Component />);
}).toThrowError(
'Component(...): Nothing was returned from render. This usually means a return statement is missing. ' +
'Or, to render nothing, return null.',
);
});
it('should be able to switch between rendering null and a normal tag', () => {
const instance1 = (
<TogglingComponent firstComponent={null} secondComponent={'div'} />
);
const instance2 = (
<TogglingComponent firstComponent={'div'} secondComponent={null} />
);
ReactTestUtils.renderIntoDocument(instance1);
ReactTestUtils.renderIntoDocument(instance2);
expect(log).toHaveBeenCalledTimes(4);
expect(log).toHaveBeenNthCalledWith(1, null);
expect(log).toHaveBeenNthCalledWith(
2,
expect.objectContaining({tagName: 'DIV'}),
);
expect(log).toHaveBeenNthCalledWith(
3,
expect.objectContaining({tagName: 'DIV'}),
);
expect(log).toHaveBeenNthCalledWith(4, null);
});
it('should be able to switch in a list of children', () => {
const instance1 = (
<TogglingComponent firstComponent={null} secondComponent={'div'} />
);
ReactTestUtils.renderIntoDocument(
<div>
{instance1}
{instance1}
{instance1}
</div>,
);
expect(log).toHaveBeenCalledTimes(6);
expect(log).toHaveBeenNthCalledWith(1, null);
expect(log).toHaveBeenNthCalledWith(2, null);
expect(log).toHaveBeenNthCalledWith(3, null);
expect(log).toHaveBeenNthCalledWith(
4,
expect.objectContaining({tagName: 'DIV'}),
);
expect(log).toHaveBeenNthCalledWith(
5,
expect.objectContaining({tagName: 'DIV'}),
);
expect(log).toHaveBeenNthCalledWith(
6,
expect.objectContaining({tagName: 'DIV'}),
);
});
it('should distinguish between a script placeholder and an actual script tag', () => {
const instance1 = (
<TogglingComponent firstComponent={null} secondComponent={'script'} />
);
const instance2 = (
<TogglingComponent firstComponent={'script'} secondComponent={null} />
);
expect(function() {
ReactTestUtils.renderIntoDocument(instance1);
}).not.toThrow();
expect(function() {
ReactTestUtils.renderIntoDocument(instance2);
}).not.toThrow();
expect(log).toHaveBeenCalledTimes(4);
expect(log).toHaveBeenNthCalledWith(1, null);
expect(log).toHaveBeenNthCalledWith(
2,
expect.objectContaining({tagName: 'SCRIPT'}),
);
expect(log).toHaveBeenNthCalledWith(
3,
expect.objectContaining({tagName: 'SCRIPT'}),
);
expect(log).toHaveBeenNthCalledWith(4, null);
});
it(
'should have findDOMNode return null when multiple layers of composite ' +
'components render to the same null placeholder',
() => {
class GrandChild extends React.Component {
render() {
return null;
}
}
class Child extends React.Component {
render() {
return <GrandChild />;
}
}
const instance1 = (
<TogglingComponent firstComponent={'div'} secondComponent={Child} />
);
const instance2 = (
<TogglingComponent firstComponent={Child} secondComponent={'div'} />
);
expect(function() {
ReactTestUtils.renderIntoDocument(instance1);
}).not.toThrow();
expect(function() {
ReactTestUtils.renderIntoDocument(instance2);
}).not.toThrow();
expect(log).toHaveBeenCalledTimes(4);
expect(log).toHaveBeenNthCalledWith(
1,
expect.objectContaining({tagName: 'DIV'}),
);
expect(log).toHaveBeenNthCalledWith(2, null);
expect(log).toHaveBeenNthCalledWith(3, null);
expect(log).toHaveBeenNthCalledWith(
4,
expect.objectContaining({tagName: 'DIV'}),
);
},
);
it('works when switching components', () => {
let assertions = 0;
class Inner extends React.Component {
render() {
return <span />;
}
componentDidMount() {
// Make sure the DOM node resolves properly even if we're replacing a
// `null` component
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
assertions++;
}
componentWillUnmount() {
// Even though we're getting replaced by `null`, we haven't been
// replaced yet!
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
assertions++;
}
}
class Wrapper extends React.Component {
render() {
return this.props.showInner ? <Inner /> : null;
}
}
const el = document.createElement('div');
let component;
// Render the <Inner /> component...
component = ReactDOM.render(<Wrapper showInner={true} />, el);
expect(ReactDOM.findDOMNode(component)).not.toBe(null);
// Switch to null...
component = ReactDOM.render(<Wrapper showInner={false} />, el);
expect(ReactDOM.findDOMNode(component)).toBe(null);
// ...then switch back.
component = ReactDOM.render(<Wrapper showInner={true} />, el);
expect(ReactDOM.findDOMNode(component)).not.toBe(null);
expect(assertions).toBe(3);
});
it('can render null at the top level', () => {
const div = document.createElement('div');
ReactDOM.render(null, div);
expect(div.innerHTML).toBe('');
});
it('does not break when updating during mount', () => {
class Child extends React.Component {
componentDidMount() {
if (this.props.onMount) {
this.props.onMount();
}
}
render() {
if (!this.props.visible) {
return null;
}
return <div>hello world</div>;
}
}
class Parent extends React.Component {
update = () => {
this.forceUpdate();
};
render() {
return (
<div>
<Child key="1" visible={false} />
<Child key="0" visible={true} onMount={this.update} />
<Child key="2" visible={false} />
</div>
);
}
}
expect(function() {
ReactTestUtils.renderIntoDocument(<Parent />);
}).not.toThrow();
});
it('preserves the dom node during updates', () => {
class Empty extends React.Component {
render() {
return null;
}
}
const container = document.createElement('div');
ReactDOM.render(<Empty />, container);
const noscript1 = container.firstChild;
expect(noscript1).toBe(null);
// This update shouldn't create a DOM node
ReactDOM.render(<Empty />, container);
const noscript2 = container.firstChild;
expect(noscript2).toBe(null);
});
it('should warn about React.forwardRef that returns undefined', () => {
const Empty = () => {};
const EmptyForwardRef = React.forwardRef(Empty);
expect(() => {
ReactTestUtils.renderIntoDocument(<EmptyForwardRef />);
}).toThrowError(
'ForwardRef(Empty)(...): Nothing was returned from render.',
);
});
it('should warn about React.memo that returns undefined', () => {
const Empty = () => {};
const EmptyMemo = React.memo(Empty);
expect(() => {
ReactTestUtils.renderIntoDocument(<EmptyMemo />);
}).toThrowError('Empty(...): Nothing was returned from render.');
});
});