forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactClassMixin-test.js
545 lines (474 loc) · 13.9 KB
/
ReactClassMixin-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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
* 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';
var React;
var ReactTestUtils;
var TestComponent;
var TestComponentWithPropTypes;
var TestComponentWithReverseSpec;
var mixinPropValidator;
var componentPropValidator;
describe('ReactClass-mixin', function() {
beforeEach(function() {
React = require('React');
ReactTestUtils = require('ReactTestUtils');
mixinPropValidator = jest.genMockFn();
componentPropValidator = jest.genMockFn();
var MixinA = {
propTypes: {
propA: function() {},
},
componentDidMount: function() {
this.props.listener('MixinA didMount');
},
};
var MixinB = {
mixins: [MixinA],
propTypes: {
propB: function() {},
},
componentDidMount: function() {
this.props.listener('MixinB didMount');
},
};
var MixinBWithReverseSpec = {
componentDidMount: function() {
this.props.listener('MixinBWithReverseSpec didMount');
},
mixins: [MixinA],
};
var MixinC = {
statics: {
staticC: function() {},
},
componentDidMount: function() {
this.props.listener('MixinC didMount');
},
};
var MixinD = {
propTypes: {
value: mixinPropValidator,
},
};
TestComponent = React.createClass({
mixins: [MixinB, MixinC, MixinD],
statics: {
staticComponent: function() {},
},
propTypes: {
propComponent: function() {},
},
componentDidMount: function() {
this.props.listener('Component didMount');
},
render: function() {
return <div />;
},
});
TestComponentWithReverseSpec = React.createClass({
render: function() {
return <div />;
},
componentDidMount: function() {
this.props.listener('Component didMount');
},
mixins: [MixinBWithReverseSpec, MixinC, MixinD],
});
TestComponentWithPropTypes = React.createClass({
mixins: [MixinD],
propTypes: {
value: componentPropValidator,
},
render: function() {
return <div />;
},
});
});
it('should support merging propTypes and statics', function() {
var listener = jest.genMockFn();
var instance = <TestComponent listener={listener} />;
instance = ReactTestUtils.renderIntoDocument(instance);
var instancePropTypes = instance.constructor.propTypes;
expect('propA' in instancePropTypes).toBe(true);
expect('propB' in instancePropTypes).toBe(true);
expect('propComponent' in instancePropTypes).toBe(true);
expect('staticC' in TestComponent).toBe(true);
expect('staticComponent' in TestComponent).toBe(true);
});
it('should support chaining delegate functions', function() {
var listener = jest.genMockFn();
var instance = <TestComponent listener={listener} />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(listener.mock.calls).toEqual([
['MixinA didMount'],
['MixinB didMount'],
['MixinC didMount'],
['Component didMount'],
]);
});
it('should chain functions regardless of spec property order', function() {
var listener = jest.genMockFn();
var instance = <TestComponentWithReverseSpec listener={listener} />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(listener.mock.calls).toEqual([
['MixinA didMount'],
['MixinBWithReverseSpec didMount'],
['MixinC didMount'],
['Component didMount'],
]);
});
it('should validate prop types via mixins', function() {
expect(TestComponent.propTypes).toBeDefined();
expect(TestComponent.propTypes.value)
.toBe(mixinPropValidator);
});
it('should override mixin prop types with class prop types', function() {
// Sanity check...
expect(componentPropValidator).toNotBe(mixinPropValidator);
// Actually check...
expect(TestComponentWithPropTypes.propTypes)
.toBeDefined();
expect(TestComponentWithPropTypes.propTypes.value)
.toNotBe(mixinPropValidator);
expect(TestComponentWithPropTypes.propTypes.value)
.toBe(componentPropValidator);
});
it('should support mixins with getInitialState()', function() {
var Mixin = {
getInitialState: function() {
return {mixin: true};
},
};
var Component = React.createClass({
mixins: [Mixin],
getInitialState: function() {
return {component: true};
},
render: function() {
return <span />;
},
});
var instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state.component).toBe(true);
expect(instance.state.mixin).toBe(true);
});
it('should throw with conflicting getInitialState() methods', function() {
var Mixin = {
getInitialState: function() {
return {x: true};
},
};
var Component = React.createClass({
mixins: [Mixin],
getInitialState: function() {
return {x: true};
},
render: function() {
return <span />;
},
});
var instance = <Component />;
expect(function() {
instance = ReactTestUtils.renderIntoDocument(instance);
}).toThrow(
'mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the ' +
'same key: `x`. This conflict may be due to a mixin; in particular, ' +
'this may be caused by two getInitialState() or getDefaultProps() ' +
'methods returning objects with clashing keys.'
);
});
it('should not mutate objects returned by getInitialState()', function() {
var Mixin = {
getInitialState: function() {
return Object.freeze({mixin: true});
},
};
var Component = React.createClass({
mixins: [Mixin],
getInitialState: function() {
return Object.freeze({component: true});
},
render: function() {
return <span />;
},
});
expect(() => {
ReactTestUtils.renderIntoDocument(<Component />);
}).not.toThrow();
});
it('should support statics in mixins', function() {
var Mixin = {
statics: {
foo: 'bar',
},
};
var Component = React.createClass({
mixins: [Mixin],
statics: {
abc: 'def',
},
render: function() {
return <span />;
},
});
var instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.constructor.foo).toBe('bar');
expect(Component.foo).toBe('bar');
expect(instance.constructor.abc).toBe('def');
expect(Component.abc).toBe('def');
});
it("should throw if mixins override each others' statics", function() {
expect(function() {
var Mixin = {
statics: {
abc: 'foo',
},
};
React.createClass({
mixins: [Mixin],
statics: {
abc: 'bar',
},
render: function() {
return <span />;
},
});
}).toThrow(
'ReactClass: You are attempting to define `abc` on your component more ' +
'than once. This conflict may be due to a mixin.'
);
});
it('should throw if mixins override functions in statics', function() {
expect(function() {
var Mixin = {
statics: {
abc: function() {
console.log('foo');
},
},
};
React.createClass({
mixins: [Mixin],
statics: {
abc: function() {
console.log('bar');
},
},
render: function() {
return <span />;
},
});
}).toThrow(
'ReactClass: You are attempting to define `abc` on your component ' +
'more than once. This conflict may be due to a mixin.'
);
});
it('should warn if the mixin is undefined', function() {
spyOn(console, 'error');
React.createClass({
mixins: [undefined],
render: function() {
return <span />;
},
});
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
'either null or not an object. Check the mixins included by the ' +
'component, as well as any mixins they include themselves. ' +
'Expected object but got undefined.'
);
});
it('should warn if the mixin is null', function() {
spyOn(console, 'error');
React.createClass({
mixins: [null],
render: function() {
return <span />;
},
});
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
'either null or not an object. Check the mixins included by the ' +
'component, as well as any mixins they include themselves. ' +
'Expected object but got null.'
);
});
it('should warn if an undefined mixin is included in another mixin', function() {
spyOn(console, 'error');
var mixinA = {
mixins: [undefined],
};
React.createClass({
mixins: [mixinA],
render: function() {
return <span />;
},
});
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
'either null or not an object. Check the mixins included by the ' +
'component, as well as any mixins they include themselves. ' +
'Expected object but got undefined.'
);
});
it('should warn if a null mixin is included in another mixin', function() {
spyOn(console, 'error');
var mixinA = {
mixins: [null],
};
React.createClass({
mixins: [mixinA],
render: function() {
return <span />;
},
});
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
'either null or not an object. Check the mixins included by the ' +
'component, as well as any mixins they include themselves. ' +
'Expected object but got null.'
);
});
it('should throw if the mixin is a React component', function() {
expect(function() {
React.createClass({
mixins: [<div />],
render: function() {
return <span />;
},
});
}).toThrow(
'ReactClass: You\'re attempting to use a component as a mixin. ' +
'Instead, just use a regular object.'
);
});
it('should throw if the mixin is a React component class', function() {
expect(function() {
var Component = React.createClass({
render: function() {
return <span />;
},
});
React.createClass({
mixins: [Component],
render: function() {
return <span />;
},
});
}).toThrow(
'ReactClass: You\'re attempting to use a component class or function ' +
'as a mixin. Instead, just use a regular object.'
);
});
it('should have bound the mixin methods to the component', function() {
var mixin = {
mixinFunc: function() {
return this;
},
};
var Component = React.createClass({
mixins: [mixin],
componentDidMount: function() {
expect(this.mixinFunc()).toBe(this);
},
render: function() {
return <span />;
},
});
var instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
});
it('should include the mixin keys in even if their values are falsy', function() {
var mixin = {
keyWithNullValue: null,
randomCounter: 0,
};
var Component = React.createClass({
mixins: [mixin],
componentDidMount: function() {
expect(this.randomCounter).toBe(0);
expect(this.keyWithNullValue).toBeNull();
},
render: function() {
return <span />;
},
});
var instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
});
it('should work with a null getInitialState return value and a mixin', () => {
var Component;
var instance;
var Mixin = {
getInitialState: function() {
return {foo: 'bar'};
},
};
Component = React.createClass({
mixins: [Mixin],
getInitialState: function() {
return null;
},
render: function() {
return <span />;
},
});
expect(
() => ReactTestUtils.renderIntoDocument(<Component />)
).not.toThrow();
instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'bar'});
// Also the other way round should work
var Mixin2 = {
getInitialState: function() {
return null;
},
};
Component = React.createClass({
mixins: [Mixin2],
getInitialState: function() {
return {foo: 'bar'};
},
render: function() {
return <span />;
},
});
expect(
() => ReactTestUtils.renderIntoDocument(<Component />)
).not.toThrow();
instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'bar'});
// Multiple mixins should be fine too
Component = React.createClass({
mixins: [Mixin, Mixin2],
getInitialState: function() {
return {x: true};
},
render: function() {
return <span />;
},
});
expect(
() => ReactTestUtils.renderIntoDocument(<Component />)
).not.toThrow();
instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'bar', x: true});
});
});