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
var React;
15
var ReactTestUtils;
16
var reactComponentExpect;
17
18
var TestComponent;
19
20
describe('ReactPropTransferer', function() {
21
22
beforeEach(function() {
23
require('mock-modules').dumpCache();
24
25
React = require('React');
26
ReactTestUtils = require('ReactTestUtils');
27
reactComponentExpect = require('reactComponentExpect');
28
29
// We expect to get a warning from transferPropsTo since it's deprecated
30
spyOn(console, 'warn');
31
32
TestComponent = React.createClass({
33
render: function() {
34
var result = this.transferPropsTo(
35
<input
36
className="textinput"
37
style={{display: 'block', color: 'green'}}
38
type="text"
39
value=""
40
/>
41
);
42
expect(console.warn).toHaveBeenCalled();
43
return result;
44
}
45
});
46
});
47
48
it('should leave explicitly specified properties intact', function() {
49
var instance = <TestComponent type="radio" />;
50
instance = ReactTestUtils.renderIntoDocument(instance);
51
52
reactComponentExpect(instance)
53
.expectRenderedChild()
54
.toBeComponentOfType('input')
55
.scalarPropsEqual({
56
className: 'textinput',
57
style: {display: 'block', color: 'green'},
58
type: 'text',
59
value: ''
60
});
61
});
62
63
it('should transfer unspecified properties', function() {
64
var instance = <TestComponent placeholder="Type here..." />;
65
instance = ReactTestUtils.renderIntoDocument(instance);
66
67
reactComponentExpect(instance)
68
.expectRenderedChild()
69
.toBeComponentOfType('input')
70
.scalarPropsEqual({placeholder: 'Type here...'});
71
});
72
73
it('should transfer using merge strategies', function() {
74
var instance =
75
<TestComponent
76
className="hidden_elem"
77
style={{width: '100%', display: 'none'}}
78
/>;
79
instance = ReactTestUtils.renderIntoDocument(instance);
80
81
reactComponentExpect(instance)
82
.expectRenderedChild()
83
.toBeComponentOfType('input')
84
.scalarPropsEqual({
85
className: 'textinput hidden_elem',
86
style: {
87
color: 'green',
88
display: 'block',
89
width: '100%'
90
}
91
});
92
});
93
94
it('should not transfer children', function() {
95
var ChildrenTestComponent = React.createClass({
96
render: function() {
97
return this.transferPropsTo(<div />);
98
}
99
});
100
101
var instance =
102
<ChildrenTestComponent>
103
<span>Hello!</span>
104
</ChildrenTestComponent>;
105
106
instance = ReactTestUtils.renderIntoDocument(instance);
107
reactComponentExpect(instance)
108
.expectRenderedChild()
109
.toBeDOMComponentWithTag('div')
110
.toBeDOMComponentWithNoChildren();
111
});
112
113
it('should not transfer ref or key', function() {
114
var TestComponent = React.createClass({
115
render: function() {
116
expect(this.props.ref).toBeUndefined();
117
expect(this.props.key).toBeUndefined();
118
return <div />;
119
}
120
});
121
var OuterTestComponent = React.createClass({
122
render: function() {
123
return this.transferPropsTo(<TestComponent />);
124
}
125
});
126
var OuterOuterTestComponent = React.createClass({
127
render: function() {
128
return <OuterTestComponent ref="testref" key="testkey" />;
129
}
130
});
131
132
ReactTestUtils.renderIntoDocument(<OuterOuterTestComponent />);
133
});
134
135
it('should not transferPropsTo() a component you don\'t own', function() {
136
var Parent = React.createClass({
137
render: function() {
138
return <Child><span /></Child>;
139
}
140
});
141
142
var Child = React.createClass({
143
render: function() {
144
return this.transferPropsTo(this.props.children);
145
}
146
});
147
148
expect(function() {
149
ReactTestUtils.renderIntoDocument(<Parent />);
150
}).toThrow(
151
'Invariant Violation: ' +
152
'Child: You can\'t call transferPropsTo() on a component that you ' +
153
'don\'t own, span. ' +
154
'This usually means you are calling transferPropsTo() on a component ' +
155
'passed in as props or children.'
156
);
157
});
158
159
it('uses the default instead of the transferred prop (regress)', function() {
160
161
var Child = React.createClass({
162
163
getDefaultProps: function() {
164
return {
165
x: 2
166
};
167
},
168
169
render: function() {
170
expect(this.props.x).toBe(2);
171
return <div />;
172
}
173
174
});
175
176
var Parent = React.createClass({
177
178
render: function() {
179
return this.transferPropsTo(<Child />);
180
}
181
182
});
183
184
ReactTestUtils.renderIntoDocument(<Parent x={5} />);
185
186
});
187
188
});
189
190