Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, renderHook, waitFor } from '@testing-library/react';
import type { RenderHookResult } from '@testing-library/react';
import useRequest from '../index';
import { request } from '../../utils/testingHelpers';
import { request, sleep } from '../../utils/testingHelpers';

describe('useLoadingDelayPlugin', () => {
jest.useFakeTimers();
Expand Down Expand Up @@ -75,4 +75,30 @@ describe('useLoadingDelayPlugin', () => {

await waitFor(() => expect(hook.result.current.loading).toBe(true));
});

it.only('useLoadingDelayPlugin should update loading when loadingDelay is reset and refreshDeps is changed', async () => {
jest.useRealTimers();

let loadingDelay: number | null = 1500;

act(() => {
hook = setUp(request, {
loadingDelay,
});
});

expect(hook.result.current.loading).toBe(false);

loadingDelay = null;

hook.rerender({
loadingDelay,
});

await act(async () => {
await sleep(1600);
});

expect(hook.result.current.loading).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import type { Plugin, Timeout } from '../types';
const useLoadingDelayPlugin: Plugin<any, any[]> = (fetchInstance, { loadingDelay, ready }) => {
const timerRef = useRef<Timeout>();

if (!loadingDelay) {
return {};
}

const cancelTimeout = () => {
if (timerRef.current) {
clearTimeout(timerRef.current);
}
};

if (!loadingDelay) {
cancelTimeout();
return {};
}

return {
onBefore: () => {
cancelTimeout();
Expand Down
Loading