From b64216bc264e775f214c1c574ffb8df948521c53 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 5 Mar 2021 14:43:14 +0800 Subject: [PATCH] feat(useRequest): add `reload` function to clear the `queries` list --- src/__tests__/index.test.tsx | 140 +++++++++++++++++++++++++++++++++++ src/useRequest.ts | 26 ++++++- 2 files changed, 162 insertions(+), 4 deletions(-) diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index cd692ac..bbb4033 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -2243,4 +2243,144 @@ describe('useRequest', () => { expect(wrapperB.find('#D').text()).toBe('false'); expect(wrapperB.find('#E').text()).toBe('false'); }); + + test('reload should work: case 1', async () => { + const wrapper = shallowMount( + defineComponent({ + setup() { + const { run, reload, data } = useRequest(request, { + defaultParams: ['hello'], + }); + return () => ( +
+ + +
{data.value}
+
+ ); + }, + }), + ); + + const dataEl = wrapper.find('.data'); + const runEl = wrapper.find('.run'); + const reloadEl = wrapper.find('.reload'); + + await waitForTime(1000); + expect(dataEl.text()).toBe('hello'); + + await runEl.trigger('click'); + await waitForTime(1000); + expect(dataEl.text()).toEqual('hi there'); + + await reloadEl.trigger('click'); + await waitForTime(1000); + expect(dataEl.text()).toEqual('hello'); + + await runEl.trigger('click'); + await waitForTime(1000); + expect(dataEl.text()).toEqual('hi there'); + }); + + test('reload should work: case 2', async () => { + const users = [ + { id: '1', username: 'A' }, + { id: '2', username: 'B' }, + { id: '3', username: 'C' }, + ]; + + const wrapper = shallowMount( + defineComponent({ + setup() { + const { run, queries, data, loading, reload } = useRequest(request, { + manual: true, + refreshOnWindowFocus: true, + queryKey: id => id, + }); + + return () => ( +
+
{data.value}
+
{loading.value.toString()}
+
reload()} /> +
    + {users.map(item => ( +
  • run(item.id)} + > + {queries[item.id]?.loading + ? 'loading' + : queries[item.id]?.data} +
  • + ))} +
+
+ ); + }, + }), + ); + + const dataEl = wrapper.find('#data'); + const loadingEl = wrapper.find('#loading'); + const reloadEl = wrapper.find('#reload'); + + expect(FOCUS_LISTENER.size).toBe(1); + expect(VISIBLE_LISTENER.size).toBe(2); + expect(RECONNECT_LISTENER.size).toBe(1); + + for (let i = 0; i < users.length; i++) { + const userName = users[i].username; + const currentId = users[i].id; + + await wrapper.find(`#${userName}`).trigger('click'); + expect(wrapper.find(`#${userName}`).text()).toBe('loading'); + + expect(dataEl.text()).toBe(''); + expect(loadingEl.text()).toBe('true'); + + await waitForTime(1000); + expect(wrapper.find(`#${userName}`).text()).toBe(currentId); + + expect(dataEl.text()).toBe(currentId); + expect(loadingEl.text()).toBe('false'); + } + + expect(FOCUS_LISTENER.size).toBe(4); + expect(VISIBLE_LISTENER.size).toBe(8); + expect(RECONNECT_LISTENER.size).toBe(4); + + await reloadEl.trigger('click'); + for (let i = 0; i < users.length; i++) { + const userName = users[i].username; + expect(wrapper.find(`#${userName}`).text()).toBe(''); + expect(dataEl.text()).toBe(''); + } + + expect(FOCUS_LISTENER.size).toBe(1); + expect(VISIBLE_LISTENER.size).toBe(2); + expect(RECONNECT_LISTENER.size).toBe(1); + + for (let i = 0; i < users.length; i++) { + const userName = users[i].username; + const currentId = users[i].id; + + await wrapper.find(`#${userName}`).trigger('click'); + expect(wrapper.find(`#${userName}`).text()).toBe('loading'); + + expect(dataEl.text()).toBe(''); + expect(loadingEl.text()).toBe('true'); + + await waitForTime(1000); + expect(wrapper.find(`#${userName}`).text()).toBe(currentId); + + expect(dataEl.text()).toBe(currentId); + expect(loadingEl.text()).toBe('false'); + } + + expect(FOCUS_LISTENER.size).toBe(4); + expect(VISIBLE_LISTENER.size).toBe(8); + expect(RECONNECT_LISTENER.size).toBe(4); + }); }); diff --git a/src/useRequest.ts b/src/useRequest.ts index 0c50912..8969c99 100644 --- a/src/useRequest.ts +++ b/src/useRequest.ts @@ -3,24 +3,42 @@ import useAsyncQuery, { BaseResult } from './core/useAsyncQuery'; import generateService from './core/utils/generateService'; import { IService } from './core/utils/types'; +export interface RequestResult + extends Omit, 'reset'> { + reload: () => void; +} function useRequest( service: IService, -): BaseResult; +): RequestResult; function useRequest( service: IService, options: FormatOptions, -): BaseResult; +): RequestResult; function useRequest( service: IService, options: BaseOptions, -): BaseResult; +): RequestResult; function useRequest( service: IService, options?: MixinOptions, ) { const promiseQuery = generateService(service); + const { reset, run, ...rest } = useAsyncQuery( + promiseQuery, + (options ?? {}) as any, + ); + + const reload = () => { + const { defaultParams = ([] as unknown) as P, manual } = options!; + reset(); + !manual && run(...defaultParams); + }; - return useAsyncQuery(promiseQuery, (options ?? {}) as any); + return { + reload, + run, + ...rest, + }; } export default useRequest;