@@ -24,6 +24,44 @@ describe('useCollections', () => {
2424 expect ( result . current . state ) . toEqual ( 'IDLE' ) ;
2525 } ) ;
2626
27+ it ( 'handles error with JSON response' , async ( ) => {
28+ fetch
29+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
30+ . mockResponseOnce ( JSON . stringify ( { error : 'Wrong query' } ) , { status : 400 , statusText : 'Bad Request' } ) ;
31+
32+ const { result, waitForNextUpdate } = renderHook (
33+ ( ) => useCollections ( ) ,
34+ { wrapper }
35+ ) ;
36+
37+ await waitForNextUpdate ( ) ;
38+ await waitForNextUpdate ( ) ;
39+
40+ expect ( result . current . error ) . toEqual ( {
41+ status : 400 ,
42+ statusText : 'Bad Request' ,
43+ detail : { error : 'Wrong query' }
44+ } ) ;
45+ } ) ;
46+
47+ it ( 'handles error with non-JSON response' , async ( ) => {
48+ fetch
49+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
50+ . mockResponseOnce ( 'Wrong query' , { status : 400 , statusText : 'Bad Request' } ) ;
51+
52+ const { result, waitForNextUpdate } = renderHook (
53+ ( ) => useCollections ( ) ,
54+ { wrapper }
55+ ) ;
56+ await waitForNextUpdate ( ) ;
57+ await waitForNextUpdate ( ) ;
58+ expect ( result . current . error ) . toEqual ( {
59+ status : 400 ,
60+ statusText : 'Bad Request' ,
61+ detail : 'Wrong query'
62+ } ) ;
63+ } ) ;
64+
2765 it ( 'reloads collections' , async ( ) => {
2866 fetch
2967 . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
@@ -40,6 +78,7 @@ describe('useCollections', () => {
4078
4179 expect ( result . current . state ) . toEqual ( 'IDLE' ) ;
4280 act ( ( ) => result . current . reload ( ) ) ;
81+
4382 await waitForNextUpdate ( ) ;
4483 expect ( result . current . collections ) . toEqual ( { data : 'reloaded' } ) ;
4584 } ) ;
0 commit comments