From aa18606965db3059090472d766795b46bb90d443 Mon Sep 17 00:00:00 2001 From: Leland Richardson Date: Thu, 7 Jan 2016 11:36:00 -0800 Subject: [PATCH] Normalized setProps behavior between mount/shallow --- package.json | 1 + src/ReactWrapperComponent.jsx | 4 +++- src/__tests__/ReactWrapper-spec.js | 19 +++++++++++++++++++ src/__tests__/ShallowWrapper-spec.js | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d208a2f9d..e2e91d181 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "license": "MIT", "dependencies": { "cheerio": "^0.19.0", + "object.assign": "^4.0.3", "sinon": "^1.15.4", "underscore": "^1.8.3" }, diff --git a/src/ReactWrapperComponent.jsx b/src/ReactWrapperComponent.jsx index 57018316e..698477a72 100644 --- a/src/ReactWrapperComponent.jsx +++ b/src/ReactWrapperComponent.jsx @@ -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 @@ -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)); }, diff --git a/src/__tests__/ReactWrapper-spec.js b/src/__tests__/ReactWrapper-spec.js index 40dff38c7..6f6774fac 100644 --- a/src/__tests__/ReactWrapper-spec.js +++ b/src/__tests__/ReactWrapper-spec.js @@ -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 ( +
+ ); + } + } + + const wrapper = mount(); + 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)', () => { diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index b547ad3db..bc1837aa0 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -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 ( +
+ ); + } + } + + const wrapper = shallow(); + 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)', () => {