From 1c2c58b4e554f3b0c5f862f8de79f15a62bef5cf Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 26 Jun 2018 12:17:50 -0700 Subject: [PATCH] [Fix] `shallow`: `simulate`: ensure it returns itself. Also make mount simulate tests the same as shallow's. Fixes #1601 --- .../test/ReactWrapper-spec.jsx | 55 +++++++++++++++---- .../test/ShallowWrapper-spec.jsx | 5 ++ packages/enzyme/src/ReactWrapper.js | 4 +- packages/enzyme/src/ShallowWrapper.js | 1 + 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 971aa5a41..e058aed45 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -1500,6 +1500,21 @@ describeWithDOM('mount', () => { .to.throw(TypeError, "ReactWrapper::simulate() event 'invalidEvent' does not exist"); }); + describeIf(!REACT013, 'stateless function component', () => { + it('should pass in event data', () => { + const spy = sinon.spy(); + const Foo = () => ( + foo + ); + + const wrapper = mount(); + + wrapper.simulate('click', { someSpecialData: 'foo' }); + expect(spy.calledOnce).to.equal(true); + expect(spy.args[0][0].someSpecialData).to.equal('foo'); + }); + }); + describe('Normalizing JS event names', () => { it('should convert lowercase events to React camelcase', () => { const spy = sinon.spy(); @@ -1539,19 +1554,37 @@ describeWithDOM('mount', () => { }); }); - describeIf(!REACT013, 'stateless function component', () => { - it('should pass in event data', () => { - const spy = sinon.spy(); - const Foo = () => ( - foo - ); + it('should be batched updates', () => { + let renderCount = 0; + class Foo extends React.Component { + constructor(props) { + super(props); + this.state = { + count: 0, + }; + this.onClick = this.onClick.bind(this); + } + onClick() { + this.setState({ count: this.state.count + 1 }); + this.setState({ count: this.state.count + 1 }); + } + render() { + renderCount += 1; + return ( + {this.state.count} + ); + } + } - const wrapper = mount(); + const wrapper = mount(); + wrapper.simulate('click'); + expect(wrapper.text()).to.equal('1'); + expect(renderCount).to.equal(2); + }); - wrapper.simulate('click', { someSpecialData: 'foo' }); - expect(spy.calledOnce).to.equal(true); - expect(spy.args[0][0].someSpecialData).to.equal('foo'); - }); + it('chains', () => { + const wrapper = mount(
); + expect(wrapper.simulate('click')).to.equal(wrapper); }); }); diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 84c485acd..a663b45fd 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -1386,6 +1386,11 @@ describe('shallow', () => { expect(wrapper.text()).to.equal('1'); expect(renderCount).to.equal(2); }); + + it('chains', () => { + const wrapper = shallow(
); + expect(wrapper.simulate('click')).to.equal(wrapper); + }); }); describe('.setState(newState[, callback])', () => { diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index 96f5f117c..db7235169 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -573,11 +573,11 @@ class ReactWrapper { * @returns {ReactWrapper} */ simulate(event, mock = {}) { - this.single('simulate', (n) => { + return this.single('simulate', (n) => { this[RENDERER].simulateEvent(n, event, mock); this[ROOT].update(); + return this; }); - return this; } /** diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 8dfb9eb2d..ab6152d91 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -709,6 +709,7 @@ class ShallowWrapper { return this.single('simulate', (n) => { this[RENDERER].simulateEvent(n, event, ...args); this[ROOT].update(); + return this; }); }