15
15
16
16
var ReactFiberReconciler = require ( 'ReactFiberReconciler' ) ;
17
17
var emptyObject = require ( 'emptyObject' ) ;
18
- var invariant = require ( 'invariant' ) ;
19
18
20
- import type { ReactElement } from 'ReactElementType' ;
21
- import type { ReactInstance } from 'ReactInstanceType' ;
19
+ import type { TestRendererOptions } from 'ReactTestMount' ;
22
20
23
21
type ReactTestRendererJSON = {
24
22
type : string ,
25
23
props : { [ propName : string ] : string } ,
26
- children : null | Array < string | ReactTestRendererJSON > ,
24
+ children : null | Array < string | number | ReactTestRendererJSON > ,
27
25
$$typeof ? : any
28
26
}
29
27
30
28
let instanceCounter = 0 ;
31
29
32
30
class TestContainer {
31
+ rootID : string ;
32
+ createNodeMock : Function ;
33
+
33
34
constructor ( rootID , createNodeMock ) {
34
35
this . rootID = rootID ;
35
36
this . createNodeMock = createNodeMock ;
@@ -41,6 +42,13 @@ class TestContainer {
41
42
}
42
43
43
44
class TestComponent {
45
+ id : number ;
46
+ props : Object ;
47
+ type : string ;
48
+ rootContainerInstance : TestContainer ;
49
+ children : Array < Instance | TextInstance > ;
50
+ $$typeof : Symbol ;
51
+
44
52
constructor ( type , props , rootContainerInstance ) {
45
53
this . id = instanceCounter ++ ;
46
54
this . type = type ;
@@ -93,7 +101,7 @@ class TestComponent {
93
101
// eslint-disable ignore the children
94
102
const { children, ...props } = this . props ;
95
103
// eslint-enable
96
- const json = {
104
+ const json : ReactTestRendererJSON = {
97
105
type : this . type ,
98
106
props : props ,
99
107
children : null ,
@@ -108,7 +116,7 @@ class TestComponent {
108
116
if ( typeof child . toJSON === 'function' ) {
109
117
childrenJSON . push ( child . toJSON ( ) ) ;
110
118
} else if ( typeof child . text !== 'undefined' ) {
111
- childrenJSON . push ( isNaN ( + child . text ) ? child . text : + child . text ) ;
119
+ childrenJSON . push ( ) ;
112
120
}
113
121
} ) ;
114
122
json . children = childrenJSON . length ? childrenJSON : null ;
@@ -117,17 +125,22 @@ class TestComponent {
117
125
}
118
126
}
119
127
128
+ type Container = TestContainer ;
129
+ type Props = Object ;
120
130
type Instance = TestComponent ;
131
+ type TextInstance = {
132
+ text : string | number ,
133
+ id : number ,
134
+ rootContainerInstance : Container ,
135
+ toJSON ( ) : string | number ,
136
+ } ;
121
137
122
138
var TestRenderer = ReactFiberReconciler ( {
123
- getRootHostContext ( rootContainerInstance : Container ) : HostContext {
139
+ getRootHostContext ( ) {
124
140
return emptyObject ;
125
141
} ,
126
142
127
- getChildHostContext (
128
- parentHostContext : HostContext ,
129
- type : string ,
130
- ) : HostContext {
143
+ getChildHostContext ( ) {
131
144
return emptyObject ;
132
145
} ,
133
146
@@ -143,7 +156,7 @@ var TestRenderer = ReactFiberReconciler({
143
156
type : string ,
144
157
props : Props ,
145
158
rootContainerInstance : Container ,
146
- hostContext : HostContext ,
159
+ hostContext : Object ,
147
160
internalInstanceHandle : Object ,
148
161
) : Instance {
149
162
return new TestComponent ( type , props , rootContainerInstance ) ;
@@ -158,17 +171,18 @@ var TestRenderer = ReactFiberReconciler({
158
171
type : string ,
159
172
props : Props ,
160
173
rootContainerInstance : Container ,
161
- ) : void {
174
+ ) : boolean {
162
175
// console.log('finalizeInitialChildren');
163
176
// setInitialProperties(testElement, type, props, rootContainerInstance);
177
+ return false ;
164
178
} ,
165
179
166
180
prepareUpdate (
167
181
testElement : Instance ,
168
182
type : string ,
169
183
oldProps : Props ,
170
184
newProps : Props ,
171
- hostContext : HostContext ,
185
+ hostContext : Object ,
172
186
) : boolean {
173
187
return true ;
174
188
} ,
@@ -184,6 +198,16 @@ var TestRenderer = ReactFiberReconciler({
184
198
instance . update ( type , newProps ) ;
185
199
} ,
186
200
201
+ commitMount (
202
+ instance : Instance ,
203
+ type : string ,
204
+ newProps : Props ,
205
+ rootContainerInstance : Object ,
206
+ internalInstanceHandle : Object
207
+ ) : void {
208
+ // Noop
209
+ } ,
210
+
187
211
shouldSetTextContent ( props : Props ) : boolean {
188
212
return (
189
213
typeof props . children === 'string' ||
@@ -196,10 +220,15 @@ var TestRenderer = ReactFiberReconciler({
196
220
createTextInstance (
197
221
text : string ,
198
222
rootContainerInstance : Container ,
199
- hostContext : HostContext ,
223
+ hostContext : Object ,
200
224
internalInstanceHandle : Object
201
225
) : TextInstance {
202
- var inst = { text : text , id : instanceCounter ++ } ;
226
+ var inst = {
227
+ text : text ,
228
+ id : instanceCounter ++ ,
229
+ rootContainerInstance,
230
+ toJSON : ( ) => isNaN ( + inst . text ) ? inst . text : + inst . text ,
231
+ } ;
203
232
// Hide from unit tests
204
233
Object . defineProperty ( inst , 'id' , { value : inst . id , enumerable : false } ) ;
205
234
return inst ;
@@ -245,33 +274,47 @@ var defaultTestOptions = {
245
274
} ;
246
275
247
276
var ReactTestFiberRenderer = {
248
- create ( element , options ) {
277
+ create ( element : ReactElement < any > , options : TestRendererOptions ) {
249
278
var createNodeMock = defaultTestOptions . createNodeMock ;
250
279
if ( options && typeof options . createNodeMock === 'function' ) {
251
280
createNodeMock = options . createNodeMock ;
252
281
}
253
282
var container = new TestContainer ( '<default>' , createNodeMock ) ;
254
283
var root = TestRenderer . createContainer ( container ) ;
255
- TestRenderer . updateContainer ( element , root , null , null ) ;
284
+ if ( root ) {
285
+ TestRenderer . updateContainer ( element , root , null , null ) ;
286
+ }
256
287
return {
257
288
toJSON ( ) {
289
+ if ( root == null ) {
290
+ return null ;
291
+ }
258
292
const hostInstance = TestRenderer . findHostInstance ( root ) ;
259
293
if ( hostInstance === null ) {
260
294
return hostInstance ;
261
295
}
262
296
return hostInstance . toJSON ( ) ;
263
297
} ,
264
298
265
- update ( newElement ) {
299
+ update ( newElement : ReactElement < any > ) {
300
+ if ( root == null ) {
301
+ return ;
302
+ }
266
303
TestRenderer . updateContainer ( newElement , root , null , null ) ;
267
304
} ,
268
305
unmount ( ) {
306
+ if ( root == null ) {
307
+ return ;
308
+ }
269
309
TestRenderer . updateContainer ( null , root , null , ( ) => {
270
310
container = null ;
271
311
root = null ;
272
312
} ) ;
273
313
} ,
274
314
getInstance ( ) {
315
+ if ( root == null ) {
316
+ return null ;
317
+ }
275
318
return TestRenderer . getPublicRootInstance ( root ) ;
276
319
} ,
277
320
} ;
0 commit comments