Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
require('mock-modules')
15
.dontMock('cloneWithProps')
16
.dontMock('emptyObject');
17
18
var mocks = require('mocks');
19
20
var React;
21
var ReactTestUtils;
22
23
var onlyChild;
24
var cloneWithProps;
25
var emptyObject;
26
27
describe('cloneWithProps', function() {
28
29
beforeEach(function() {
30
React = require('React');
31
ReactTestUtils = require('ReactTestUtils');
32
onlyChild = require('onlyChild');
33
cloneWithProps = require('cloneWithProps');
34
emptyObject = require('emptyObject');
35
});
36
37
it('should clone a DOM component with new props', function() {
38
var Grandparent = React.createClass({
39
render: function() {
40
return <Parent><div className="child" /></Parent>;
41
}
42
});
43
var Parent = React.createClass({
44
render: function() {
45
return (
46
<div className="parent">
47
{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}
48
</div>
49
);
50
}
51
});
52
var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
53
expect(component.getDOMNode().childNodes[0].className)
54
.toBe('xyz child');
55
});
56
57
it('should clone a composite component with new props', function() {
58
59
var Child = React.createClass({
60
render: function() {
61
return <div className={this.props.className} />;
62
}
63
});
64
65
var Grandparent = React.createClass({
66
render: function() {
67
return <Parent><Child className="child" /></Parent>;
68
}
69
});
70
var Parent = React.createClass({
71
render: function() {
72
return (
73
<div className="parent">
74
{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}
75
</div>
76
);
77
}
78
});
79
var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
80
expect(component.getDOMNode().childNodes[0].className)
81
.toBe('xyz child');
82
});
83
84
it('should warn when cloning with refs', function() {
85
var Grandparent = React.createClass({
86
render: function() {
87
return <Parent><div ref="yolo" /></Parent>;
88
}
89
});
90
var Parent = React.createClass({
91
render: function() {
92
return (
93
<div>
94
{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}
95
</div>
96
);
97
}
98
});
99
100
var _warn = console.warn;
101
102
try {
103
console.warn = mocks.getMockFunction();
104
105
var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
106
expect(component.refs).toBe(emptyObject);
107
expect(console.warn.mock.calls.length).toBe(1);
108
} finally {
109
console.warn = _warn;
110
}
111
});
112
113
it('should transfer the key property', function() {
114
var Component = React.createClass({
115
render: function() {
116
return null;
117
}
118
});
119
var clone = cloneWithProps(<Component />, {key: 'xyz'});
120
expect(clone.key).toBe('xyz');
121
});
122
123
it('should transfer children', function() {
124
var Component = React.createClass({
125
render: function() {
126
expect(this.props.children).toBe('xyz');
127
return <div />;
128
}
129
});
130
131
ReactTestUtils.renderIntoDocument(
132
cloneWithProps(<Component />, {children: 'xyz'})
133
);
134
});
135
136
it('should shallow clone children', function() {
137
var Component = React.createClass({
138
render: function() {
139
expect(this.props.children).toBe('xyz');
140
return <div />;
141
}
142
});
143
144
ReactTestUtils.renderIntoDocument(
145
cloneWithProps(<Component>xyz</Component>, {})
146
);
147
});
148
149
it('should support keys and refs', function() {
150
var Component = React.createClass({
151
render: function() {
152
return <div />;
153
}
154
});
155
156
var Parent = React.createClass({
157
render: function() {
158
var clone =
159
cloneWithProps(this.props.children, {key: 'xyz', ref: 'xyz'});
160
expect(clone.key).toBe('xyz');
161
expect(clone.ref).toBe('xyz');
162
return <div>{clone}</div>;
163
}
164
});
165
166
var Grandparent = React.createClass({
167
render: function() {
168
return <Parent><Component key="abc" /></Parent>;
169
}
170
});
171
172
ReactTestUtils.renderIntoDocument(<Grandparent />);
173
});
174
175
it('should overwrite props', function() {
176
var Component = React.createClass({
177
render: function() {
178
expect(this.props.myprop).toBe('xyz');
179
return <div />;
180
}
181
});
182
183
ReactTestUtils.renderIntoDocument(
184
cloneWithProps(<Component myprop="abc" />, {myprop: 'xyz'})
185
);
186
});
187
});
188
189