-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathReactCompositeComponentState-test.js
More file actions
178 lines (157 loc) · 6.48 KB
/
ReactCompositeComponentState-test.js
File metadata and controls
178 lines (157 loc) · 6.48 KB
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
/**
* Copyright 2013-2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/
"use strict";
var mocks = require('mocks');
var React;
var ReactInstanceMap;
var ReactTestUtils;
var reactComponentExpect;
var TestComponent;
describe('ReactCompositeComponent-state', function() {
beforeEach(function() {
React = require('React');
ReactInstanceMap = require('ReactInstanceMap');
ReactTestUtils = require('ReactTestUtils');
reactComponentExpect = require('reactComponentExpect');
TestComponent = React.createClass({
peekAtState: function(from, state) {
if (state !== undefined) {
this.props.stateListener(from, state && state.color);
} else {
var internalInstance = ReactInstanceMap.get(this);
var pendingState = internalInstance ? internalInstance._pendingState :
null;
this.props.stateListener(
from,
this.state && this.state.color,
pendingState && pendingState.color
);
}
},
setFavoriteColor: function(nextColor) {
this.setState({color: nextColor});
},
getInitialState: function() {
this.peekAtState('getInitialState');
return {color: 'red'};
},
render: function() {
this.peekAtState('render');
return <div>{this.state && this.state.color}</div>;
},
componentWillMount: function() {
this.peekAtState('componentWillMount-start');
this.setState({color: 'sunrise'});
this.peekAtState('componentWillMount-after-sunrise');
this.setState({color: 'orange'});
this.peekAtState('componentWillMount-end');
},
componentDidMount: function() {
this.peekAtState('componentDidMount-start');
this.setState({color: 'yellow'});
this.peekAtState('componentDidMount-end');
},
componentWillReceiveProps: function(newProps) {
this.peekAtState('componentWillReceiveProps-start');
if (newProps.nextColor) {
this.setState({color: newProps.nextColor});
}
this.peekAtState('componentWillReceiveProps-end');
},
shouldComponentUpdate: function(nextProps, nextState) {
this.peekAtState('shouldComponentUpdate-currentState');
this.peekAtState('shouldComponentUpdate-nextState', nextState);
return true;
},
componentWillUpdate: function(nextProps, nextState) {
this.peekAtState('componentWillUpdate-currentState');
this.peekAtState('componentWillUpdate-nextState', nextState);
},
componentDidUpdate: function(prevProps, prevState) {
this.peekAtState('componentDidUpdate-currentState');
this.peekAtState('componentDidUpdate-prevState', prevState);
},
componentWillUnmount: function() {
this.peekAtState('componentWillUnmount');
}
});
});
it('should support setting state', function() {
var container = document.createElement('div');
document.documentElement.appendChild(container);
var stateListener = mocks.getMockFunction();
var instance = <TestComponent stateListener={stateListener} />;
instance = React.render(instance, container);
instance.setProps({nextColor: 'green'});
instance.setFavoriteColor('blue');
instance.forceUpdate();
instance.replaceState(null);
React.unmountComponentAtNode(container);
expect(stateListener.mock.calls).toEqual([
// there is no state when getInitialState() is called
[ 'getInitialState', null, null ],
[ 'componentWillMount-start', 'red', null ],
// setState() only enqueues a pending state.
[ 'componentWillMount-after-sunrise', 'red', 'sunrise' ],
[ 'componentWillMount-end', 'red', 'orange' ],
// pending state has been applied
[ 'render', 'orange', null ],
[ 'componentDidMount-start', 'orange', null ],
// componentDidMount() called setState({color:'yellow'}), currently this
// occurs inline.
// In a future where setState() is async, this test result will change.
[ 'shouldComponentUpdate-currentState', 'orange', null ],
[ 'shouldComponentUpdate-nextState', 'yellow' ],
[ 'componentWillUpdate-currentState', 'orange', null ],
[ 'componentWillUpdate-nextState', 'yellow' ],
[ 'render', 'yellow', null ],
[ 'componentDidUpdate-currentState', 'yellow', null ],
[ 'componentDidUpdate-prevState', 'orange' ],
// componentDidMount() finally closes.
[ 'componentDidMount-end', 'yellow', null ],
[ 'componentWillReceiveProps-start', 'yellow', null ],
// setState({color:'green'}) only enqueues a pending state.
[ 'componentWillReceiveProps-end', 'yellow', 'green' ],
[ 'shouldComponentUpdate-currentState', 'yellow', null ],
[ 'shouldComponentUpdate-nextState', 'green' ],
[ 'componentWillUpdate-currentState', 'yellow', null ],
[ 'componentWillUpdate-nextState', 'green' ],
[ 'render', 'green', null ],
[ 'componentDidUpdate-currentState', 'green', null ],
[ 'componentDidUpdate-prevState', 'yellow' ],
// setFavoriteColor('blue')
[ 'shouldComponentUpdate-currentState', 'green', null ],
[ 'shouldComponentUpdate-nextState', 'blue' ],
[ 'componentWillUpdate-currentState', 'green', null ],
[ 'componentWillUpdate-nextState', 'blue' ],
[ 'render', 'blue', null ],
[ 'componentDidUpdate-currentState', 'blue', null ],
[ 'componentDidUpdate-prevState', 'green' ],
// forceUpdate()
[ 'componentWillUpdate-currentState', 'blue', null ],
[ 'componentWillUpdate-nextState', 'blue' ],
[ 'render', 'blue', null ],
[ 'componentDidUpdate-currentState', 'blue', null ],
[ 'componentDidUpdate-prevState', 'blue' ],
// replaceState(null)
[ 'shouldComponentUpdate-currentState', 'blue', null ],
[ 'shouldComponentUpdate-nextState', null ],
[ 'componentWillUpdate-currentState', 'blue', null ],
[ 'componentWillUpdate-nextState', null ],
[ 'render', null, null ],
[ 'componentDidUpdate-currentState', null, null ],
[ 'componentDidUpdate-prevState', 'blue' ],
// unmountComponent()
// state is available within `componentWillUnmount()`
[ 'componentWillUnmount', null, null ]
]);
});
});