mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
|
import type * as React from 'react';
|
||
|
import { renderHook } from '@testing-library/react-hooks';
|
||
|
import useNowEffect from './useNowEffect';
|
||
|
|
||
|
describe('useNowEffect', () => {
|
||
|
test('should call the cleanup callback when the effect function is called and after unmount', async () => {
|
||
|
const cleanupFunction = jest.fn(() => {});
|
||
|
const effectFunction = jest.fn(() => cleanupFunction);
|
||
|
const useTestHook = (dependencies: React.DependencyList) => {
|
||
|
return useNowEffect(effectFunction, dependencies);
|
||
|
};
|
||
|
|
||
|
const hook = renderHook(useTestHook, { initialProps: [0] });
|
||
|
expect(cleanupFunction).not.toHaveBeenCalled();
|
||
|
expect(effectFunction).toHaveBeenCalledTimes(1);
|
||
|
|
||
|
hook.rerender([0]);
|
||
|
expect(cleanupFunction).not.toHaveBeenCalled();
|
||
|
expect(effectFunction).toHaveBeenCalledTimes(1);
|
||
|
|
||
|
hook.rerender([1]);
|
||
|
expect(cleanupFunction).toHaveBeenCalledTimes(1);
|
||
|
expect(effectFunction).toHaveBeenCalledTimes(2);
|
||
|
|
||
|
hook.unmount();
|
||
|
expect(cleanupFunction).toHaveBeenCalledTimes(2);
|
||
|
expect(effectFunction).toHaveBeenCalledTimes(2);
|
||
|
});
|
||
|
});
|