1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/hooks/useQueuedAsyncEffect.test.ts

36 lines
1.2 KiB
TypeScript

import type * as React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import useQueuedAsyncEffect from './useQueuedAsyncEffect';
import { runWithFakeTimers } from '../testing/test-utils';
describe('useQueuedAsyncEffect', () => {
test('should debounce effect updates', async () => {
const effectFunction = jest.fn(async () => { });
const useTestHook = (dependencies: React.DependencyList) => {
return useQueuedAsyncEffect(effectFunction, dependencies);
};
await runWithFakeTimers(async () => {
const result = renderHook(useTestHook, { initialProps: ['test'] });
// Should pause to allow debouncing.
expect(effectFunction).not.toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(12500);
expect(effectFunction).toHaveBeenCalledTimes(1);
await jest.advanceTimersByTimeAsync(1000);
// Changing twice quickly: Should only update once
result.rerender(['changed']);
expect(effectFunction).toHaveBeenCalledTimes(1);
result.rerender(['changed again']);
await jest.advanceTimersByTimeAsync(500);
expect(effectFunction).toHaveBeenCalledTimes(2);
await jest.advanceTimersByTimeAsync(500);
expect(effectFunction).toHaveBeenCalledTimes(2);
});
});
});