Skip to content

Commit

Permalink
Normalized setProps behavior between mount/shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Jan 7, 2016
1 parent e9fc572 commit aa18606
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"license": "MIT",
"dependencies": {
"cheerio": "^0.19.0",
"object.assign": "^4.0.3",
"sinon": "^1.15.4",
"underscore": "^1.8.3"
},
Expand Down
4 changes: 3 additions & 1 deletion src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PropTypes } from 'react';
import objectAssign from 'object.assign';

/**
* This is a utility component to wrap around the nodes we are
Expand Down Expand Up @@ -30,7 +31,8 @@ export default function createWrapperComponent(node, options = {}) {
};
},

setChildProps(props) {
setChildProps(newProps) {
const props = objectAssign({}, this.state.props, newProps);
return new Promise(resolve => this.setState({ props }, resolve));
},

Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ describeWithDOM('mount', () => {
expect(spy.calledWith(nextProps)).to.equal(true);
});

it('should merge newProps with oldProps', () => {
class Foo extends React.Component {
render() {
return (
<div {...this.props} />
);
}
}

const wrapper = mount(<Foo a="a" b="b" />);
expect(wrapper.props().a).to.equal('a');
expect(wrapper.props().b).to.equal('b');

wrapper.setProps({ b: 'c', d: 'e' });
expect(wrapper.props().a).to.equal('a');
expect(wrapper.props().b).to.equal('c');
expect(wrapper.props().d).to.equal('e');
});

});

describe('.setContext(newContext)', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,25 @@ describe('shallow', () => {
expect(spy.calledWith(nextProps)).to.equal(true);
});

it('should merge newProps with oldProps', () => {
class Foo extends React.Component {
render() {
return (
<div {...this.props} />
);
}
}

const wrapper = shallow(<Foo a="a" b="b" />);
expect(wrapper.props().a).to.equal('a');
expect(wrapper.props().b).to.equal('b');

wrapper.setProps({ b: 'c', d: 'e' });
expect(wrapper.props().a).to.equal('a');
expect(wrapper.props().b).to.equal('c');
expect(wrapper.props().d).to.equal('e');
});

});

describe('.setContext(newContext)', () => {
Expand Down

0 comments on commit aa18606

Please sign in to comment.