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 0ca4365
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default function createWrapperComponent(node, options = {}) {
};
},

setChildProps(props) {
setChildProps(newProps) {
const props = Object.assign({}, this.state.props, newProps);

This comment has been minimized.

Copy link
@ljharb

ljharb Jan 7, 2016

Member

does enzyme pull in the es6-shim or shim the global? if not, we should probably use the object.assign module here, since Object.assign isn't in older nodes.

This comment has been minimized.

Copy link
@lelandrichardson

lelandrichardson Jan 7, 2016

Author Collaborator

Agreed. Will change.

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 0ca4365

Please sign in to comment.