Skip to content

Commit

Permalink
Wrap mounts simulate() in ReactTestUtils.act()
Browse files Browse the repository at this point in the history
Wraps the mount simulate method in ReactTestUtils.act() to correctly
allow component interactions to be tested in a synchronous way

Fixes #2084
  • Loading branch information
petersendidit committed May 24, 2019
1 parent 78034b1 commit af18900
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ class ReactSixteenAdapter extends EnzymeAdapter {
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
}
eventFn(adapter.nodeToHostNode(node), mock);
wrapAct(() => {
eventFn(adapter.nodeToHostNode(node), mock);
});
},
batchedUpdates(fn) {
return fn();
Expand Down
19 changes: 19 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,25 @@ describeWithDOM('mount', () => {
done();
}, 100);
});

it('works with `useEffect` simulated events', () => {
const effectSpy = sinon.spy();
function ComponentUsingEffectHook() {
useEffect(effectSpy);
const [counter, setCounter] = useState(0);

return (
<button onClick={() => setCounter(counter + 1)}>{counter}</button>
);
}
const wrapper = mount(<ComponentUsingEffectHook />);

const button = wrapper.find('button');
button.simulate('click');

expect(button.text()).to.equal('1');
expect(effectSpy).to.have.property('callCount', 2);
});
});

itIf(is('>= 16.2'), 'supports fragments', () => {
Expand Down

0 comments on commit af18900

Please sign in to comment.