Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Mixin getDefaultProps before calling it
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuk committed Nov 12, 2016
1 parent 5b21fc5 commit 8a28f39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,19 @@ let findDOMNode = component => component && component.base || component;
function F(){}

function createClass(obj) {
let mixins = obj.mixins && collateMixins(obj.mixins);
obj = extend({}, obj);

function cl(props, context) {
extend(this, obj);
if (mixins) applyMixins(this, mixins);
Component.call(this, props, context, BYPASS_HOOK);
bindAll(this);
newComponentHook.call(this, props, context);
}

let mixins = obj.mixins && collateMixins(obj.mixins);
// We need to apply mixins here so that getDefaultProps is correctly mixed
if (mixins) applyMixins(obj, mixins);

if (obj.statics) {
extend(cl, obj.statics);
}
Expand Down
39 changes: 39 additions & 0 deletions test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,45 @@ describe('components', () => {
});

describe("mixins", () => {
describe("getDefaultProps", () => {
it('should combine the results', () => {
const Foo = React.createClass({
mixins: [
{ getDefaultProps: () => ({ a: true }) },
{ getDefaultProps: () => ({ b: true }) }
],
getDefaultProps() {
return { c: true };
},
render() {
return <div />;
}
});

expect(Foo.defaultProps).to.eql({
a: true,
b: true,
c: true
});
});

it('should throw an error for duplicate keys', () => {
expect(() => {
const Foo = React.createClass({
mixins: [
{ getDefaultProps: () => ({ a: true }) }
],
getDefaultProps() {
return { a: true };
},
render() {
return <div />;
}
});
}).to.throw();
});
});

describe("getInitialState", () => {
it('should combine the results', () => {
const Foo = React.createClass({
Expand Down

0 comments on commit 8a28f39

Please sign in to comment.