@@ -27,34 +27,96 @@ type ReactTestRendererJSON = {
27
27
}
28
28
29
29
let instanceCounter = 0 ;
30
- var createElement = ( type , rawProps , rootContainerInstance ) => {
31
- const { children, ...props } = rawProps ;
32
- var inst = {
33
- id : instanceCounter ++ ,
34
- type : type ,
35
- children : typeof children === 'undefined' ? null : Array . isArray ( children ) ? children : [ children ] ,
36
- props : props ,
37
- } ;
38
- // Hide from unit tests
39
- Object . defineProperty ( inst , 'id' , { value : inst . id , enumerable : false } ) ;
40
- Object . defineProperty ( inst , '$$typeof' , {
41
- value : Symbol . for ( 'react.test.json' ) ,
42
- } ) ;
43
- // todo: something like this?
44
- // const mockInst = rootContainerInstance.createNodeMock(inst);
45
- return inst ;
46
- } ;
47
30
48
- var setInitialProperties = ( ) => {
49
- throw new Error ( 'TODO: setInitialProperties' ) ;
50
- } ;
31
+ class TestContainer {
32
+ constructor ( rootID , createNodeMock ) {
33
+ this . rootID = rootID ;
34
+ this . createNodeMock = createNodeMock ;
35
+ }
36
+ appendChild ( child ) { }
37
+ insertBefore ( beforeChild , child ) { }
38
+ removeChild ( child ) { }
39
+ toJSON ( ) { }
40
+ }
51
41
52
- var updateProperties = ( element , type , oldProps , newProps ) => {
53
- const { children, ...props } = newProps ;
54
- element . type = type ;
55
- element . props = props ;
56
- element . children = typeof children === 'undefined' ? null : Array . isArray ( children ) ? children : [ children ] ;
57
- } ;
42
+ class TestComponent {
43
+ constructor ( type , props , rootContainerInstance ) {
44
+ this . id = instanceCounter ++ ;
45
+ this . type = type ;
46
+ this . props = props ;
47
+ this . children = [ ] ;
48
+ this . rootContainerInstance = rootContainerInstance ;
49
+
50
+ Object . defineProperty ( this , 'id' , { value : this . id , enumerable : false } ) ;
51
+ Object . defineProperty ( this , '$$typeof' , {
52
+ value : Symbol . for ( 'react.test.json' ) ,
53
+ } ) ;
54
+ }
55
+
56
+ appendChild ( child ) {
57
+ const index = this . children . indexOf ( child ) ;
58
+ if ( index !== - 1 ) {
59
+ this . children . splice ( index , 1 ) ;
60
+ }
61
+ this . children . push ( child ) ;
62
+ }
63
+
64
+ removeChild ( child ) {
65
+ const index = this . children . indexOf ( child ) ;
66
+ if ( index === - 1 ) {
67
+ throw new Error ( 'This child does not exist.' ) ;
68
+ }
69
+ this . children . splice ( index , 1 ) ;
70
+ }
71
+
72
+ insertBefore ( beforeChild , child ) {
73
+ const index = this . children . indexOf ( child ) ;
74
+ if ( index !== - 1 ) {
75
+ this . children . splice ( index , 1 ) ;
76
+ }
77
+ const beforeIndex = this . children . indexOf ( beforeChild ) ;
78
+ if ( beforeIndex === - 1 ) {
79
+ throw new Error ( 'This child does not exist.' ) ;
80
+ }
81
+ this . children . splice ( beforeIndex , 0 , child ) ;
82
+ }
83
+
84
+ update ( type , props ) {
85
+ // console.log('update', type, props, this.children);
86
+ // need to manually update text nodes
87
+ this . type = type ;
88
+ this . props = props ;
89
+ }
90
+
91
+ toJSON ( ) {
92
+ // eslint-disable ignore the children
93
+ const { children, ...props } = this . props ;
94
+ // eslint-enable
95
+ const json = {
96
+ type : this . type ,
97
+ props : props ,
98
+ children : null ,
99
+ } ;
100
+
101
+ Object . defineProperty ( json , '$$typeof' , { value : Symbol . for ( 'react.test.json' ) } ) ;
102
+ if ( typeof children === 'string' ) {
103
+ json . children = [ children ] ;
104
+ } else {
105
+ var childrenJSON = [ ] ;
106
+ this . children . forEach ( ( child ) => {
107
+ if ( typeof child . toJSON === 'function' ) {
108
+ childrenJSON . push ( child . toJSON ( ) ) ;
109
+ } else if ( typeof child . text !== 'undefined' ) {
110
+ childrenJSON . push ( isNaN ( + child . text ) ? child . text : + child . text ) ;
111
+ }
112
+ } ) ;
113
+ json . children = childrenJSON . length ? childrenJSON : null ;
114
+ }
115
+ return json ;
116
+ }
117
+ }
118
+
119
+ type Instance = TestComponent ;
58
120
59
121
var TestRenderer = ReactFiberReconciler ( {
60
122
getRootHostContext ( rootContainerInstance : Container ) : HostContext {
@@ -83,20 +145,11 @@ var TestRenderer = ReactFiberReconciler({
83
145
hostContext : HostContext ,
84
146
internalInstanceHandle : Object ,
85
147
) : Instance {
86
- const inst = createElement ( type , props , rootContainerInstance ) ;
87
- return inst ;
148
+ return new TestComponent ( type , props , rootContainerInstance ) ;
88
149
} ,
89
150
90
151
appendInitialChild ( parentInstance : Instance , child : Instance | TextInstance ) : void {
91
- const appliedChild = child . text ? String ( child . text ) : child ;
92
- if ( parentInstance . children == null ) {
93
- parentInstance . children = appliedChild ;
94
- } else if ( Array . isArray ( parentInstance . children ) ) {
95
- parentInstance . children =
96
- parentInstance . children . concat ( appliedChild ) ;
97
- } else {
98
- // parentInstance.children = appliedChild;
99
- }
152
+ parentInstance. appendChild ( child ) ;
100
153
} ,
101
154
102
155
finalizeInitialChildren (
@@ -127,10 +180,7 @@ var TestRenderer = ReactFiberReconciler({
127
180
rootContainerInstance : Container ,
128
181
internalInstanceHandle : Object ,
129
182
) : void {
130
- // Update the internal instance handle so that we know which props are
131
- // the current ones.
132
- // precacheFiberNode(internalInstanceHandle, testElement);
133
- updateProperties ( instance , type , oldProps , newProps ) ;
183
+ instance . update ( type , newProps ) ;
134
184
} ,
135
185
136
186
shouldSetTextContent ( props : Props ) : boolean {
@@ -148,7 +198,6 @@ var TestRenderer = ReactFiberReconciler({
148
198
hostContext : HostContext ,
149
199
internalInstanceHandle : Object
150
200
) : TextInstance {
151
- // return text;
152
201
var inst = { text : text , id : instanceCounter ++ } ;
153
202
// Hide from unit tests
154
203
Object . defineProperty ( inst , 'id' , { value : inst . id , enumerable : false } ) ;
@@ -160,53 +209,19 @@ var TestRenderer = ReactFiberReconciler({
160
209
} ,
161
210
162
211
appendChild ( parentInstance : Instance | Container , child : Instance | TextInstance ) : void {
163
- if ( parentInstance . children === null ) {
164
- parentInstance . children = [ child ] ;
165
- } else {
166
- const index = parentInstance . children . indexOf ( child ) ;
167
- if ( index !== - 1 ) {
168
- parentInstance . children . splice ( index , 1 ) ;
169
- }
170
- parentInstance . children . push ( child ) ;
171
- }
212
+ parentInstance. appendChild ( child ) ;
172
213
} ,
173
214
174
215
insertBefore (
175
216
parentInstance : Instance | Container ,
176
217
child : Instance | TextInstance ,
177
218
beforeChild : Instance | TextInstance
178
219
) : void {
179
- console. log ( 'insertBefore' ) ;
180
- if ( parentInstance . children === null ) {
181
- throw new Error ( 'This child does not exist.' ) ;
182
- }
183
- const index = parentInstance . children . indexOf ( child ) ;
184
- if ( index !== - 1 ) {
185
- parentInstance . children . splice ( index , 1 ) ;
186
- }
187
- const beforeIndex = parentInstance . children . indexOf ( beforeChild ) ;
188
- if ( beforeIndex === - 1 ) {
189
- throw new Error ( 'This child does not exist.' ) ;
190
- }
191
- parentInstance . children . splice ( beforeIndex , 0 , child ) ;
220
+ parentInstance . insertBefore ( beforeChild , child ) ;
192
221
} ,
193
222
194
223
removeChild ( parentInstance : Instance | Container , child : Instance | TextInstance ) : void {
195
- if ( parentInstance . children === null ) {
196
- throw new Error ( 'This child does not exist.' ) ;
197
- }
198
- const index = parentInstance . children . indexOf ( child ) ;
199
- if ( index === - 1 ) {
200
- throw new Error ( 'This child does not exist.' ) ;
201
- }
202
- parentInstance . children . splice ( index , 1 ) ;
203
- } ,
204
-
205
- getPublicInstance (
206
- privateInstance : Instance | Container ,
207
- hostContainerInfo
208
- ) {
209
- console . log ( 'TODO: implement me for getNodeMock' ) ;
224
+ parentInstance. removeChild ( child ) ;
210
225
} ,
211
226
212
227
scheduleAnimationCallback : window . requestAnimationFrame ,
@@ -222,39 +237,23 @@ var defaultTestOptions = {
222
237
} ,
223
238
} ;
224
239
225
- var toJSON = ( instance ) => {
226
- var { children, ...json } = instance ;
227
- Object . defineProperty ( json , '$$typeof' , { value : Symbol . for ( 'react.test.json' ) } ) ;
228
-
229
- var childrenJSON = [ ] ;
230
- if ( children ) {
231
- children . forEach ( ( child ) => {
232
- if ( child . $$typeof === Symbol . for ( 'react.element' ) ) {
233
- // childrenJSON.push(toJSON(child));
234
- // skip maybe console.log('TODO: getPublicInstance');
235
- // probably recurse
236
- } else {
237
- childrenJSON . push ( child ) ;
238
- }
239
- } ) ;
240
- }
241
- json . children = childrenJSON . length ? childrenJSON : null ;
242
- return json ;
243
- } ;
244
-
245
240
var ReactTestFiberRenderer = {
246
- create ( element ) {
247
- var container = { rootID : '<default>' , children : [ ] , createNodeMock : defaultTestOptions . createNodeMock } ;
241
+ create ( element , options ) {
242
+ var createNodeMock = defaultTestOptions . createNodeMock ;
243
+ if ( options && typeof options . createNodeMock === 'function' ) {
244
+ createNodeMock = options . createNodeMock ;
245
+ }
246
+ var container = new TestContainer ( '<default>' , createNodeMock ) ;
248
247
var root = TestRenderer . mountContainer ( element , container , null , null ) ;
249
248
return {
250
249
toJSON ( ) {
251
250
const hostInstance = TestRenderer . findHostInstance ( root ) ;
252
251
if ( hostInstance === null ) {
253
252
return hostInstance ;
254
253
}
255
-
256
- return toJSON ( hostInstance ) ;
254
+ return hostInstance . toJSON ( ) ;
257
255
} ,
256
+
258
257
update ( newElement ) {
259
258
TestRenderer . updateContainer ( newElement , root , null , null ) ;
260
259
} ,
0 commit comments