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
17
var reactComponentExpect;
18
19
describe('ReactComponent', function() {
20
beforeEach(function() {
21
React = require('React');
22
ReactTestUtils = require('ReactTestUtils');
23
reactComponentExpect = require('reactComponentExpect');
24
});
25
26
it('should throw on invalid render targets', function() {
27
var container = document.createElement('div');
28
// jQuery objects are basically arrays; people often pass them in by mistake
29
expect(function() {
30
React.render(<div></div>, [container]);
31
}).toThrow(
32
'Invariant Violation: _registerComponent(...): Target container ' +
33
'is not a DOM element.'
34
);
35
36
expect(function() {
37
React.render(<div></div>, null);
38
}).toThrow(
39
'Invariant Violation: _registerComponent(...): Target container ' +
40
'is not a DOM element.'
41
);
42
});
43
44
it('should throw when supplying a ref outside of render method', function() {
45
var instance = <div ref="badDiv" />;
46
expect(function() {
47
instance = ReactTestUtils.renderIntoDocument(instance);
48
}).toThrow();
49
});
50
51
it('should throw when attempting to hijack a ref', function() {
52
var Component = React.createClass({
53
render: function() {
54
var child = this.props.child;
55
this.attachRef('test', child);
56
return child;
57
}
58
});
59
60
var childInstance = ReactTestUtils.renderIntoDocument(<span />);
61
var instance = <Component child={childInstance} />;
62
63
expect(function() {
64
instance = ReactTestUtils.renderIntoDocument(instance);
65
}).toThrow(
66
'Invariant Violation: attachRef(test, ...): Only a component\'s owner ' +
67
'can store a ref to it.'
68
);
69
});
70
71
it('should support refs on owned components', function() {
72
var innerObj = {}, outerObj = {};
73
74
var Wrapper = React.createClass({
75
76
getObject: function() {
77
return this.props.object;
78
},
79
80
render: function() {
81
return <div>{this.props.children}</div>;
82
}
83
84
});
85
86
var Component = React.createClass({
87
render: function() {
88
var inner = <Wrapper object={innerObj} ref="inner" />;
89
var outer = <Wrapper object={outerObj} ref="outer">{inner}</Wrapper>;
90
return outer;
91
},
92
componentDidMount: function() {
93
expect(this.refs.inner.getObject()).toEqual(innerObj);
94
expect(this.refs.outer.getObject()).toEqual(outerObj);
95
}
96
});
97
98
var instance = <Component />;
99
instance = ReactTestUtils.renderIntoDocument(instance);
100
});
101
102
it('should not have refs on unmounted components', function() {
103
var Parent = React.createClass({
104
render: function() {
105
return <Child><div ref="test" /></Child>;
106
},
107
componentDidMount: function() {
108
expect(this.refs && this.refs.test).toEqual(undefined);
109
}
110
});
111
var Child = React.createClass({
112
render: function() {
113
return <div />;
114
}
115
});
116
117
var instance = <Parent child={<span />} />;
118
instance = ReactTestUtils.renderIntoDocument(instance);
119
});
120
121
it('should correctly determine if a component is mounted', function() {
122
var Component = React.createClass({
123
componentWillMount: function() {
124
expect(this.isMounted()).toBeFalsy();
125
},
126
componentDidMount: function() {
127
expect(this.isMounted()).toBeTruthy();
128
},
129
render: function() {
130
return <div/>;
131
}
132
});
133
134
var element = <Component />;
135
136
var instance = ReactTestUtils.renderIntoDocument(element);
137
expect(instance.isMounted()).toBeTruthy();
138
});
139
140
it('should know its simple mount depth', function() {
141
var Owner = React.createClass({
142
render: function() {
143
return <Child ref="child" />;
144
}
145
});
146
147
var Child = React.createClass({
148
render: function() {
149
return <div />;
150
}
151
});
152
153
var instance = <Owner />;
154
instance = ReactTestUtils.renderIntoDocument(instance);
155
expect(instance._mountDepth).toBe(0);
156
expect(instance.refs.child._mountDepth).toBe(1);
157
});
158
159
it('should know its (complicated) mount depth', function() {
160
var Box = React.createClass({
161
render: function() {
162
return <div ref="boxDiv">{this.props.children}</div>;
163
}
164
});
165
166
var Child = React.createClass({
167
render: function() {
168
return <span ref="span">child</span>;
169
}
170
});
171
172
var Switcher = React.createClass({
173
getInitialState: function() {
174
return {tabKey: 'hello'};
175
},
176
177
render: function() {
178
var child = this.props.children;
179
180
return (
181
<Box ref="box">
182
<div
183
ref="switcherDiv"
184
style={{
185
display: this.state.tabKey === child.key ? '' : 'none'
186
}}>
187
{child}
188
</div>
189
</Box>
190
);
191
}
192
});
193
194
var App = React.createClass({
195
render: function() {
196
return (
197
<Switcher ref="switcher">
198
<Child key="hello" ref="child" />
199
</Switcher>
200
);
201
}
202
});
203
204
var root = <App />;
205
root = ReactTestUtils.renderIntoDocument(root);
206
207
expect(root._mountDepth).toBe(0);
208
expect(root.refs.switcher._mountDepth).toBe(1);
209
expect(root.refs.switcher.refs.box._mountDepth).toBe(2);
210
expect(root.refs.switcher.refs.switcherDiv._mountDepth).toBe(4);
211
expect(root.refs.child._mountDepth).toBe(5);
212
expect(root.refs.switcher.refs.box.refs.boxDiv._mountDepth).toBe(3);
213
expect(root.refs.child.refs.span._mountDepth).toBe(6);
214
});
215
});
216
217