Skip to content

Commit 0109e59

Browse files
committed
fix: properly type reload as async across all hooks
- Change reload return type from () => void to () => Promise<QueryObserverResult> - Remove incorrect type casts from refetch in useCollection, useCollections, useItem - Update tests to await reload() calls with proper act() wrapping - Remove eslint-disable comment from useItem test - Remove debounce wrapper from useCollections reload (was returning void) The refetch function from React Query returns a Promise, not void. This fix properly reflects the async nature of reload operations and resolves React act() warnings in tests. Debouncing was removed from useCollections.reload as it's uncommon to debounce explicit reload actions.
1 parent c785dea commit 0109e59

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

src/hooks/useCollection.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ describe('useCollection', () => {
8383
expect(result.current.collection).toEqual({ id: 'abc', title: 'Collection A' })
8484
);
8585

86-
act(() => result.current.reload());
86+
await act(async () => {
87+
await result.current.reload();
88+
});
8789

8890
await waitFor(() =>
8991
expect(result.current.collection).toEqual({ id: 'abc', title: 'Collection A - Updated' })

src/hooks/useCollection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useQuery } from '@tanstack/react-query';
1+
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
22
import type { ApiErrorType } from '../types';
33
import type { Collection } from '../types/stac';
44
import { ApiError } from '../utils/ApiError';
@@ -10,7 +10,7 @@ type StacCollectionHook = {
1010
isLoading: boolean;
1111
isFetching: boolean;
1212
error?: ApiErrorType;
13-
reload: () => void;
13+
reload: () => Promise<QueryObserverResult<Collection, ApiErrorType>>;
1414
};
1515

1616
function useCollection(collectionId: string): StacCollectionHook {
@@ -50,7 +50,7 @@ function useCollection(collectionId: string): StacCollectionHook {
5050
isLoading,
5151
isFetching,
5252
error: error as ApiErrorType,
53-
reload: refetch as () => void,
53+
reload: refetch,
5454
};
5555
}
5656

src/hooks/useCollections.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('useCollections', () => {
3232
await waitFor(() => expect(result.current.collections).toEqual({ data: 'original' }));
3333
await waitFor(() => expect(result.current.isLoading).toEqual(false));
3434

35-
act(() => result.current.reload());
35+
await act(async () => {
36+
await result.current.reload();
37+
});
3638

3739
await waitFor(() => expect(result.current.collections).toEqual({ data: 'reloaded' }));
3840
});

src/hooks/useCollections.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { useMemo } from 'react';
2-
import { useQuery } from '@tanstack/react-query';
1+
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
32
import { type ApiErrorType } from '../types';
43
import type { CollectionsResponse } from '../types/stac';
5-
import debounce from '../utils/debounce';
64
import { ApiError } from '../utils/ApiError';
75
import { generateCollectionsQueryKey } from '../utils/queryKeys';
86
import { useStacApiContext } from '../context/useStacApiContext';
97

108
type StacCollectionsHook = {
119
collections?: CollectionsResponse;
12-
reload: () => void;
10+
reload: () => Promise<QueryObserverResult<CollectionsResponse, ApiErrorType>>;
1311
isLoading: boolean;
1412
isFetching: boolean;
1513
error?: ApiErrorType;
@@ -47,11 +45,9 @@ function useCollections(): StacCollectionsHook {
4745
retry: false,
4846
});
4947

50-
const reload = useMemo(() => debounce(refetch), [refetch]);
51-
5248
return {
5349
collections,
54-
reload,
50+
reload: refetch,
5551
isLoading,
5652
isFetching,
5753
error: error as ApiErrorType,

src/hooks/useItem.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('useItem', () => {
6969
await waitFor(() => expect(result.current.item).toEqual({ id: 'abc' }));
7070

7171
await act(async () => {
72-
// eslint-disable-next-line @typescript-eslint/await-thenable
7372
await result.current.reload();
7473
});
7574

src/hooks/useItem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useQuery } from '@tanstack/react-query';
1+
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
22
import { Item } from '../types/stac';
33
import { type ApiErrorType } from '../types';
44
import { useStacApiContext } from '../context/useStacApiContext';
@@ -10,7 +10,7 @@ type ItemHook = {
1010
isLoading: boolean;
1111
isFetching: boolean;
1212
error?: ApiErrorType;
13-
reload: () => void;
13+
reload: () => Promise<QueryObserverResult<Item, ApiErrorType>>;
1414
};
1515

1616
function useItem(url: string): ItemHook {
@@ -50,7 +50,7 @@ function useItem(url: string): ItemHook {
5050
isLoading,
5151
isFetching,
5252
error: error as ApiErrorType,
53-
reload: refetch as () => void,
53+
reload: refetch,
5454
};
5555
}
5656

0 commit comments

Comments
 (0)