Skip to content

Commit

Permalink
Warn if the included mixin is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaroop SM committed Apr 11, 2016
1 parent 56c423a commit 3d03798
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/isomorphic/classic/class/ReactClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,21 @@ function validateMethodOverride(isAlreadyDefined, name) {
*/
function mixSpecIntoComponent(Constructor, spec) {
if (!spec) {
if (__DEV__) {
var typeofSpec = typeof spec;
var isMixinValid = typeofSpec === 'object' && spec !== null;

warning(
isMixinValid,
'%s: 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 %s.',
Constructor.displayName || 'ReactClass',
spec === null ? null : typeofSpec
);
}

return;
}

Expand Down
88 changes: 88 additions & 0 deletions src/isomorphic/classic/class/__tests__/ReactClassMixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,94 @@ describe('ReactClass-mixin', function() {
);
});

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({
Expand Down

0 comments on commit 3d03798

Please sign in to comment.