@@ -11,11 +11,11 @@ describe('useCollection' ,() => {
1111 it ( 'queries collection' , async ( ) => {
1212 fetch
1313 . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
14- . mockResponseOnce ( JSON . stringify ( {
14+ . mockResponseOnce ( JSON . stringify ( {
1515 collections : [
1616 { id : 'abc' , title : 'Collection A' } ,
1717 { id : 'def' , title : 'Collection B' }
18- ]
18+ ]
1919 } ) ) ;
2020
2121 const { result, waitForNextUpdate } = renderHook (
@@ -27,4 +27,65 @@ describe('useCollection' ,() => {
2727 expect ( result . current . collection ) . toEqual ( { id : 'abc' , title : 'Collection A' } ) ;
2828 expect ( result . current . state ) . toEqual ( 'IDLE' ) ;
2929 } ) ;
30+
31+ it ( 'returns error if collection does not exist' , async ( ) => {
32+ fetch
33+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
34+ . mockResponseOnce ( JSON . stringify ( {
35+ collections : [
36+ { id : 'abc' , title : 'Collection A' } ,
37+ { id : 'def' , title : 'Collection B' }
38+ ]
39+ } ) ) ;
40+
41+ const { result, waitForNextUpdate } = renderHook (
42+ ( ) => useCollection ( 'ghi' ) ,
43+ { wrapper }
44+ ) ;
45+ await waitForNextUpdate ( ) ;
46+ await waitForNextUpdate ( ) ;
47+ expect ( result . current . error ) . toEqual ( {
48+ status : 404 ,
49+ statusText : 'Not found' ,
50+ detail : 'Collection does not exist'
51+ } ) ;
52+ } ) ;
53+
54+ it ( 'handles error with JSON response' , async ( ) => {
55+ fetch
56+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
57+ . mockResponseOnce ( JSON . stringify ( { error : 'Wrong query' } ) , { status : 400 , statusText : 'Bad Request' } ) ;
58+
59+ const { result, waitForNextUpdate } = renderHook (
60+ ( ) => useCollection ( 'abc' ) ,
61+ { wrapper }
62+ ) ;
63+ await waitForNextUpdate ( ) ;
64+ await waitForNextUpdate ( ) ;
65+
66+ expect ( result . current . error ) . toEqual ( {
67+ status : 400 ,
68+ statusText : 'Bad Request' ,
69+ detail : { error : 'Wrong query' }
70+ } ) ;
71+ } ) ;
72+
73+ it ( 'handles error with non-JSON response' , async ( ) => {
74+ fetch
75+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
76+ . mockResponseOnce ( 'Wrong query' , { status : 400 , statusText : 'Bad Request' } ) ;
77+
78+ const { result, waitForNextUpdate } = renderHook (
79+ ( ) => useCollection ( 'abc' ) ,
80+ { wrapper }
81+ ) ;
82+ await waitForNextUpdate ( ) ;
83+ await waitForNextUpdate ( ) ;
84+
85+ expect ( result . current . error ) . toEqual ( {
86+ status : 400 ,
87+ statusText : 'Bad Request' ,
88+ detail : 'Wrong query'
89+ } ) ;
90+ } ) ;
3091} ) ;
0 commit comments