Skip to content

Commit 3d03798

Browse files
author
Swaroop SM
committed
Warn if the included mixin is undefined
1 parent 56c423a commit 3d03798

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Diff for: src/isomorphic/classic/class/ReactClass.js

+15
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,21 @@ function validateMethodOverride(isAlreadyDefined, name) {
437437
*/
438438
function mixSpecIntoComponent(Constructor, spec) {
439439
if (!spec) {
440+
if (__DEV__) {
441+
var typeofSpec = typeof spec;
442+
var isMixinValid = typeofSpec === 'object' && spec !== null;
443+
444+
warning(
445+
isMixinValid,
446+
'%s: You\'re attempting to include a mixin that is either null ' +
447+
'or not an object. Check the mixins included by the component, ' +
448+
'as well as any mixins they include themselves. ' +
449+
'Expected object but got %s.',
450+
Constructor.displayName || 'ReactClass',
451+
spec === null ? null : typeofSpec
452+
);
453+
}
454+
440455
return;
441456
}
442457

Diff for: src/isomorphic/classic/class/__tests__/ReactClassMixin-test.js

+88
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,94 @@ describe('ReactClass-mixin', function() {
310310
);
311311
});
312312

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+
313401
it('should throw if the mixin is a React component', function() {
314402
expect(function() {
315403
React.createClass({

0 commit comments

Comments
 (0)