Warn if the included mixin is undefined (#6158)
Swaroop SM committed
Jun 27, 2016 at 00:44 UTC
18bad0669f6ee698da40878adcf3e6a718d1e601
2 files changed
+103
src/isomorphic/classic/class/ReactClass.js
+15
@@ -434,6 +434,21 @@ function validateMethodOverride(isAlreadyDefined, name) {
434
*/
435
function mixSpecIntoComponent(Constructor, spec) {
436
if (!spec) {
437
+ if (__DEV__) {
438
+ var typeofSpec = typeof spec;
439
+ var isMixinValid = typeofSpec === 'object' && spec !== null;
440
+
441
+ warning(
442
+ isMixinValid,
443
+ '%s: You\'re attempting to include a mixin that is either null ' +
444
+ 'or not an object. Check the mixins included by the component, ' +
445
+ 'as well as any mixins they include themselves. ' +
446
+ 'Expected object but got %s.',
447
+ Constructor.displayName || 'ReactClass',
448
+ spec === null ? null : typeofSpec
449
+ );
450
+ }
451
+
452
return;
453
}
454
src/isomorphic/classic/class/__tests__/ReactClassMixin-test.js
+88
@@ -310,6 +310,94 @@ describe('ReactClass-mixin', function() {
310
);
311
});
312
313
+ it('should warn if the mixin is undefined', function() {
314
+ spyOn(console, 'error');
315
+
316
+ React.createClass({
317
+ mixins: [undefined],
318
+
319
+ render: function() {
320
+ return <span />;
321
+ },
322
+ });
323
+
324
+ expect(console.error.argsForCall.length).toBe(1);
325
+ expect(console.error.argsForCall[0][0]).toBe(
326
+ 'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
327
+ 'either null or not an object. Check the mixins included by the ' +
328
+ 'component, as well as any mixins they include themselves. ' +
329
+ 'Expected object but got undefined.'
330
+ );
331
+ });
332
+
333
+ it('should warn if the mixin is null', function() {
334
+ spyOn(console, 'error');
335
+
336
+ React.createClass({
337
+ mixins: [null],
338
+
339
+ render: function() {
340
+ return <span />;
341
+ },
342
+ });
343
+
344
+ expect(console.error.argsForCall.length).toBe(1);
345
+ expect(console.error.argsForCall[0][0]).toBe(
346
+ 'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
347
+ 'either null or not an object. Check the mixins included by the ' +
348
+ 'component, as well as any mixins they include themselves. ' +
349
+ 'Expected object but got null.'
350
+ );
351
+ });
352
+
353
+ it('should warn if an undefined mixin is included in another mixin', function() {
354
+ spyOn(console, 'error');
355
+
356
+ var mixinA = {
357
+ mixins: [undefined],
358
+ };
359
+
360
+ React.createClass({
361
+ mixins: [mixinA],
362
+
363
+ render: function() {
364
+ return <span />;
365
+ },
366
+ });
367
+
368
+ expect(console.error.argsForCall.length).toBe(1);
369
+ expect(console.error.argsForCall[0][0]).toBe(
370
+ 'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
371
+ 'either null or not an object. Check the mixins included by the ' +
372
+ 'component, as well as any mixins they include themselves. ' +
373
+ 'Expected object but got undefined.'
374
+ );
375
+ });
376
+
377
+ it('should warn if a null mixin is included in another mixin', function() {
378
+ spyOn(console, 'error');
379
+
380
+ var mixinA = {
381
+ mixins: [null],
382
+ };
383
+
384
+ React.createClass({
385
+ mixins: [mixinA],
386
+
387
+ render: function() {
388
+ return <span />;
389
+ },
390
+ });
391
+
392
+ expect(console.error.argsForCall.length).toBe(1);
393
+ expect(console.error.argsForCall[0][0]).toBe(
394
+ 'Warning: ReactClass: You\'re attempting to include a mixin that is ' +
395
+ 'either null or not an object. Check the mixins included by the ' +
396
+ 'component, as well as any mixins they include themselves. ' +
397
+ 'Expected object but got null.'
398
+ );
399
+ });
400
+
401
it('should throw if the mixin is a React component', function() {
402
expect(function() {
403
React.createClass({