Skip to content

Commit d8ad8e8

Browse files
committed
Add same test for layout effects
1 parent 03a9fc8 commit d8ad8e8

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,6 +2858,54 @@ describe('ReactHooksWithNoopRenderer', () => {
28582858
]);
28592859
expect(ReactNoop.getChildren()).toEqual([span('OuterFallback')]);
28602860
});
2861+
2862+
it('assumes layout effect destroy function is either a function or undefined', () => {
2863+
function App(props) {
2864+
useLayoutEffect(() => {
2865+
return props.return;
2866+
});
2867+
return null;
2868+
}
2869+
2870+
const root1 = ReactNoop.createRoot();
2871+
expect(() =>
2872+
act(() => {
2873+
root1.render(<App return={17} />);
2874+
}),
2875+
).toErrorDev([
2876+
'Warning: An effect function must not return anything besides a ' +
2877+
'function, which is used for clean-up. You returned: 17',
2878+
]);
2879+
2880+
const root2 = ReactNoop.createRoot();
2881+
expect(() =>
2882+
act(() => {
2883+
root2.render(<App return={null} />);
2884+
}),
2885+
).toErrorDev([
2886+
'Warning: An effect function must not return anything besides a ' +
2887+
'function, which is used for clean-up. You returned null. If your ' +
2888+
'effect does not require clean up, return undefined (or nothing).',
2889+
]);
2890+
2891+
const root3 = ReactNoop.createRoot();
2892+
expect(() =>
2893+
act(() => {
2894+
root3.render(<App return={Promise.resolve()} />);
2895+
}),
2896+
).toErrorDev([
2897+
'Warning: An effect function must not return anything besides a ' +
2898+
'function, which is used for clean-up.\n\n' +
2899+
'It looks like you wrote useEffect(async () => ...) or returned a Promise.',
2900+
]);
2901+
2902+
// Error on unmount because React assumes the value is a function
2903+
expect(() =>
2904+
act(() => {
2905+
root3.unmount();
2906+
}),
2907+
).toThrow('is not a function');
2908+
});
28612909
});
28622910

28632911
describe('useCallback', () => {

0 commit comments

Comments
 (0)