Skip to content

Commit 1c2c58b

Browse files
committed
[Fix] shallow: simulate: ensure it returns itself.
Also make mount simulate tests the same as shallow's. Fixes #1601
1 parent 52561c5 commit 1c2c58b

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,21 @@ describeWithDOM('mount', () => {
15001500
.to.throw(TypeError, "ReactWrapper::simulate() event 'invalidEvent' does not exist");
15011501
});
15021502

1503+
describeIf(!REACT013, 'stateless function component', () => {
1504+
it('should pass in event data', () => {
1505+
const spy = sinon.spy();
1506+
const Foo = () => (
1507+
<a onClick={spy}>foo</a>
1508+
);
1509+
1510+
const wrapper = mount(<Foo />);
1511+
1512+
wrapper.simulate('click', { someSpecialData: 'foo' });
1513+
expect(spy.calledOnce).to.equal(true);
1514+
expect(spy.args[0][0].someSpecialData).to.equal('foo');
1515+
});
1516+
});
1517+
15031518
describe('Normalizing JS event names', () => {
15041519
it('should convert lowercase events to React camelcase', () => {
15051520
const spy = sinon.spy();
@@ -1539,19 +1554,37 @@ describeWithDOM('mount', () => {
15391554
});
15401555
});
15411556

1542-
describeIf(!REACT013, 'stateless function component', () => {
1543-
it('should pass in event data', () => {
1544-
const spy = sinon.spy();
1545-
const Foo = () => (
1546-
<a onClick={spy}>foo</a>
1547-
);
1557+
it('should be batched updates', () => {
1558+
let renderCount = 0;
1559+
class Foo extends React.Component {
1560+
constructor(props) {
1561+
super(props);
1562+
this.state = {
1563+
count: 0,
1564+
};
1565+
this.onClick = this.onClick.bind(this);
1566+
}
1567+
onClick() {
1568+
this.setState({ count: this.state.count + 1 });
1569+
this.setState({ count: this.state.count + 1 });
1570+
}
1571+
render() {
1572+
renderCount += 1;
1573+
return (
1574+
<a onClick={this.onClick}>{this.state.count}</a>
1575+
);
1576+
}
1577+
}
15481578

1549-
const wrapper = mount(<Foo />);
1579+
const wrapper = mount(<Foo />);
1580+
wrapper.simulate('click');
1581+
expect(wrapper.text()).to.equal('1');
1582+
expect(renderCount).to.equal(2);
1583+
});
15501584

1551-
wrapper.simulate('click', { someSpecialData: 'foo' });
1552-
expect(spy.calledOnce).to.equal(true);
1553-
expect(spy.args[0][0].someSpecialData).to.equal('foo');
1554-
});
1585+
it('chains', () => {
1586+
const wrapper = mount(<div />);
1587+
expect(wrapper.simulate('click')).to.equal(wrapper);
15551588
});
15561589
});
15571590

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,11 @@ describe('shallow', () => {
13861386
expect(wrapper.text()).to.equal('1');
13871387
expect(renderCount).to.equal(2);
13881388
});
1389+
1390+
it('chains', () => {
1391+
const wrapper = shallow(<div />);
1392+
expect(wrapper.simulate('click')).to.equal(wrapper);
1393+
});
13891394
});
13901395

13911396
describe('.setState(newState[, callback])', () => {

packages/enzyme/src/ReactWrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ class ReactWrapper {
573573
* @returns {ReactWrapper}
574574
*/
575575
simulate(event, mock = {}) {
576-
this.single('simulate', (n) => {
576+
return this.single('simulate', (n) => {
577577
this[RENDERER].simulateEvent(n, event, mock);
578578
this[ROOT].update();
579+
return this;
579580
});
580-
return this;
581581
}
582582

583583
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ class ShallowWrapper {
709709
return this.single('simulate', (n) => {
710710
this[RENDERER].simulateEvent(n, event, ...args);
711711
this[ROOT].update();
712+
return this;
712713
});
713714
}
714715

0 commit comments

Comments
 (0)