Skip to content

Commit 2c4500b

Browse files
committed
TestRendererFiber: use a simple class as TestComponentInstances
1 parent 9620ed5 commit 2c4500b

File tree

1 file changed

+102
-103
lines changed

1 file changed

+102
-103
lines changed

src/renderers/testing/fiber/ReactTestFiberRenderer.js

Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,96 @@ type ReactTestRendererJSON = {
2727
}
2828

2929
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-
};
4730

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+
}
5141

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;
58120

59121
var TestRenderer = ReactFiberReconciler({
60122
getRootHostContext(rootContainerInstance : Container) : HostContext {
@@ -83,20 +145,11 @@ var TestRenderer = ReactFiberReconciler({
83145
hostContext : HostContext,
84146
internalInstanceHandle : Object,
85147
) : Instance {
86-
const inst = createElement(type, props, rootContainerInstance);
87-
return inst;
148+
return new TestComponent(type, props, rootContainerInstance);
88149
},
89150

90151
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);
100153
},
101154

102155
finalizeInitialChildren(
@@ -127,10 +180,7 @@ var TestRenderer = ReactFiberReconciler({
127180
rootContainerInstance : Container,
128181
internalInstanceHandle : Object,
129182
) : 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);
134184
},
135185

136186
shouldSetTextContent(props : Props) : boolean {
@@ -148,7 +198,6 @@ var TestRenderer = ReactFiberReconciler({
148198
hostContext : HostContext,
149199
internalInstanceHandle : Object
150200
) : TextInstance {
151-
// return text;
152201
var inst = { text : text, id: instanceCounter++ };
153202
// Hide from unit tests
154203
Object.defineProperty(inst, 'id', { value: inst.id, enumerable: false });
@@ -160,53 +209,19 @@ var TestRenderer = ReactFiberReconciler({
160209
},
161210

162211
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);
172213
},
173214

174215
insertBefore(
175216
parentInstance : Instance | Container,
176217
child : Instance | TextInstance,
177218
beforeChild : Instance | TextInstance
178219
) : 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);
192221
},
193222

194223
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);
210225
},
211226

212227
scheduleAnimationCallback: window.requestAnimationFrame,
@@ -222,39 +237,23 @@ var defaultTestOptions = {
222237
},
223238
};
224239

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-
245240
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);
248247
var root = TestRenderer.mountContainer(element, container, null, null);
249248
return {
250249
toJSON() {
251250
const hostInstance = TestRenderer.findHostInstance(root);
252251
if (hostInstance === null) {
253252
return hostInstance;
254253
}
255-
256-
return toJSON(hostInstance);
254+
return hostInstance.toJSON();
257255
},
256+
258257
update(newElement) {
259258
TestRenderer.updateContainer(newElement, root, null, null);
260259
},

0 commit comments

Comments
 (0)