Skip to content

Commit 9684545

Browse files
committed
feat: Add useCollection hook
1 parent b2cf2d8 commit 9684545

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/hooks/useCollection.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import fetch from 'jest-fetch-mock';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import useCollection from './useCollection';
4+
import wrapper from './wrapper';
5+
6+
describe('useCollection' ,() => {
7+
beforeEach(() => {
8+
fetch.resetMocks();
9+
});
10+
11+
it('queries collection', async () => {
12+
fetch
13+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
14+
.mockResponseOnce(JSON.stringify({
15+
collections: [
16+
{id: 'abc', title: 'Collection A'},
17+
{id: 'def', title: 'Collection B'}
18+
]
19+
}));
20+
21+
const { result, waitForNextUpdate } = renderHook(
22+
() => useCollection('abc'),
23+
{ wrapper }
24+
);
25+
await waitForNextUpdate();
26+
await waitForNextUpdate();
27+
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A'});
28+
expect(result.current.state).toEqual('IDLE');
29+
});
30+
});

src/hooks/useCollection.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useMemo } from 'react';
2+
3+
import type { LoadingState } from '../types';
4+
import type { Collection } from '../types/stac';
5+
import useCollections from './useCollections';
6+
7+
type StacCollectionHook = {
8+
collection?: Collection,
9+
state: LoadingState
10+
};
11+
12+
function useCollection(collectionId: string): StacCollectionHook {
13+
const { collections, state } = useCollections();
14+
15+
const collection = useMemo(
16+
() => collections?.collections.find(({ id }) => id === collectionId),
17+
[collectionId, collections]
18+
);
19+
20+
return {
21+
collection,
22+
state
23+
};
24+
}
25+
26+
export default useCollection;

0 commit comments

Comments
 (0)