Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [fix] mount: wrap simulate() in ReactTest…
Browse files Browse the repository at this point in the history
…Utils.act()

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 authored and ljharb committed May 24, 2019
1 parent 78034b1 commit aedb208
Show file tree
Hide file tree
Showing 2 changed files with 30 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
27 changes: 27 additions & 0 deletions packages/enzyme-test-suite/test/shared/methods/simulate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {

import {
memo,
useEffect,
useState,
} from '../../_helpers/react-compat';

export default function describeSimulate({
Expand Down Expand Up @@ -316,5 +318,30 @@ export default function describeSimulate({
expect(handleClick).to.have.property('callCount', 3);
});
});

describeIf(is('>= 16.8'), 'hooks', () => {
// TODO: fix for shallow when useEffect works for shallow
itIf(!isShallow, 'works with `useEffect` simulated events', () => {
const effectSpy = sinon.spy();
function ComponentUsingEffectHook() {
useEffect(effectSpy);
const [counter, setCounter] = useState(0);

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

const button = wrapper.find('button');
expect(button.text()).to.equal('0');
expect(effectSpy).to.have.property('callCount', 1);

button.simulate('click');

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

0 comments on commit aedb208

Please sign in to comment.