Skip to content

Commit bfc53ae

Browse files
JackWang032jialan
andauthored
fix(useList): fix setLoading when request multiple times (#391) (#397)
Co-authored-by: jialan <[email protected]>
1 parent 2124e4f commit bfc53ae

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/blockHeader/__tests__/blockHeader.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
22
import { cleanup, fireEvent, render } from '@testing-library/react';
3-
import { SizeType } from 'dt-react-component/esm/blockHeader';
43
import '@testing-library/jest-dom/extend-expect';
54

6-
import BlockHeader from '../index';
5+
import BlockHeader, { type SizeType } from '../index';
76

87
const props = {
98
title: '标题1',

src/useList/__tests__/useList.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import { renderHook } from '@testing-library/react-hooks';
33

44
import useList from '..';
55

6+
const awaitTimers = async () => {
7+
for (let i = 0; i < 10; i++) {
8+
await Promise.resolve();
9+
}
10+
};
11+
612
describe('Test useList hook', () => {
13+
afterEach(() => jest.useRealTimers());
14+
715
it('Should get initial data with default params', async () => {
816
const fetcher = jest.fn().mockResolvedValue({
917
total: 1,
@@ -84,4 +92,45 @@ describe('Test useList hook', () => {
8492

8593
expect(fetcher).not.toBeCalled();
8694
});
95+
96+
it('Should hide loading after the last fetching done', async () => {
97+
jest.useFakeTimers({ advanceTimers: true });
98+
const fetcher = jest
99+
.fn()
100+
.mockReturnValueOnce(
101+
new Promise((resolve) => {
102+
setTimeout(() => {
103+
resolve({
104+
total: 0,
105+
data: [],
106+
});
107+
}, 100);
108+
})
109+
)
110+
.mockReturnValueOnce(
111+
new Promise((resolve) => {
112+
setTimeout(() => {
113+
resolve({
114+
total: 0,
115+
data: [],
116+
});
117+
}, 200);
118+
})
119+
);
120+
121+
const { result } = renderHook(() => useList(fetcher, {}, { immediate: false }));
122+
123+
act(() => {
124+
result.current.mutate();
125+
result.current.mutate();
126+
});
127+
128+
jest.advanceTimersByTime(100);
129+
await awaitTimers();
130+
expect(result.current.loading).toBe(true);
131+
132+
jest.advanceTimersByTime(100);
133+
await awaitTimers();
134+
expect(result.current.loading).toBe(false);
135+
});
87136
});

src/useList/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from 'react';
1+
import { useEffect, useMemo, useRef, useState } from 'react';
22
import { merge } from 'lodash';
33

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

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

3334
const performFetch = (raw = params) => {
35+
const requestId = Symbol('id');
36+
lastRequestId.current = requestId;
3437
setLoading(true);
3538
fetcher(raw)
3639
.then(({ data, total }) => {
@@ -39,7 +42,7 @@ export default function useList<T extends Record<string, any>, P extends Record<
3942
})
4043
.catch(setError)
4144
.finally(() => {
42-
setLoading(false);
45+
lastRequestId.current === requestId && setLoading(false);
4346
});
4447
};
4548

0 commit comments

Comments
 (0)