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 mocks = require('mocks');
15
16
var React;
17
var ReactTestUtils;
18
var reactComponentExpect;
19
20
var TestComponent;
21
22
describe('ReactCompositeComponent-state', function() {
23
24
beforeEach(function() {
25
React = require('React');
26
ReactTestUtils = require('ReactTestUtils');
27
reactComponentExpect = require('reactComponentExpect');
28
29
TestComponent = React.createClass({
30
peekAtState: function(from, state) {
31
if (state) {
32
this.props.stateListener(from, state && state.color);
33
} else {
34
this.props.stateListener(
35
from,
36
this.state && this.state.color,
37
this._pendingState && this._pendingState.color
38
);
39
}
40
},
41
42
setFavoriteColor: function(nextColor) {
43
this.setState({color: nextColor});
44
},
45
46
getInitialState: function() {
47
this.peekAtState('getInitialState');
48
return {color: 'red'};
49
},
50
51
render: function() {
52
this.peekAtState('render');
53
return <div>{this.state.color}</div>;
54
},
55
56
componentWillMount: function() {
57
this.peekAtState('componentWillMount-start');
58
this.setState({color: 'sunrise'});
59
this.peekAtState('componentWillMount-after-sunrise');
60
this.setState({color: 'orange'});
61
this.peekAtState('componentWillMount-end');
62
},
63
64
componentDidMount: function() {
65
this.peekAtState('componentDidMount-start');
66
this.setState({color: 'yellow'});
67
this.peekAtState('componentDidMount-end');
68
},
69
70
componentWillReceiveProps: function(newProps) {
71
this.peekAtState('componentWillReceiveProps-start');
72
if (newProps.nextColor) {
73
this.setState({color: newProps.nextColor});
74
}
75
this.peekAtState('componentWillReceiveProps-end');
76
},
77
78
shouldComponentUpdate: function(nextProps, nextState) {
79
this.peekAtState('shouldComponentUpdate-currentState');
80
this.peekAtState('shouldComponentUpdate-nextState', nextState);
81
return true;
82
},
83
84
componentWillUpdate: function(nextProps, nextState) {
85
this.peekAtState('componentWillUpdate-currentState');
86
this.peekAtState('componentWillUpdate-nextState', nextState);
87
},
88
89
componentDidUpdate: function(prevProps, prevState) {
90
this.peekAtState('componentDidUpdate-currentState');
91
this.peekAtState('componentDidUpdate-prevState', prevState);
92
},
93
94
componentWillUnmount: function() {
95
this.peekAtState('componentWillUnmount');
96
}
97
});
98
99
});
100
101
it('should support setting state', function() {
102
var stateListener = mocks.getMockFunction();
103
var instance = <TestComponent stateListener={stateListener} />;
104
instance = ReactTestUtils.renderIntoDocument(instance);
105
instance.setProps({nextColor: 'green'});
106
instance.setFavoriteColor('blue');
107
instance.forceUpdate();
108
instance.unmountComponent();
109
110
expect(stateListener.mock.calls).toEqual([
111
// there is no state when getInitialState() is called
112
[ 'getInitialState', null, null ],
113
[ 'componentWillMount-start', 'red', null ],
114
// setState() only enqueues a pending state.
115
[ 'componentWillMount-after-sunrise', 'red', 'sunrise' ],
116
[ 'componentWillMount-end', 'red', 'orange' ],
117
// pending state has been applied
118
[ 'render', 'orange', null ],
119
[ 'componentDidMount-start', 'orange', null ],
120
// componentDidMount() called setState({color:'yellow'}), currently this
121
// occurs inline.
122
// In a future where setState() is async, this test result will change.
123
[ 'shouldComponentUpdate-currentState', 'orange', null ],
124
[ 'shouldComponentUpdate-nextState', 'yellow' ],
125
[ 'componentWillUpdate-currentState', 'orange', null ],
126
[ 'componentWillUpdate-nextState', 'yellow' ],
127
[ 'render', 'yellow', null ],
128
[ 'componentDidUpdate-currentState', 'yellow', null ],
129
[ 'componentDidUpdate-prevState', 'orange' ],
130
// componentDidMount() finally closes.
131
[ 'componentDidMount-end', 'yellow', null ],
132
[ 'componentWillReceiveProps-start', 'yellow', null ],
133
// setState({color:'green'}) only enqueues a pending state.
134
[ 'componentWillReceiveProps-end', 'yellow', 'green' ],
135
[ 'shouldComponentUpdate-currentState', 'yellow', null ],
136
[ 'shouldComponentUpdate-nextState', 'green' ],
137
[ 'componentWillUpdate-currentState', 'yellow', null ],
138
[ 'componentWillUpdate-nextState', 'green' ],
139
[ 'render', 'green', null ],
140
[ 'componentDidUpdate-currentState', 'green', null ],
141
[ 'componentDidUpdate-prevState', 'yellow' ],
142
// setFavoriteColor('blue')
143
[ 'shouldComponentUpdate-currentState', 'green', null ],
144
[ 'shouldComponentUpdate-nextState', 'blue' ],
145
[ 'componentWillUpdate-currentState', 'green', null ],
146
[ 'componentWillUpdate-nextState', 'blue' ],
147
[ 'render', 'blue', null ],
148
[ 'componentDidUpdate-currentState', 'blue', null ],
149
[ 'componentDidUpdate-prevState', 'green' ],
150
// forceUpdate()
151
[ 'componentWillUpdate-currentState', 'blue', null ],
152
[ 'componentWillUpdate-nextState', 'blue' ],
153
[ 'render', 'blue', null ],
154
[ 'componentDidUpdate-currentState', 'blue', null ],
155
[ 'componentDidUpdate-prevState', 'blue' ],
156
// unmountComponent()
157
// state is available within `componentWillUnmount()`
158
[ 'componentWillUnmount', 'blue', null ]
159
]);
160
});
161
});
162
163