Skip to content

Commit

Permalink
Warn if the included mixin is undefined (#6158)
Browse files Browse the repository at this point in the history
(cherry picked from commit 18bad06)
  • Loading branch information
swaroopsm authored and zpao committed Jul 13, 2016
1 parent 3b80d4d commit 9d385ef
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 @@ -436,6 +436,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 9d385ef

Please sign in to comment.