@@ -3,65 +3,81 @@ import { renderHook, act } from '@testing-library/react-hooks';
33import useCollection from './useCollection' ;
44import wrapper from './wrapper' ;
55
6- describe ( 'useCollection' , ( ) => {
6+ describe ( 'useCollection' , ( ) => {
77 beforeEach ( ( ) => {
88 fetch . resetMocks ( ) ;
99 } ) ;
1010
1111 it ( 'queries collection' , async ( ) => {
1212 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- } ) ) ;
13+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , {
14+ url : 'https://fake-stac-api.net'
15+ } )
16+ . mockResponseOnce (
17+ JSON . stringify ( {
18+ id : 'abc' ,
19+ title : 'Collection A'
20+ } )
21+ ) ;
2022
2123 const { result, waitForNextUpdate } = renderHook (
2224 ( ) => useCollection ( 'abc' ) ,
2325 { wrapper }
2426 ) ;
2527 await waitForNextUpdate ( ) ;
26- await waitForNextUpdate ( ) ;
27- expect ( result . current . collection ) . toEqual ( { id : 'abc' , title : 'Collection A' } ) ;
28+ expect ( result . current . collection ) . toEqual ( {
29+ id : 'abc' ,
30+ title : 'Collection A'
31+ } ) ;
2832 expect ( result . current . state ) . toEqual ( 'IDLE' ) ;
2933 } ) ;
3034
3135 it ( 'returns error if collection does not exist' , async ( ) => {
3236 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- } ) ) ;
37+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , {
38+ url : 'https://fake-stac-api.net'
39+ } )
40+ . mockResponseOnce (
41+ JSON . stringify ( {
42+ code : 'NotFoundError' ,
43+ description : 'Collection asdasd does not exist.'
44+ } ) ,
45+ {
46+ status : 404 ,
47+ statusText : 'Not found'
48+ }
49+ ) ;
4050
4151 const { result, waitForNextUpdate } = renderHook (
4252 ( ) => useCollection ( 'ghi' ) ,
4353 { wrapper }
4454 ) ;
4555 await waitForNextUpdate ( ) ;
46- await waitForNextUpdate ( ) ;
4756 expect ( result . current . error ) . toEqual ( {
4857 status : 404 ,
4958 statusText : 'Not found' ,
50- detail : 'Collection does not exist'
59+ detail : {
60+ code : 'NotFoundError' ,
61+ description : 'Collection asdasd does not exist.'
62+ }
5163 } ) ;
5264 } ) ;
5365
5466 it ( 'handles error with JSON response' , async ( ) => {
5567 fetch
56- . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
57- . mockResponseOnce ( JSON . stringify ( { error : 'Wrong query' } ) , { status : 400 , statusText : 'Bad Request' } ) ;
68+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , {
69+ url : 'https://fake-stac-api.net'
70+ } )
71+ . mockResponseOnce ( JSON . stringify ( { error : 'Wrong query' } ) , {
72+ status : 400 ,
73+ statusText : 'Bad Request'
74+ } ) ;
5875
5976 const { result, waitForNextUpdate } = renderHook (
6077 ( ) => useCollection ( 'abc' ) ,
6178 { wrapper }
6279 ) ;
6380 await waitForNextUpdate ( ) ;
64- await waitForNextUpdate ( ) ;
6581
6682 expect ( result . current . error ) . toEqual ( {
6783 status : 400 ,
@@ -72,15 +88,19 @@ describe('useCollection' ,() => {
7288
7389 it ( 'handles error with non-JSON response' , async ( ) => {
7490 fetch
75- . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , { url : 'https://fake-stac-api.net' } )
76- . mockResponseOnce ( 'Wrong query' , { status : 400 , statusText : 'Bad Request' } ) ;
91+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , {
92+ url : 'https://fake-stac-api.net'
93+ } )
94+ . mockResponseOnce ( 'Wrong query' , {
95+ status : 400 ,
96+ statusText : 'Bad Request'
97+ } ) ;
7798
7899 const { result, waitForNextUpdate } = renderHook (
79100 ( ) => useCollection ( 'abc' ) ,
80101 { wrapper }
81102 ) ;
82103 await waitForNextUpdate ( ) ;
83- await waitForNextUpdate ( ) ;
84104
85105 expect ( result . current . error ) . toEqual ( {
86106 status : 400 ,
@@ -91,31 +111,35 @@ describe('useCollection' ,() => {
91111
92112 it ( 'reloads collection' , async ( ) => {
93113 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- } ) ) ;
114+ . mockResponseOnce ( JSON . stringify ( { links : [ ] } ) , {
115+ url : 'https://fake-stac-api.net'
116+ } )
117+ . mockResponseOnce (
118+ JSON . stringify ( {
119+ id : 'abc' ,
120+ title : 'Collection A'
121+ } )
122+ )
123+ . mockResponseOnce (
124+ JSON . stringify ( { id : 'abc' , title : 'Collection A - Updated' } )
125+ ) ;
107126
108127 const { result, waitForNextUpdate } = renderHook (
109128 ( ) => useCollection ( 'abc' ) ,
110129 { wrapper }
111130 ) ;
112131 await waitForNextUpdate ( ) ;
113- await waitForNextUpdate ( ) ;
114- expect ( result . current . collection ) . toEqual ( { id : 'abc' , title : 'Collection A' } ) ;
132+ expect ( result . current . collection ) . toEqual ( {
133+ id : 'abc' ,
134+ title : 'Collection A'
135+ } ) ;
115136
116137 act ( ( ) => result . current . reload ( ) ) ;
117138
118139 await waitForNextUpdate ( ) ;
119- expect ( result . current . collection ) . toEqual ( { id : 'abc' , title : 'Collection A - Updated' } ) ;
140+ expect ( result . current . collection ) . toEqual ( {
141+ id : 'abc' ,
142+ title : 'Collection A - Updated'
143+ } ) ;
120144 } ) ;
121145} ) ;
0 commit comments