mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
fix: useUpdateEffect returns optional cleanup function
This commit is contained in:
parent
662403f5c3
commit
0ce421ced7
@ -5,7 +5,9 @@ const useUpdateEffect: typeof useEffect = (effect, deps) => {
|
||||
const isFirstMount = useFirstMountState();
|
||||
|
||||
useEffect(() => {
|
||||
!isFirstMount && effect();
|
||||
if (!isFirstMount) {
|
||||
return effect();
|
||||
}
|
||||
}, deps);
|
||||
};
|
||||
|
||||
|
||||
@ -1,13 +1,22 @@
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { useUpdateEffect } from '../src';
|
||||
|
||||
const mockEffectCleanup = jest.fn();
|
||||
const mockEffectCallback = jest.fn().mockReturnValue(mockEffectCleanup);
|
||||
|
||||
it('should run effect on update', () => {
|
||||
const { rerender } = renderHook(() => useUpdateEffect(mockEffectCallback));
|
||||
expect(mockEffectCallback).not.toHaveBeenCalled();
|
||||
const effect = jest.fn();
|
||||
|
||||
const { rerender } = renderHook(() => useUpdateEffect(effect));
|
||||
expect(effect).not.toHaveBeenCalled();
|
||||
|
||||
rerender();
|
||||
expect(mockEffectCallback).toHaveBeenCalledTimes(1);
|
||||
expect(effect).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should run cleanup on unmount', () => {
|
||||
const cleanup = jest.fn();
|
||||
const hook = renderHook(() => useUpdateEffect(cleanup));
|
||||
|
||||
hook.rerender();
|
||||
hook.unmount();
|
||||
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user