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: correctly handle value of initialPromise for fetchWhenDepsChange conditions in useLoadData #28

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions hooks/useLoadData/useLoadData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,63 @@ describe('useLoadData', () => {
expect(result.current.isError).toBe(true);
expect(result.current.error).toBe('immediate failure');
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with normal dep', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>('a');
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep('b'));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);

expect(getSuccess).toHaveBeenCalledWith('b');
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with finished dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>({...successfulResponse, result: 'a'});
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with pending dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>(pendingResponse);
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);
await act(() => result.current.setDep({...successfulResponse, result: 'a'}));

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
});
});
11 changes: 9 additions & 2 deletions hooks/useLoadData/useLoadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
// eslint-disable-next-line @typescript-eslint/promise-function-async
const initialPromise = useMemo(() => {
const correctedArgs = correctOptionalDependencies(fetchDataArgs);
if (!data && counter < 1 && checkArgsAreLoaded(correctedArgs)) {
// initialPromise should NOT be defined in the following scenarios:
// 1. data is passed, in which case fetchData should never be invoked
// 2. dependencies are not ready initially, so we cannot proceed with calling fetchData
// 3. we are attempting to retry calling fetchData, and we do not want initialPromise to interfere
// with re-invoking fetchData
// 4. we are attempting to refetch data due to dependencies changing, and we do not want initialPromise
// to interfere with re-invoking fetchData
if (!data && counter < 1 && checkArgsAreLoaded(correctedArgs) && !localFetchWhenDepsChange) {
try {
return {
res: fetchData(...((correctedArgs.map(unboxApiResponse) || []) as Parameters<typeof fetchData>)),
Expand All @@ -177,7 +184,7 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
} else {
return {res: undefined, error: undefined};
}
}, [counter]);
}, [counter, localFetchWhenDepsChange]);

const nonPromiseResult = initialPromise.res instanceof Promise ? undefined : initialPromise.res;
const initialData = data || nonPromiseResult;
Expand Down
Loading