From af189003f8fb79ac00e5a0787976c8dfcfd2a049 Mon Sep 17 00:00:00 2001 From: David Petersen Date: Thu, 23 May 2019 20:11:23 -0500 Subject: [PATCH] Wrap mounts simulate() in ReactTestUtils.act() Wraps the mount simulate method in ReactTestUtils.act() to correctly allow component interactions to be tested in a synchronous way Fixes #2084 --- .../src/ReactSixteenAdapter.js | 4 +++- .../test/ReactWrapper-spec.jsx | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js index 0fe85e834..afa65effa 100644 --- a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js +++ b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js @@ -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(); diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index f511ee28d..31867c086 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -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 ( + + ); + } + const wrapper = mount(); + + 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', () => {