1
- import React from 'react' ;
1
+ import React , { Component , PropTypes , createElement } from 'react' ;
2
2
3
- class BuiltEmitter extends React . Component {
4
- constructor ( props ) {
5
- super ( props )
6
-
7
- this . callWhenDone = done => done ( ) ;
3
+ class BuiltEmitter extends Component {
4
+ static propTypes = {
5
+ feature : PropTypes . func . isRequired
8
6
}
9
7
10
8
componentDidMount ( ) {
11
- this . callWhenDone ( ( ) => document . dispatchEvent ( new Event ( 'ReactFeatureDidMount' ) ) ) ;
9
+ const { feature } = this . props
10
+
11
+ // Class components must call this.props.onReady when they're ready for the test.
12
+ // We will assume functional components are ready immediately after mounting.
13
+ if ( ! Component . isPrototypeOf ( feature ) ) {
14
+ this . handleReady ( ) ;
15
+ }
12
16
}
13
17
14
- render ( ) {
15
- const feature = React . cloneElement ( React . Children . only ( this . props . children ) , {
16
- setCallWhenDone : done => {
17
- this . callWhenDone = done ;
18
- }
19
- } ) ;
18
+ handleReady ( ) {
19
+ document . dispatchEvent ( new Event ( 'ReactFeatureDidMount' ) ) ;
20
+ }
20
21
21
- return < div > { feature } </ div > ;
22
+ render ( ) {
23
+ const {
24
+ props : { feature } ,
25
+ handleReady
26
+ } = this ;
27
+ return (
28
+ < div >
29
+ { createElement ( feature , {
30
+ onReady : handleReady
31
+ } ) }
32
+ </ div >
33
+ ) ;
22
34
}
23
35
}
24
36
25
- class App extends React . Component {
37
+ class App extends Component {
26
38
constructor ( props ) {
27
39
super ( props ) ;
28
40
@@ -105,9 +117,7 @@ class App extends React.Component {
105
117
case 'unknown-ext-inclusion' :
106
118
require . ensure ( [ ] , ( ) => this . setFeature ( require ( './features/webpack/UnknownExtInclusion' ) . default ) ) ;
107
119
break ;
108
- default :
109
- this . setFeature ( null ) ;
110
- break ;
120
+ default : throw new Error ( 'Unknown feature!' ) ;
111
121
}
112
122
}
113
123
@@ -116,8 +126,11 @@ class App extends React.Component {
116
126
}
117
127
118
128
render ( ) {
119
- const Feature = this . state . feature ;
120
- return Feature ? < BuiltEmitter > < Feature /> </ BuiltEmitter > : null ;
129
+ const { feature } = this . state ;
130
+ if ( feature !== null ) {
131
+ return < BuiltEmitter feature = { feature } /> ;
132
+ }
133
+ return null ;
121
134
}
122
135
}
123
136
0 commit comments