Skip to content

Commit

Permalink
[Tests] add mount-passing, shallow-failing test for useEffect hook
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan authored and ljharb committed Mar 6, 2019
1 parent 47350d0 commit df0c326
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
forwardRef,
memo,
PureComponent,
useEffect,
useState,
} from './_helpers/react-compat';
import {
describeWithDOM,
Expand Down Expand Up @@ -648,6 +650,42 @@ describeWithDOM('mount', () => {
});
});

describeIf(is('>= 16.8'), 'hooks', () => {
it('works with `useEffect`', (done) => {
function ComponentUsingEffectHook() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = mount(<ComponentUsingEffectHook />);

expect(wrapper.debug()).to.equal(`<ComponentUsingEffectHook>
<div>
1
</div>
</ComponentUsingEffectHook>`);

setTimeout(() => {
wrapper.update();
expect(wrapper.debug()).to.equal(`<ComponentUsingEffectHook>
<div>
2
</div>
</ComponentUsingEffectHook>`);
done();
}, 1e3);
});
});

describe('.contains(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
Expand Down
35 changes: 35 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
forwardRef,
memo,
PureComponent,
useEffect,
useState,
} from './_helpers/react-compat';
import {
describeIf,
Expand Down Expand Up @@ -653,6 +655,39 @@ describe('shallow', () => {
});
});

describeIf(is('>= 16.8'), 'hooks', () => {
// TODO: enable when the shallow renderer fixes its bug
it.skip('works with `useEffect`', (done) => {
function ComponentUsingEffectHook() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = shallow(<ComponentUsingEffectHook />);

expect(wrapper.debug()).to.equal(`<div>
1
</div>`);

setTimeout(() => {
wrapper.update();
expect(wrapper.debug()).to.equal(`<div>
2
</div>`);
done();
}, 1e3);
});
});

describe('.contains(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
Expand Down

0 comments on commit df0c326

Please sign in to comment.