Skip to content

Commit

Permalink
fix(useList): fix setLoading when request multiple times (#391) (#397)
Browse files Browse the repository at this point in the history
Co-authored-by: jialan <jialan@dtstack.com>
  • Loading branch information
JackWang032 and jialan committed Nov 7, 2023
1 parent 2124e4f commit bfc53ae
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/blockHeader/__tests__/blockHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from 'react';
import { cleanup, fireEvent, render } from '@testing-library/react';
import { SizeType } from 'dt-react-component/esm/blockHeader';
import '@testing-library/jest-dom/extend-expect';

import BlockHeader from '../index';
import BlockHeader, { type SizeType } from '../index';

const props = {
title: '标题1',
Expand Down
49 changes: 49 additions & 0 deletions src/useList/__tests__/useList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { renderHook } from '@testing-library/react-hooks';

import useList from '..';

const awaitTimers = async () => {
for (let i = 0; i < 10; i++) {
await Promise.resolve();
}
};

describe('Test useList hook', () => {
afterEach(() => jest.useRealTimers());

it('Should get initial data with default params', async () => {
const fetcher = jest.fn().mockResolvedValue({
total: 1,
Expand Down Expand Up @@ -84,4 +92,45 @@ describe('Test useList hook', () => {

expect(fetcher).not.toBeCalled();
});

it('Should hide loading after the last fetching done', async () => {
jest.useFakeTimers({ advanceTimers: true });
const fetcher = jest
.fn()
.mockReturnValueOnce(
new Promise((resolve) => {
setTimeout(() => {
resolve({
total: 0,
data: [],
});
}, 100);
})
)
.mockReturnValueOnce(
new Promise((resolve) => {
setTimeout(() => {
resolve({
total: 0,
data: [],
});
}, 200);
})
);

const { result } = renderHook(() => useList(fetcher, {}, { immediate: false }));

act(() => {
result.current.mutate();
result.current.mutate();
});

jest.advanceTimersByTime(100);
await awaitTimers();
expect(result.current.loading).toBe(true);

jest.advanceTimersByTime(100);
await awaitTimers();
expect(result.current.loading).toBe(false);
});
});
7 changes: 5 additions & 2 deletions src/useList/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { merge } from 'lodash';

export type Fetcher<T, P> = (params: P) => Promise<{ data: T[]; total: number }>;
Expand Down Expand Up @@ -27,10 +27,13 @@ export default function useList<T extends Record<string, any>, P extends Record<
const [total, setTotal] = useState(0);
const [params, setParams] = useState<P>(initialParams);
const [loading, setLoading] = useState(false);
const lastRequestId = useRef<symbol | undefined>(undefined);

const options = useMemo(() => merge({ immediate: true }, rawOptions), [rawOptions]);

const performFetch = (raw = params) => {
const requestId = Symbol('id');
lastRequestId.current = requestId;
setLoading(true);
fetcher(raw)
.then(({ data, total }) => {
Expand All @@ -39,7 +42,7 @@ export default function useList<T extends Record<string, any>, P extends Record<
})
.catch(setError)
.finally(() => {
setLoading(false);
lastRequestId.current === requestId && setLoading(false);
});
};

Expand Down

0 comments on commit bfc53ae

Please sign in to comment.