Skip to content

Commit 3c6c437

Browse files
sebmarkbagezpao
authored andcommitted
Don't transfer props to mocked empty component
We currently automatically render empty components in place of mocks. However, we were accidentally transferring the props from the mocked descriptor to the empty component placeholder. Even children. This change just cleans that up and should only affect unit tests.
1 parent f5a0d66 commit 3c6c437

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed

src/core/instantiateReactComponent.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,55 @@ var ReactLegacyDescriptor = require('ReactLegacyDescriptor');
2626
var ReactEmptyComponent = require('ReactEmptyComponent');
2727

2828
/**
29-
* Given `type` and `props` invoke the type function and create an instance from
30-
* it. For the normal case it just runs the constructor. For mocks we need to
31-
* invoke the convenience constructor and resolve that into an instance.
29+
* Given a `componentDescriptor` create an instance that will actually be
30+
* mounted.
3231
*
33-
* @param {function} type
34-
* @param {*} props
35-
* @return {object} A new instance of `type`.
32+
* @param {object} descriptor
33+
* @return {object} A new instance of componentDescriptor's constructor.
3634
* @protected
3735
*/
38-
function createInstance(type, props) {
36+
function instantiateReactComponent(descriptor) {
37+
var instance;
38+
3939
if (__DEV__) {
40-
if (type._mockedReactClassConstructor) {
40+
warning(
41+
descriptor && (typeof descriptor.type === 'function' ||
42+
typeof descriptor.type === 'string'),
43+
'Only functions or strings can be mounted as React components.'
44+
// Not really strings yet, but as soon as I solve the cyclic dep, they
45+
// will be allowed here.
46+
);
47+
48+
// Resolve mock instances
49+
if (descriptor.type._mockedReactClassConstructor) {
4150
// If this is a mocked class, we treat the legacy factory as if it was the
4251
// class constructor for future proofing unit tests. Because this might
4352
// be mocked as a legacy factory, we ignore any warnings triggerd by
4453
// this temporary hack.
4554
ReactLegacyDescriptor._isLegacyCallWarningEnabled = false;
46-
var instance;
4755
try {
48-
instance = new type._mockedReactClassConstructor(props);
56+
instance = new descriptor.type._mockedReactClassConstructor(
57+
descriptor.props
58+
);
4959
} finally {
5060
ReactLegacyDescriptor._isLegacyCallWarningEnabled = true;
5161
}
5262

5363
// If the mock implementation was a legacy factory, then it returns a
5464
// descriptor. We need to turn this into a real component instance.
5565
if (ReactDescriptor.isValidDescriptor(instance)) {
56-
type = instance.type;
57-
props = instance.props;
58-
instance = new type(props);
66+
instance = new instance.type(instance.props);
5967
}
6068

6169
var render = instance.render;
6270
if (!render) {
6371
// For auto-mocked factories, the prototype isn't shimmed and therefore
6472
// there is no render function on the instance. We replace the whole
6573
// component with an empty component instance instead.
66-
var descriptor = ReactEmptyComponent.getEmptyComponent();
67-
type = descriptor.type;
68-
props = descriptor.props;
69-
instance = new type(props);
74+
descriptor = ReactEmptyComponent.getEmptyComponent();
75+
instance = new descriptor.type(descriptor.props);
76+
instance.construct(descriptor);
77+
return instance;
7078
} else if (render._isMockFunction && !render._getMockImplementation()) {
7179
// Auto-mocked components may have a prototype with a mocked render
7280
// function. For those, we'll need to mock the result of the render
@@ -75,32 +83,13 @@ function createInstance(type, props) {
7583
ReactEmptyComponent.getEmptyComponent
7684
);
7785
}
86+
instance.construct(descriptor);
87+
return instance;
7888
}
7989
}
80-
// Normal case for non-mocks
81-
return new type(props);
82-
}
83-
84-
/**
85-
* Given a `componentDescriptor` create an instance that will actually be
86-
* mounted.
87-
*
88-
* @param {object} descriptor
89-
* @return {object} A new instance of componentDescriptor's constructor.
90-
* @protected
91-
*/
92-
function instantiateReactComponent(descriptor) {
93-
if (__DEV__) {
94-
warning(
95-
descriptor && (typeof descriptor.type === 'function' ||
96-
typeof descriptor.type === 'string'),
97-
'Only functions or strings can be mounted as React components.'
98-
// Not really strings yet, but as soon as I solve the cyclic dep, they
99-
// will be allowed here.
100-
);
101-
}
10290

103-
var instance = createInstance(descriptor.type, descriptor.props);
91+
// Normal case for non-mocks
92+
instance = new descriptor.type(descriptor.props);
10493

10594
if (__DEV__) {
10695
warning(
@@ -114,6 +103,7 @@ function instantiateReactComponent(descriptor) {
114103
// This actually sets up the internal instance. This will become decoupled
115104
// from the public instance in a future diff.
116105
instance.construct(descriptor);
106+
117107
return instance;
118108
}
119109

0 commit comments

Comments
 (0)