@@ -26,47 +26,55 @@ var ReactLegacyDescriptor = require('ReactLegacyDescriptor');
26
26
var ReactEmptyComponent = require ( 'ReactEmptyComponent' ) ;
27
27
28
28
/**
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.
32
31
*
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.
36
34
* @protected
37
35
*/
38
- function createInstance ( type , props ) {
36
+ function instantiateReactComponent ( descriptor ) {
37
+ var instance ;
38
+
39
39
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 ) {
41
50
// If this is a mocked class, we treat the legacy factory as if it was the
42
51
// class constructor for future proofing unit tests. Because this might
43
52
// be mocked as a legacy factory, we ignore any warnings triggerd by
44
53
// this temporary hack.
45
54
ReactLegacyDescriptor . _isLegacyCallWarningEnabled = false ;
46
- var instance ;
47
55
try {
48
- instance = new type . _mockedReactClassConstructor ( props ) ;
56
+ instance = new descriptor . type . _mockedReactClassConstructor (
57
+ descriptor . props
58
+ ) ;
49
59
} finally {
50
60
ReactLegacyDescriptor . _isLegacyCallWarningEnabled = true ;
51
61
}
52
62
53
63
// If the mock implementation was a legacy factory, then it returns a
54
64
// descriptor. We need to turn this into a real component instance.
55
65
if ( ReactDescriptor . isValidDescriptor ( instance ) ) {
56
- type = instance . type ;
57
- props = instance . props ;
58
- instance = new type ( props ) ;
66
+ instance = new instance . type ( instance . props ) ;
59
67
}
60
68
61
69
var render = instance . render ;
62
70
if ( ! render ) {
63
71
// For auto-mocked factories, the prototype isn't shimmed and therefore
64
72
// there is no render function on the instance. We replace the whole
65
73
// 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 ;
70
78
} else if ( render . _isMockFunction && ! render . _getMockImplementation ( ) ) {
71
79
// Auto-mocked components may have a prototype with a mocked render
72
80
// function. For those, we'll need to mock the result of the render
@@ -75,32 +83,13 @@ function createInstance(type, props) {
75
83
ReactEmptyComponent . getEmptyComponent
76
84
) ;
77
85
}
86
+ instance . construct ( descriptor ) ;
87
+ return instance ;
78
88
}
79
89
}
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
- }
102
90
103
- var instance = createInstance ( descriptor . type , descriptor . props ) ;
91
+ // Normal case for non-mocks
92
+ instance = new descriptor . type ( descriptor . props ) ;
104
93
105
94
if ( __DEV__ ) {
106
95
warning (
@@ -114,6 +103,7 @@ function instantiateReactComponent(descriptor) {
114
103
// This actually sets up the internal instance. This will become decoupled
115
104
// from the public instance in a future diff.
116
105
instance . construct ( descriptor ) ;
106
+
117
107
return instance ;
118
108
}
119
109
0 commit comments