File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments