forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactDOMProduction-test.js
196 lines (170 loc) · 4.93 KB
/
ReactDOMProduction-test.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright 2013-present, 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';
describe('ReactDOMProduction', () => {
var oldProcess;
var React;
var ReactDOM;
beforeEach(() => {
__DEV__ = false;
oldProcess = process;
global.process = {
env: Object.assign({}, process.env, {NODE_ENV: 'production'}),
};
jest.resetModuleRegistry();
React = require('React');
ReactDOM = require('ReactDOM');
});
afterEach(() => {
__DEV__ = true;
global.process = oldProcess;
});
it('should use prod fbjs', () => {
var warning = require('warning');
spyOn(console, 'error');
warning(false, 'Do cows go moo?');
expectDev(console.error.calls.count()).toBe(0);
});
it('should use prod React', () => {
spyOn(console, 'error');
// no key warning
void <div>{[<span />]}</div>;
expectDev(console.error.calls.count()).toBe(0);
});
it('should handle a simple flow', () => {
class Component extends React.Component {
render() {
return <span>{this.props.children}</span>;
}
}
var container = document.createElement('div');
var inst = ReactDOM.render(
<div className="blue">
<Component key={1}>A</Component>
<Component key={2}>B</Component>
<Component key={3}>C</Component>
</div>,
container
);
expect(container.firstChild).toBe(inst);
expect(inst.className).toBe('blue');
expect(inst.textContent).toBe('ABC');
ReactDOM.render(
<div className="red">
<Component key={2}>B</Component>
<Component key={1}>A</Component>
<Component key={3}>C</Component>
</div>,
container
);
expect(inst.className).toBe('red');
expect(inst.textContent).toBe('BAC');
ReactDOM.unmountComponentAtNode(container);
expect(container.childNodes.length).toBe(0);
});
it('should call lifecycle methods', () => {
var log = [];
class Component extends React.Component {
state = {y: 1};
shouldComponentUpdate(nextProps, nextState) {
log.push(['shouldComponentUpdate', nextProps, nextState]);
return nextProps.x !== this.props.x || nextState.y !== this.state.y;
}
componentWillMount() {
log.push(['componentWillMount']);
}
componentDidMount() {
log.push(['componentDidMount']);
}
componentWillReceiveProps(nextProps) {
log.push(['componentWillReceiveProps', nextProps]);
}
componentWillUpdate(nextProps, nextState) {
log.push(['componentWillUpdate', nextProps, nextState]);
}
componentDidUpdate(prevProps, prevState) {
log.push(['componentDidUpdate', prevProps, prevState]);
}
componentWillUnmount() {
log.push(['componentWillUnmount']);
}
render() {
log.push(['render']);
return null;
}
}
var container = document.createElement('div');
var inst = ReactDOM.render(
<Component x={1} />,
container
);
expect(log).toEqual([
['componentWillMount'],
['render'],
['componentDidMount'],
]);
log = [];
inst.setState({y: 2});
expect(log).toEqual([
['shouldComponentUpdate', {x: 1}, {y: 2}],
['componentWillUpdate', {x: 1}, {y: 2}],
['render'],
['componentDidUpdate', {x: 1}, {y: 1}],
]);
log = [];
inst.setState({y: 2});
expect(log).toEqual([
['shouldComponentUpdate', {x: 1}, {y: 2}],
]);
log = [];
ReactDOM.render(
<Component x={2} />,
container
);
expect(log).toEqual([
['componentWillReceiveProps', {x: 2}],
['shouldComponentUpdate', {x: 2}, {y: 2}],
['componentWillUpdate', {x: 2}, {y: 2}],
['render'],
['componentDidUpdate', {x: 1}, {y: 2}],
]);
log = [];
ReactDOM.render(
<Component x={2} />,
container
);
expect(log).toEqual([
['componentWillReceiveProps', {x: 2}],
['shouldComponentUpdate', {x: 2}, {y: 2}],
]);
log = [];
ReactDOM.unmountComponentAtNode(container);
expect(log).toEqual([
['componentWillUnmount'],
]);
});
it('should throw with an error code in production', () => {
expect(function() {
class Component extends React.Component {
render() {
return undefined;
}
}
var container = document.createElement('div');
ReactDOM.render(<Component />, container);
}).toThrowError(
'Minified React error #109; visit ' +
'http://facebook.github.io/react/docs/error-decoder.html?invariant=109&args[]=Component' +
' for the full message or use the non-minified dev environment' +
' for full errors and additional helpful warnings.'
);
});
});