Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loading is always return true when set loadingDelay #1903

Merged
merged 7 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
hchlq marked this conversation as resolved.
Show resolved Hide resolved
timerRef.current = setTimeout(() => {
fetchInstance.setState({
loading: true,
});
}, loadingDelay);
}

return {
loading: false,
Expand Down