Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 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 ReactTransitionGroup;
16
var mocks;
17
18
// Most of the real functionality is covered in other unit tests, this just
19
// makes sure we're wired up correctly.
20
describe('ReactTransitionGroup', function() {
21
var container;
22
23
beforeEach(function() {
24
React = require('React');
25
ReactTransitionGroup = require('ReactTransitionGroup');
26
mocks = require('mocks');
27
28
container = document.createElement('div');
29
});
30
31
32
it('should handle willEnter correctly', function() {
33
var log = [];
34
35
var Child = React.createClass({
36
componentDidMount: function() {
37
log.push('didMount');
38
},
39
componentWillEnter: function(cb) {
40
log.push('willEnter');
41
cb();
42
},
43
componentDidEnter: function() {
44
log.push('didEnter');
45
},
46
componentWillLeave: function(cb) {
47
log.push('willLeave');
48
cb();
49
},
50
componentDidLeave: function() {
51
log.push('didLeave');
52
},
53
componentWillUnmount: function() {
54
log.push('willUnmount');
55
},
56
render: function() {
57
return <span />;
58
}
59
});
60
61
var Component = React.createClass({
62
getInitialState: function() {
63
return {count: 1};
64
},
65
render: function() {
66
var children = [];
67
for (var i = 0; i < this.state.count; i++) {
68
children.push(<Child key={i} />);
69
}
70
return <ReactTransitionGroup>{children}</ReactTransitionGroup>;
71
}
72
});
73
74
var instance = React.render(<Component />, container);
75
expect(log).toEqual(['didMount']);
76
77
instance.setState({count: 2}, function() {
78
expect(log).toEqual(['didMount', 'didMount', 'willEnter', 'didEnter']);
79
instance.setState({count: 1}, function() {
80
expect(log).toEqual([
81
"didMount", "didMount", "willEnter", "didEnter",
82
"willLeave", "didLeave", "willUnmount"
83
]);
84
});
85
});
86
});
87
88
it('should handle enter/leave/enter/leave correctly', function() {
89
var log = [];
90
var cb;
91
92
var Child = React.createClass({
93
componentDidMount: function() {
94
log.push('didMount');
95
},
96
componentWillEnter: function(_cb) {
97
log.push('willEnter');
98
cb = _cb;
99
},
100
componentDidEnter: function() {
101
log.push('didEnter');
102
},
103
componentWillLeave: function(cb) {
104
log.push('willLeave');
105
cb();
106
},
107
componentDidLeave: function() {
108
log.push('didLeave');
109
},
110
componentWillUnmount: function() {
111
log.push('willUnmount');
112
},
113
render: function() {
114
return <span />;
115
}
116
});
117
118
var Component = React.createClass({
119
getInitialState: function() {
120
return {count: 1};
121
},
122
render: function() {
123
var children = [];
124
for (var i = 0; i < this.state.count; i++) {
125
children.push(<Child key={i} />);
126
}
127
return <ReactTransitionGroup>{children}</ReactTransitionGroup>;
128
}
129
});
130
131
var instance = React.render(<Component />, container);
132
expect(log).toEqual(['didMount']);
133
instance.setState({count: 2});
134
expect(log).toEqual(['didMount', 'didMount', 'willEnter']);
135
for (var i = 0; i < 5; i++) {
136
instance.setState({count: 2});
137
expect(log).toEqual(['didMount', 'didMount', 'willEnter']);
138
instance.setState({count: 1});
139
}
140
cb();
141
expect(log).toEqual([
142
'didMount', 'didMount', 'willEnter',
143
'didEnter', 'willLeave', 'didLeave', 'willUnmount'
144
]);
145
});
146
147
it('should handle enter/leave/enter correctly', function() {
148
var log = [];
149
var cb;
150
151
var Child = React.createClass({
152
componentDidMount: function() {
153
log.push('didMount');
154
},
155
componentWillEnter: function(_cb) {
156
log.push('willEnter');
157
cb = _cb;
158
},
159
componentDidEnter: function() {
160
log.push('didEnter');
161
},
162
componentWillLeave: function(cb) {
163
log.push('willLeave');
164
cb();
165
},
166
componentDidLeave: function() {
167
log.push('didLeave');
168
},
169
componentWillUnmount: function() {
170
log.push('willUnmount');
171
},
172
render: function() {
173
return <span />;
174
}
175
});
176
177
var Component = React.createClass({
178
getInitialState: function() {
179
return {count: 1};
180
},
181
render: function() {
182
var children = [];
183
for (var i = 0; i < this.state.count; i++) {
184
children.push(<Child key={i} />);
185
}
186
return <ReactTransitionGroup>{children}</ReactTransitionGroup>;
187
}
188
});
189
190
var instance = React.render(<Component />, container);
191
expect(log).toEqual(['didMount']);
192
instance.setState({count: 2});
193
expect(log).toEqual(['didMount', 'didMount', 'willEnter']);
194
for (var i = 0; i < 5; i++) {
195
instance.setState({count: 1});
196
expect(log).toEqual(['didMount', 'didMount', 'willEnter']);
197
instance.setState({count: 2});
198
}
199
cb();
200
expect(log).toEqual([
201
'didMount', 'didMount', 'willEnter', 'didEnter'
202
]);
203
});
204
});
205
206