1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/lib/hooks/useNowEffect.test.ts
Henry Heino 4a88d6ff7a
Desktop: Multiple window support (#11181)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-11-08 15:32:05 +00:00

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);
});
});