Skip to content

Commit 5db4b2b

Browse files
committed
feat: Add reload to useCollection
1 parent 025023b commit 5db4b2b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/hooks/useCollection.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from 'jest-fetch-mock';
2-
import { renderHook } from '@testing-library/react-hooks';
2+
import { renderHook, act } from '@testing-library/react-hooks';
33
import useCollection from './useCollection';
44
import wrapper from './wrapper';
55

@@ -88,4 +88,34 @@ describe('useCollection' ,() => {
8888
detail: 'Wrong query'
8989
});
9090
});
91+
92+
it('reloads collection', async () => {
93+
fetch
94+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
95+
.mockResponseOnce(JSON.stringify({
96+
collections: [
97+
{id: 'abc', title: 'Collection A'},
98+
{id: 'def', title: 'Collection B'}
99+
]
100+
}))
101+
.mockResponseOnce(JSON.stringify({
102+
collections: [
103+
{id: 'abc', title: 'Collection A - Updated'},
104+
{id: 'def', title: 'Collection B'}
105+
]
106+
}));
107+
108+
const { result, waitForNextUpdate } = renderHook(
109+
() => useCollection('abc'),
110+
{ wrapper }
111+
);
112+
await waitForNextUpdate();
113+
await waitForNextUpdate();
114+
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A'});
115+
116+
act(() => result.current.reload());
117+
118+
await waitForNextUpdate();
119+
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A - Updated'});
120+
});
91121
});

src/hooks/useCollection.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import useCollections from './useCollections';
77
type StacCollectionHook = {
88
collection?: Collection,
99
state: LoadingState,
10-
error?: ApiError
10+
error?: ApiError,
11+
reload: () => void
1112
};
1213

1314
function useCollection(collectionId: string): StacCollectionHook {
14-
const { collections, state, error: requestError } = useCollections();
15+
const { collections, state, error: requestError, reload } = useCollections();
1516
const [ error, setError ] = useState<ApiError>();
1617

1718
useEffect(() => {
@@ -36,7 +37,8 @@ function useCollection(collectionId: string): StacCollectionHook {
3637
return {
3738
collection,
3839
state,
39-
error
40+
error,
41+
reload
4042
};
4143
}
4244

0 commit comments

Comments
 (0)