react-use/tests/useUpdateEffect.test.ts
2020-01-07 19:34:57 +01:00

24 lines
614 B
TypeScript

import { renderHook } from '@testing-library/react-hooks';
import { useUpdateEffect } from '../src';
it('should run effect on update', () => {
const effect = jest.fn();
const { rerender } = renderHook(() => useUpdateEffect(effect));
expect(effect).not.toHaveBeenCalled();
rerender();
expect(effect).toHaveBeenCalledTimes(1);
});
it('should run cleanup on unmount', () => {
const cleanup = jest.fn();
const effect = jest.fn().mockReturnValue(cleanup);
const hook = renderHook(() => useUpdateEffect(effect));
hook.rerender();
hook.unmount();
expect(cleanup).toHaveBeenCalledTimes(1);
});