Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React hooks test cases part 1 -- useLayoutEffect #2121

Merged
merged 1 commit into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PureComponent,
Suspense,
useEffect,
useLayoutEffect,
useState,
} from './_helpers/react-compat';
import {
Expand Down Expand Up @@ -973,7 +974,7 @@ describeWithDOM('mount', () => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, 100);
}, []);
return (
<div>
Expand All @@ -997,7 +998,41 @@ describeWithDOM('mount', () => {
</div>
</ComponentUsingEffectHook>`);
done();
}, 1e3);
}, 100);
});

it('works with `useLayoutEffect`', (done) => {
ljharb marked this conversation as resolved.
Show resolved Hide resolved
function ComponentUsingLayoutEffectHook() {
const [ctr, setCtr] = useState(0);
useLayoutEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 100);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = mount(<ComponentUsingLayoutEffectHook />);

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

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

Expand Down
38 changes: 35 additions & 3 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
PureComponent,
Suspense,
useEffect,
useLayoutEffect,
useState,
Profiler,
memo,
Expand Down Expand Up @@ -1131,13 +1132,44 @@ describe('shallow', () => {
describeIf(is('>= 16.8.5'), 'hooks', () => {
// TODO: enable when the shallow renderer fixes its bug
it.skip('works with `useEffect`', (done) => {
function ComponentUsingEffectHook() {
function ComponentUsingLayoutEffectHook() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, 100);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = shallow(<ComponentUsingLayoutEffectHook />);

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

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

// TODO: enable when https://github.com/facebook/react/issues/15275 is fixed
it.skip('works with `useLayoutEffect`', (done) => {
function ComponentUsingEffectHook() {
const [ctr, setCtr] = useState(0);
useLayoutEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 100);
}, []);
return (
<div>
Expand All @@ -1157,7 +1189,7 @@ describe('shallow', () => {
2
</div>`);
done();
}, 1e3);
}, 100);
});
});

Expand Down