-
Notifications
You must be signed in to change notification settings - Fork 627
/
cloneElement.js
312 lines (264 loc) 路 7.65 KB
/
cloneElement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* @jsx createElement */
import cloneElement from '../';
import { createElement, Component, render, shared } from 'rax';
import createFactory from 'rax-create-factory';
import ServerDriver from 'driver-server';
const { Host } = shared;
describe('cloneElement', () => {
function createNodeElement(tagName) {
return {
nodeType: 1,
tagName: tagName.toUpperCase(),
attributes: {},
style: {},
childNodes: [],
parentNode: null
};
}
beforeEach(function() {
Host.driver = ServerDriver;
});
afterEach(function() {
Host.driver = null;
});
class ComponentClass extends Component {
render() {
return <div />;
}
}
it('should use the same key for a cloned element', function() {
var instance =
<div>
<div />
</div>
;
var element = instance.props.children;
var cloned = cloneElement(element);
expect(cloned.key).toBe(element.key);
});
it('should clone a DOM component with new props', function() {
const container = createNodeElement('div');
class Grandparent {
render() {
return <Parent child={<div className="child" />} />;
}
}
class Parent {
render() {
return (
<div className="parent">
{cloneElement(this.props.child, { className: 'xyz' })}
</div>
);
}
}
render(<Grandparent />, container);
expect(container.childNodes[0].childNodes[0].attributes.class).toBe('xyz');
});
it('should clone a composite component with new props', function() {
const container = createNodeElement('div');
class Child {
render() {
return <div className={this.props.className} />;
}
}
class Grandparent {
render() {
return <Parent child={<Child className="child" />} />;
}
}
class Parent {
render() {
return (
<div className="parent">
{cloneElement(this.props.child, { className: 'xyz' })}
</div>
);
}
}
render(<Grandparent />, container);
expect(container.childNodes[0].childNodes[0].attributes.class).toBe('xyz');
});
it('should keep the original ref if it is not overridden', function() {
class Grandparent {
render() {
return <Parent child={<div ref="yolo" />} />;
}
}
class Parent {
render() {
return (
<div>
{cloneElement(this.props.child, { className: 'xyz' })}
</div>
);
}
}
var component = render(<Grandparent />);
expect(component.refs.yolo.tagName).toBe('DIV');
});
it('should transfer the key property', function() {
class MyComponent {
render() {
return null;
}
}
var clone = cloneElement(<MyComponent />, {key: 'xyz'});
expect(clone.key).toBe('xyz');
});
it('should transfer children', function() {
class MyComponent {
render() {
expect(this.props.children).toBe('xyz');
return <div />;
}
}
render(
cloneElement(<MyComponent />, {children: 'xyz'})
);
});
it('should shallow clone children', function() {
class MyComponent {
render() {
expect(this.props.children).toBe('xyz');
return <div />;
}
}
render(
cloneElement(<MyComponent>xyz</MyComponent>, {})
);
});
it('should accept children as rest arguments', function() {
class MyComponent {
render() {
return null;
}
}
var clone = cloneElement(
<MyComponent>xyz</MyComponent>,
{ children: <MyComponent /> },
<div />,
<span />
);
expect(clone.props.children).toEqual([
<div />,
<span />,
]);
});
it('should override children if undefined is provided as an argument', function() {
var element = createElement(ComponentClass, {
children: 'text',
}, undefined);
expect(element.props.children).toBe(undefined);
var element2 = cloneElement(createElement(ComponentClass, {
children: 'text',
}), {}, undefined);
expect(element2.props.children).toBe(undefined);
});
it('should support keys and refs', function() {
class Parent {
render() {
var clone = cloneElement(this.props.children, {key: 'xyz', ref: 'xyz'});
expect(clone.key).toBe('xyz');
expect(clone.ref).toBe('xyz');
return <div>{clone}</div>;
}
}
class Grandparent {
render() {
return <Parent ref="parent"><span key="abc" /></Parent>;
}
}
var component = render(<Grandparent />);
expect(component.refs.parent.refs.xyz.tagName).toBe('SPAN');
});
it('should steal the ref if a new ref is specified', function() {
class Parent {
render() {
var clone = cloneElement(this.props.children, {ref: 'xyz'});
return <div>{clone}</div>;
}
}
class Grandparent {
render() {
return <Parent ref="parent"><span ref="child" /></Parent>;
}
}
var component = render(<Grandparent />);
expect(component.refs.child).toBeUndefined();
expect(component.refs.parent.refs.xyz.tagName).toBe('SPAN');
});
it('should overwrite props', function() {
class MyComponent {
render() {
expect(this.props.myprop).toBe('xyz');
return <div />;
}
}
render(
cloneElement(<MyComponent myprop="abc" />, {myprop: 'xyz'})
);
});
it('should normalize props with default values', function() {
class MyComponent {
static defaultProps = {prop: 'testKey'};
render() {
return <span />;
}
}
var instance = createElement(MyComponent);
var clonedInstance = cloneElement(instance, {prop: undefined});
expect(clonedInstance.props.prop).toBe('testKey');
var clonedInstance2 = cloneElement(instance, {prop: null});
expect(clonedInstance2.props.prop).toBe(null);
var instance2 = createElement(MyComponent, {prop: 'newTestKey'});
var cloneInstance3 = cloneElement(instance2, {prop: undefined});
expect(cloneInstance3.props.prop).toBe('testKey');
var cloneInstance4 = cloneElement(instance2, {});
expect(cloneInstance4.props.prop).toBe('newTestKey');
});
it('should ignore key and ref warning getters', function() {
var elementA = createElement('div');
var elementB = cloneElement(elementA, elementA.props);
expect(elementB.key).toBe(null);
expect(elementB.ref).toBe(null);
});
it('should ignore undefined key and ref', function() {
expect(() => {
var element = createFactory(ComponentClass)({
key: '12',
ref: '34',
foo: '56',
});
var props = {
key: undefined,
ref: undefined,
foo: 'ef',
};
var clone = cloneElement(element, props);
expect(clone.type).toBe(ComponentClass);
expect(clone.key).toBe('12');
expect(clone.ref).toBe('34');
expect(clone.props).toEqual({foo: 'ef'});
}).toWarnDev('Adding a string ref "34" that was not created inside render method, or multiple copies of Rax are used.', {withoutStack: true});
});
it('should extract null key and ref', function() {
expect(() => {
var element = createFactory(ComponentClass)({
key: '12',
ref: '34',
foo: '56',
});
var props = {
key: null,
ref: null,
foo: 'ef',
};
var clone = cloneElement(element, props);
expect(clone.type).toBe(ComponentClass);
expect(clone.key).toBe('null');
expect(clone.ref).toBe(null);
expect(clone.props).toEqual({foo: 'ef'});
}).toWarnDev('Adding a string ref "34" that was not created inside render method, or multiple copies of Rax are used.', {withoutStack: true});
});
});