Skip to content

Commit

Permalink
feat: add onBefore and onAfter hooks #42
Browse files Browse the repository at this point in the history
  • Loading branch information
John60676 committed May 8, 2021
1 parent 5034f2c commit 135e76f
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2463,4 +2463,35 @@ describe('useRequest', () => {
expect(VISIBLE_LISTENER.size).toBe(8);
expect(RECONNECT_LISTENER.size).toBe(4);
});

test('onBefore and onAfter hooks can use', async () => {
const onBefore = jest.fn();
const onAfter = jest.fn();

const wrapper = shallowMount(
defineComponent({
setup() {
const { data, run } = useRequest(request, {
onBefore,
onAfter,
});

return () => (
<button onClick={() => run()}>{`data:${data.value}`}</button>
);
},
}),
);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(0);
await waitForTime(100);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(0);
await waitForTime(800);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(0);
await waitForTime(100);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(1);
});
});
41 changes: 41 additions & 0 deletions src/__tests__/load-more.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -827,4 +827,45 @@ describe('useLoadMore', () => {
expect(wrapper.find('#D').text()).toBe('1');
expect(wrapper.find('#E').text()).toBe('5');
});

test('onBefore and onAfter hooks can use in `useLoadMore`', async () => {
const onBefore = jest.fn();
const onAfter = jest.fn();

const wrapper = shallowMount(
defineComponent({
setup() {
const { loadMore } = useLoadMore(normalRequest, {
onBefore,
onAfter,
});
return () => (
<div>
<div
class="loadMore"
onClick={() => {
loadMore();
}}
/>
</div>
);
},
}),
);

const loadMoreEl = wrapper.find('.loadMore');

expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(0);
await waitForTime(1000);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(1);

await loadMoreEl.trigger('click');
expect(onBefore).toHaveBeenCalledTimes(2);
expect(onAfter).toHaveBeenCalledTimes(1);
await waitForTime(1000);
expect(onBefore).toHaveBeenCalledTimes(2);
expect(onAfter).toHaveBeenCalledTimes(2);
});
});
39 changes: 39 additions & 0 deletions src/__tests__/pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,43 @@ describe('usePagination', () => {
expect(wrapper.find('#D').text()).toBe('1');
expect(wrapper.find('#E').text()).toBe('5');
});

test('onBefore and onAfter hooks can use in `usePagination`', async () => {
const onBefore = jest.fn();
const onAfter = jest.fn();

const wrapper = shallowMount(
defineComponent({
setup() {
const { changeCurrent } = usePagination<NormalMockDataType>(
normalApi,
{
onBefore,
onAfter,
},
);
return () => (
<div>
<button class="button" onClick={() => changeCurrent(1)} />
</div>
);
},
}),
);

const buttonEl = wrapper.find('.button');

expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(0);
await waitForTime(1000);
expect(onBefore).toHaveBeenCalledTimes(1);
expect(onAfter).toHaveBeenCalledTimes(1);

await buttonEl.trigger('click');
expect(onBefore).toHaveBeenCalledTimes(2);
expect(onAfter).toHaveBeenCalledTimes(1);
await waitForTime(1000);
expect(onBefore).toHaveBeenCalledTimes(2);
expect(onAfter).toHaveBeenCalledTimes(2);
});
});
2 changes: 2 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export type BaseOptions<R, P extends unknown[]> = GlobalOptions & {
queryKey?: (...args: P) => string;
onSuccess?: (data: R, params: P) => void;
onError?: (error: Error, params: P) => void;
onBefore?: (params: P) => void;
onAfter?: (params: P) => void;
};

const FRPlaceholderType = Symbol('FR');
Expand Down
8 changes: 8 additions & 0 deletions src/core/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const createQuery = <R, P extends unknown[]>(
formatResult,
onSuccess,
onError,
onBefore,
onAfter,
} = config;

const retriedCount = ref(0);
Expand Down Expand Up @@ -193,6 +195,9 @@ const createQuery = <R, P extends unknown[]>(
count.value += 1;
const currentCount = count.value;

// onBefore hooks
onBefore?.(args);

return query(...args)
.then(res => {
if (currentCount === count.value) {
Expand Down Expand Up @@ -238,6 +243,9 @@ const createQuery = <R, P extends unknown[]>(

// run for polling
pollingTimer.value = polling(() => _run(...args));

// onAfter hooks
onAfter?.(args);
}
});
};
Expand Down
4 changes: 4 additions & 0 deletions src/core/useAsyncQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
formatResult,
onSuccess,
onError,
onBefore,
onAfter,
} = { ...getGlobalOptions(), ...injectedGlobalOptions, ...options };

const stopPollingWhenHiddenOrOffline = ref(false);
Expand Down Expand Up @@ -134,6 +136,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
formatResult,
onSuccess,
onError,
onBefore,
onAfter,
} as Config<R, P>;

const loading = ref(false);
Expand Down

0 comments on commit 135e76f

Please sign in to comment.