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 case part 2 -- useMemo #2144

Merged
merged 1 commit into from
May 29, 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
37 changes: 37 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,7 @@ import {
Suspense,
useEffect,
useLayoutEffect,
useMemo,
useState,
} from './_helpers/react-compat';
import {
Expand Down Expand Up @@ -1034,6 +1035,42 @@ describeWithDOM('mount', () => {
done();
}, 100);
});

it('get same value when using `useMemo` and rerender with same prop in dependencies', () => {
function Child() {
return false;
}
function ComponentUsingMemoHook(props) {
const { count } = props;
const memoized = useMemo(() => ({ result: count * 2 }), [count]);
return (
<Child memoized={memoized} />
);
}
const wrapper = mount(<ComponentUsingMemoHook count={1} />);
const initialValue = wrapper.find(Child).prop('memoized');
wrapper.setProps({ unRelatedProp: '123' });
const nextValue = wrapper.find(Child).prop('memoized');
expect(initialValue).to.equal(nextValue);
});

it('get different value when using `useMemo` and rerender with different prop in dependencies', () => {
function Child() {
return false;
}
function ComponentUsingMemoHook(props) {
const { count, relatedProp } = props;
const memoized = useMemo(() => ({ result: count * 2 }), [count, relatedProp]);
return (
<Child memoized={memoized} relatedProp={relatedProp} />
);
}
const wrapper = mount(<ComponentUsingMemoHook relatedProp="456" count={1} />);
const initialValue = wrapper.find(Child).prop('memoized');
wrapper.setProps({ relatedProp: '123' });
const nextValue = wrapper.find(Child).prop('memoized');
expect(initialValue).to.not.equal(nextValue);
});
});

itIf(is('>= 16.2'), 'supports fragments', () => {
Expand Down
37 changes: 37 additions & 0 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,
useMemo,
useLayoutEffect,
useState,
Profiler,
Expand Down Expand Up @@ -1210,6 +1211,42 @@ describe('shallow', () => {
done();
}, 100);
});

it('get same value when using `useMemo` and rerender with same prop in dependencies', () => {
function Child() {
return false;
}
function ComponentUsingMemoHook(props) {
const { count } = props;
const memoized = useMemo(() => ({ result: count * 2 }), [count]);
return (
<Child memoized={memoized} />
);
}
const wrapper = shallow(<ComponentUsingMemoHook count={1} />);
const initialValue = wrapper.find(Child).prop('memoized');
wrapper.setProps({ unRelatedProp: '123' });
const nextValue = wrapper.find(Child).prop('memoized');
expect(initialValue).to.equal(nextValue);
});

it('get different value when using `useMemo` and rerender with different prop in dependencies', () => {
function Child() {
return false;
}
function ComponentUsingMemoHook(props) {
const { count, relatedProp } = props;
const memoized = useMemo(() => ({ result: count * 2 }), [count, relatedProp]);
return (
<Child memoized={memoized} relatedProp={relatedProp} />
);
}
const wrapper = shallow(<ComponentUsingMemoHook relatedProp="456" count={1} />);
const initialValue = wrapper.find(Child).prop('memoized');
wrapper.setProps({ relatedProp: '123' });
const nextValue = wrapper.find(Child).prop('memoized');
expect(initialValue).to.not.equal(nextValue);
});
});

itIf(is('>= 16.2'), 'does not support fragments', () => {
Expand Down