Skip to content

Commit

Permalink
[Tests] Add useMemo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan authored and ljharb committed May 29, 2019
1 parent d824527 commit c108326
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
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

0 comments on commit c108326

Please sign in to comment.