Skip to content

Commit

Permalink
fix: loading is always return true when set loadingDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
hchlq committed Oct 9, 2022
1 parent 2be989c commit c5a20e9
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 911 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,35 @@ describe('useLoadingDelayPlugin', () => {
expect(hook.result.current.loading).toEqual(false);
hook.unmount();
});

it('useLoadingDelayPlugin should no update loading when ready is false', async () => {
act(() => {
hook = setUp(request, {
loadingDelay: 2000,
ready: false,
});
});
expect(hook.result.current.loading).toEqual(false);

act(() => {
jest.advanceTimersByTime(3000);
});

expect(hook.result.current.loading).toEqual(false);
});

it('useLoadingDelayPlugin should update loading when ready is undefined', async () => {
act(() => {
hook = setUp(request, {
loadingDelay: 2000,
});
});
expect(hook.result.current.loading).toEqual(false);

act(() => {
jest.advanceTimersByTime(3000);
});

expect(hook.result.current.loading).toEqual(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useRef } from 'react';
import type { Plugin, Timeout } from '../types';
import { isBoolean } from '../../../utils';

const useLoadingDelayPlugin: Plugin<any, any[]> = (fetchInstance, { loadingDelay }) => {
const useLoadingDelayPlugin: Plugin<any, any[]> = (fetchInstance, { loadingDelay, ready }) => {
const timerRef = useRef<Timeout>();

if (!loadingDelay) {
Expand All @@ -18,11 +19,13 @@ const useLoadingDelayPlugin: Plugin<any, any[]> = (fetchInstance, { loadingDelay
onBefore: () => {
cancelTimeout();

timerRef.current = setTimeout(() => {
fetchInstance.setState({
loading: true,
});
}, loadingDelay);
if (ready === undefined || (isBoolean(ready) && ready)) {
timerRef.current = setTimeout(() => {
fetchInstance.setState({
loading: true,
});
}, loadingDelay);
}

return {
loading: false,
Expand Down

0 comments on commit c5a20e9

Please sign in to comment.