Skip to content

Commit

Permalink
[Fix] shallow: simulate: ensure it returns itself.
Browse files Browse the repository at this point in the history
Also make mount simulate tests the same as shallow's.

Fixes #1601
  • Loading branch information
ljharb committed Jun 26, 2018
1 parent 52561c5 commit 1c2c58b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
55 changes: 44 additions & 11 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => (
<a onClick={spy}>foo</a>
);

const wrapper = mount(<Foo />);

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();
Expand Down Expand Up @@ -1539,19 +1554,37 @@ describeWithDOM('mount', () => {
});
});

describeIf(!REACT013, 'stateless function component', () => {
it('should pass in event data', () => {
const spy = sinon.spy();
const Foo = () => (
<a onClick={spy}>foo</a>
);
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 (
<a onClick={this.onClick}>{this.state.count}</a>
);
}
}

const wrapper = mount(<Foo />);
const wrapper = mount(<Foo />);
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(<div />);
expect(wrapper.simulate('click')).to.equal(wrapper);
});
});

Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,11 @@ describe('shallow', () => {
expect(wrapper.text()).to.equal('1');
expect(renderCount).to.equal(2);
});

it('chains', () => {
const wrapper = shallow(<div />);
expect(wrapper.simulate('click')).to.equal(wrapper);
});
});

describe('.setState(newState[, callback])', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ class ShallowWrapper {
return this.single('simulate', (n) => {
this[RENDERER].simulateEvent(n, event, ...args);
this[ROOT].update();
return this;
});
}

Expand Down

0 comments on commit 1c2c58b

Please sign in to comment.