forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathReactContextValidator-test.js
More file actions
292 lines (235 loc) · 7.74 KB
/
ReactContextValidator-test.js
File metadata and controls
292 lines (235 loc) · 7.74 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* 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
*/
// This test doesn't really have a good home yet. I'm leaving it here since this
// behavior belongs to the old propTypes system yet is currently implemented
// in the core ReactCompositeComponent. It should technically live in core's
// test suite but I'll leave it here to indicate that this is an issue that
// needs to be fixed.
'use strict';
var React;
var ReactDOM;
var ReactTestUtils;
describe('ReactContextValidator', () => {
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}
beforeEach(() => {
jest.resetModuleRegistry();
React = require('React');
ReactDOM = require('ReactDOM');
ReactTestUtils = require('ReactTestUtils');
});
// TODO: This behavior creates a runtime dependency on propTypes. We should
// ensure that this is not required for ES6 classes with Flow.
it('should filter out context not in contextTypes', () => {
var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string,
},
render: function() {
return <div />;
},
});
var ComponentInFooBarContext = React.createClass({
childContextTypes: {
foo: React.PropTypes.string,
bar: React.PropTypes.number,
},
getChildContext: function() {
return {
foo: 'abc',
bar: 123,
};
},
render: function() {
return <Component ref="child" />;
},
});
var instance = ReactTestUtils.renderIntoDocument(<ComponentInFooBarContext />);
expect(instance.refs.child.context).toEqual({foo: 'abc'});
});
it('should pass next context to lifecycles', () => {
var actualComponentWillReceiveProps;
var actualShouldComponentUpdate;
var actualComponentWillUpdate;
var Parent = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired,
bar: React.PropTypes.string.isRequired,
},
getChildContext: function() {
return {
foo: this.props.foo,
bar: 'bar',
};
},
render: function() {
return <Component />;
},
});
var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string,
},
componentWillReceiveProps: function(nextProps, nextContext) {
actualComponentWillReceiveProps = nextContext;
return true;
},
shouldComponentUpdate: function(nextProps, nextState, nextContext) {
actualShouldComponentUpdate = nextContext;
return true;
},
componentWillUpdate: function(nextProps, nextState, nextContext) {
actualComponentWillUpdate = nextContext;
},
render: function() {
return <div />;
},
});
var container = document.createElement('div');
ReactDOM.render(<Parent foo="abc" />, container);
ReactDOM.render(<Parent foo="def" />, container);
expect(actualComponentWillReceiveProps).toEqual({foo: 'def'});
expect(actualShouldComponentUpdate).toEqual({foo: 'def'});
expect(actualComponentWillUpdate).toEqual({foo: 'def'});
});
it('should pass previous context to lifecycles', () => {
var actualComponentDidUpdate;
var Parent = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired,
bar: React.PropTypes.string.isRequired,
},
getChildContext: function() {
return {
foo: this.props.foo,
bar: 'bar',
};
},
render: function() {
return <Component />;
},
});
var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string,
},
componentDidUpdate: function(prevProps, prevState, prevContext) {
actualComponentDidUpdate = prevContext;
},
render: function() {
return <div />;
},
});
var container = document.createElement('div');
ReactDOM.render(<Parent foo="abc" />, container);
ReactDOM.render(<Parent foo="def" />, container);
expect(actualComponentDidUpdate).toEqual({foo: 'abc'});
});
it('should check context types', () => {
spyOn(console, 'error');
var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string.isRequired,
},
render: function() {
return <div />;
},
});
ReactTestUtils.renderIntoDocument(<Component />);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Failed context type: ' +
'The context `foo` is marked as required in `Component`, but its value ' +
'is `undefined`.\n' +
' in Component (at **)'
);
var ComponentInFooStringContext = React.createClass({
childContextTypes: {
foo: React.PropTypes.string,
},
getChildContext: function() {
return {
foo: this.props.fooValue,
};
},
render: function() {
return <Component />;
},
});
ReactTestUtils.renderIntoDocument(
<ComponentInFooStringContext fooValue={'bar'} />
);
// Previous call should not error
expectDev(console.error.calls.count()).toBe(1);
var ComponentInFooNumberContext = React.createClass({
childContextTypes: {
foo: React.PropTypes.number,
},
getChildContext: function() {
return {
foo: this.props.fooValue,
};
},
render: function() {
return <Component />;
},
});
ReactTestUtils.renderIntoDocument(<ComponentInFooNumberContext fooValue={123} />);
expectDev(console.error.calls.count()).toBe(2);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: Failed context type: ' +
'Invalid context `foo` of type `number` supplied ' +
'to `Component`, expected `string`.\n' +
' in Component (at **)\n' +
' in ComponentInFooNumberContext (at **)'
);
});
it('should check child context types', () => {
spyOn(console, 'error');
var Component = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired,
bar: React.PropTypes.number,
},
getChildContext: function() {
return this.props.testContext;
},
render: function() {
return <div />;
},
});
ReactTestUtils.renderIntoDocument(<Component testContext={{bar: 123}} />);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Failed childContext type: ' +
'The child context `foo` is marked as required in `Component`, but its ' +
'value is `undefined`.\n' +
' in Component (at **)'
);
ReactTestUtils.renderIntoDocument(<Component testContext={{foo: 123}} />);
expectDev(console.error.calls.count()).toBe(2);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: Failed childContext type: ' +
'Invalid child context `foo` of type `number` ' +
'supplied to `Component`, expected `string`.\n' +
' in Component (at **)'
);
ReactTestUtils.renderIntoDocument(
<Component testContext={{foo: 'foo', bar: 123}} />
);
ReactTestUtils.renderIntoDocument(
<Component testContext={{foo: 'foo'}} />
);
// Previous calls should not log errors
expectDev(console.error.calls.count()).toBe(2);
});
});