Skip to content

Commit f7897d4

Browse files
chenesanljharb
authored andcommitted
[Tests] Add useCallback tests
`shallow`: Note that the `get same callback when using `useCallback` and rerender with same prop in dependencies` is skipped because react shallow renderer doesn't memoize callback function value. Pending facebook/react#15774
1 parent c108326 commit f7897d4

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

+41-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
Profiler,
3030
PureComponent,
3131
Suspense,
32+
useCallback,
3233
useEffect,
3334
useLayoutEffect,
3435
useMemo,
@@ -613,7 +614,7 @@ describeWithDOM('mount', () => {
613614
);
614615

615616
const context = { name: 'foo' };
616-
expect(() => mount(<SimpleComponent />, { context })).to.not.throw();
617+
expect(() => mount(<SimpleComponent />, { context })).not.to.throw();
617618
});
618619

619620
itIf(is('< 16'), 'is introspectable through context API', () => {
@@ -1069,7 +1070,45 @@ describeWithDOM('mount', () => {
10691070
const initialValue = wrapper.find(Child).prop('memoized');
10701071
wrapper.setProps({ relatedProp: '123' });
10711072
const nextValue = wrapper.find(Child).prop('memoized');
1072-
expect(initialValue).to.not.equal(nextValue);
1073+
expect(initialValue).not.to.equal(nextValue);
1074+
});
1075+
1076+
it('get same callback when using `useCallback` and rerender with same prop in dependencies', () => {
1077+
function Child() {
1078+
return null;
1079+
}
1080+
function ComponentUsingCallbackHook(props) {
1081+
const { onChange } = props;
1082+
const callback = useCallback(value => onChange(value), [onChange]);
1083+
return (
1084+
<Child callback={callback} />
1085+
);
1086+
}
1087+
const onChange = () => {};
1088+
const wrapper = mount(<ComponentUsingCallbackHook onChange={onChange} />);
1089+
const initialCallback = wrapper.find(Child).prop('callback');
1090+
wrapper.setProps({ unRelatedProp: '123' });
1091+
const nextCallback = wrapper.find(Child).prop('callback');
1092+
expect(initialCallback).to.equal(nextCallback);
1093+
});
1094+
1095+
it('get different callback when using `useCallback` and rerender with different prop in dependencies', () => {
1096+
function Child() {
1097+
return null;
1098+
}
1099+
function ComponentUsingCallbackHook(props) {
1100+
const { onChange, relatedProp } = props;
1101+
const callback = useCallback(value => onChange(value), [onChange, relatedProp]);
1102+
return (
1103+
<Child callback={callback} />
1104+
);
1105+
}
1106+
const onChange = () => {};
1107+
const wrapper = mount(<ComponentUsingCallbackHook onChange={onChange} relatedProp="456" />);
1108+
const initialCallback = wrapper.find(Child).prop('callback');
1109+
wrapper.setProps({ relatedProp: '123' });
1110+
const nextCallback = wrapper.find(Child).prop('callback');
1111+
expect(initialCallback).not.to.equal(nextCallback);
10731112
});
10741113
});
10751114

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

+45-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
lazy,
2727
PureComponent,
2828
Suspense,
29+
useCallback,
2930
useEffect,
3031
useMemo,
3132
useLayoutEffect,
@@ -412,7 +413,7 @@ describe('shallow', () => {
412413
});
413414

414415
const context = { name: 'foo' };
415-
expect(() => shallow(<SimpleComponent />, { context })).to.not.throw();
416+
expect(() => shallow(<SimpleComponent />, { context })).not.to.throw();
416417
});
417418

418419
it('is introspectable through context API', () => {
@@ -1214,7 +1215,7 @@ describe('shallow', () => {
12141215

12151216
it('get same value when using `useMemo` and rerender with same prop in dependencies', () => {
12161217
function Child() {
1217-
return false;
1218+
return null;
12181219
}
12191220
function ComponentUsingMemoHook(props) {
12201221
const { count } = props;
@@ -1232,7 +1233,7 @@ describe('shallow', () => {
12321233

12331234
it('get different value when using `useMemo` and rerender with different prop in dependencies', () => {
12341235
function Child() {
1235-
return false;
1236+
return null;
12361237
}
12371238
function ComponentUsingMemoHook(props) {
12381239
const { count, relatedProp } = props;
@@ -1245,7 +1246,45 @@ describe('shallow', () => {
12451246
const initialValue = wrapper.find(Child).prop('memoized');
12461247
wrapper.setProps({ relatedProp: '123' });
12471248
const nextValue = wrapper.find(Child).prop('memoized');
1248-
expect(initialValue).to.not.equal(nextValue);
1249+
expect(initialValue).not.to.equal(nextValue);
1250+
});
1251+
1252+
it.skip('get same callback when using `useCallback` and rerender with same prop in dependencies', () => {
1253+
function Child() {
1254+
return false;
1255+
}
1256+
function ComponentUsingCallbackHook(props) {
1257+
const { onChange } = props;
1258+
const callback = useCallback(value => onChange(value), [onChange]);
1259+
return (
1260+
<Child callback={callback} />
1261+
);
1262+
}
1263+
const onChange = () => { };
1264+
const wrapper = shallow(<ComponentUsingCallbackHook onChange={onChange} />);
1265+
const initialCallback = wrapper.find(Child).prop('callback');
1266+
wrapper.setProps({ unRelatedProp: '123' });
1267+
const nextCallback = wrapper.find(Child).prop('callback');
1268+
expect(initialCallback).to.equal(nextCallback);
1269+
});
1270+
1271+
it('get different callback when using `useCallback` and rerender with different prop in dependencies', () => {
1272+
function Child() {
1273+
return false;
1274+
}
1275+
function ComponentUsingCallbackHook(props) {
1276+
const { onChange, relatedProp } = props;
1277+
const callback = useCallback(value => onChange(value), [onChange, relatedProp]);
1278+
return (
1279+
<Child callback={callback} />
1280+
);
1281+
}
1282+
const onChange = () => { };
1283+
const wrapper = shallow(<ComponentUsingCallbackHook onChange={onChange} relatedProp="456" />);
1284+
const initialCallback = wrapper.find(Child).prop('callback');
1285+
wrapper.setProps({ relatedProp: '123' });
1286+
const nextCallback = wrapper.find(Child).prop('callback');
1287+
expect(initialCallback).not.to.equal(nextCallback);
12491288
});
12501289
});
12511290

@@ -1399,7 +1438,7 @@ describe('shallow', () => {
13991438

14001439
const context = { name: 'foo' };
14011440
const wrapper = shallow(<Foo />);
1402-
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw();
1441+
expect(() => wrapper.find(Bar).shallow({ context })).not.to.throw();
14031442
});
14041443

14051444
it('is introspectable through context API', () => {
@@ -1501,7 +1540,7 @@ describe('shallow', () => {
15011540

15021541
const context = { name: 'foo' };
15031542
const wrapper = shallow(<Foo />);
1504-
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw();
1543+
expect(() => wrapper.find(Bar).shallow({ context })).not.to.throw();
15051544
});
15061545

15071546
itIf(is('< 16'), 'is introspectable through context API', () => {

0 commit comments

Comments
 (0)