-
Notifications
You must be signed in to change notification settings - Fork 46.9k
/
ReactShallowRendererEntry.js
268 lines (219 loc) · 7.02 KB
/
ReactShallowRendererEntry.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
/**
* 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.
*
* @providesModule ReactShallowRendererEntry
* @preventMunge
*/
'use strict';
const checkPropTypes = require('prop-types/checkPropTypes');
const React = require('react');
const emptyObject = require('fbjs/lib/emptyObject');
const invariant = require('fbjs/lib/invariant');
const describeComponentFrame = require('describeComponentFrame');
const getComponentName = require('getComponentName');
class ReactShallowRenderer {
static createRenderer = function() {
return new ReactShallowRenderer();
};
constructor() {
this._context = null;
this._element = null;
this._instance = null;
this._newState = null;
this._rendered = null;
this._rendering = false;
this._updater = new Updater(this);
}
getMountedInstance() {
return this._instance;
}
getRenderOutput() {
return this._rendered;
}
render(element, context = emptyObject) {
invariant(
React.isValidElement(element),
'ReactShallowRenderer render(): Invalid component element.%s',
typeof element === 'function'
? ' Instead of passing a component class, make sure to instantiate ' +
'it by passing it to React.createElement.'
: '',
);
invariant(
typeof element.type !== 'string',
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, not primitives (%s). Instead of calling `.render(el)` and ' +
'inspecting the rendered output, look at `el.props` directly instead.',
element.type,
);
if (this._rendering) {
return;
}
this._rendering = true;
this._element = element;
this._context = context;
if (this._instance) {
this._updateClassComponent(element.props, context);
} else {
if (shouldConstruct(element.type)) {
this._instance = new element.type(
element.props,
context,
this._updater,
);
if (element.type.hasOwnProperty('contextTypes')) {
currentlyValidatingElement = element;
checkPropTypes(
element.type.contextTypes,
context,
'context',
getName(element.type, this._instance),
getStackAddendum,
);
currentlyValidatingElement = null;
}
this._mountClassComponent(element.props, context);
} else {
this._rendered = element.type(element.props, context);
}
}
this._rendering = false;
return this.getRenderOutput();
}
unmount() {
if (this._instance) {
if (typeof this._instance.componentWillUnmount === 'function') {
this._instance.componentWillUnmount();
}
}
this._context = null;
this._element = null;
this._newState = null;
this._rendered = null;
this._instance = null;
}
_mountClassComponent(props, context) {
this._instance.context = context;
this._instance.props = props;
this._instance.state = this._instance.state || emptyObject;
this._instance.updater = this._updater;
if (typeof this._instance.componentWillMount === 'function') {
const beforeState = this._newState;
this._instance.componentWillMount();
// setState may have been called during cWM
if (beforeState !== this._newState) {
this._instance.state = this._newState || emptyObject;
}
}
this._rendered = this._instance.render();
// Intentionally do not call componentDidMount()
// because DOM refs are not available.
}
_updateClassComponent(props, context) {
const oldProps = this._instance.props;
if (
oldProps !== props &&
typeof this._instance.componentWillReceiveProps === 'function'
) {
this._instance.componentWillReceiveProps(props, context);
}
// Read state after cWRP in case it calls setState
// Fallback to previous instance state to support rendering React.cloneElement()
const state = this._newState || this._instance.state || emptyObject;
if (typeof this._instance.shouldComponentUpdate === 'function') {
if (
this._instance.shouldComponentUpdate(props, state, context) === false
) {
this._instance.context = context;
this._instance.props = props;
this._instance.state = state;
return;
}
}
if (typeof this._instance.componentWillUpdate === 'function') {
this._instance.componentWillUpdate(props, state, context);
}
this._instance.context = context;
this._instance.props = props;
this._instance.state = state;
this._rendered = this._instance.render();
// Intentionally do not call componentDidUpdate()
// because DOM refs are not available.
}
}
class Updater {
constructor(renderer) {
this._renderer = renderer;
}
isMounted(publicInstance) {
return !!this._renderer._element;
}
enqueueForceUpdate(publicInstance, callback, callerName) {
this._renderer.render(this._renderer._element, this._renderer._context);
if (typeof callback === 'function') {
callback.call(publicInstance);
}
}
enqueueReplaceState(publicInstance, completeState, callback, callerName) {
this._renderer._newState = completeState;
this._renderer.render(this._renderer._element, this._renderer._context);
if (typeof callback === 'function') {
callback.call(publicInstance);
}
}
enqueueSetState(publicInstance, partialState, callback, callerName) {
if (typeof partialState === 'function') {
partialState = partialState(publicInstance.state, publicInstance.props);
}
this._renderer._newState = {
...publicInstance.state,
...partialState,
};
this._renderer.render(this._renderer._element, this._renderer._context);
if (typeof callback === 'function') {
callback.call(publicInstance);
}
}
}
var currentlyValidatingElement = null;
function getDisplayName(element) {
if (element == null) {
return '#empty';
} else if (typeof element === 'string' || typeof element === 'number') {
return '#text';
} else if (typeof element.type === 'string') {
return element.type;
} else {
return element.type.displayName || element.type.name || 'Unknown';
}
}
function getStackAddendum() {
var stack = '';
if (currentlyValidatingElement) {
var name = getDisplayName(currentlyValidatingElement);
var owner = currentlyValidatingElement._owner;
stack += describeComponentFrame(
name,
currentlyValidatingElement._source,
owner && getComponentName(owner),
);
}
return stack;
}
function getName(type, instance) {
var constructor = instance && instance.constructor;
return (
type.displayName ||
(constructor && constructor.displayName) ||
type.name ||
(constructor && constructor.name) ||
null
);
}
function shouldConstruct(Component) {
return !!(Component.prototype && Component.prototype.isReactComponent);
}
module.exports = ReactShallowRenderer;