1- import  {  fetchData ,  insertParams ,  stringifyQuery  }  from  '../src/utils' 
1+ /// <reference lib="dom" /> 
2+ import  {  fetchData ,  getData ,  deleteData ,  insertParams ,  stringifyQuery  }  from  '../src/utils' 
23
34const  fetchMock  =  jest . spyOn ( global ,  'fetch' )  as  typeof  fetch  &  jest . Mock 
45
@@ -28,7 +29,7 @@ describe('utils', () => {
2829    } ) 
2930  } ) 
3031
31-   describe ( 'fetchData ' ,  ( )  =>  { 
32+   describe ( 'getData ' ,  ( )  =>  { 
3233    it ( 'should fetch a simple url' ,  async  ( )  =>  { 
3334      fetchMock . mockImplementation ( ( )  =>  { 
3435        return  Promise . resolve ( { 
@@ -38,10 +39,60 @@ describe('utils', () => {
3839        } ) 
3940      } ) 
4041
41-       await  expect ( fetchData ( '/test/safe?q=123' ) ) . resolves . toEqual ( {  success : true  } ) 
42-       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  undefined ) 
42+       await  expect ( getData ( '/test/safe?q=123' ) ) . resolves . toEqual ( {  success : true  } ) 
43+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  {  method : 'GET'  } ) 
44+     } ) 
45+ 
46+     it ( 'should forward headers with a GET request' ,  async  ( )  =>  { 
47+       fetchMock . mockImplementation ( ( )  =>  { 
48+         return  Promise . resolve ( { 
49+           ok : true , 
50+           text : ( )  =>  Promise . resolve ( '{"success": "true"}' ) , 
51+           json : ( )  =>  Promise . resolve ( {  success : true  } ) , 
52+         } ) 
53+       } ) 
54+ 
55+       await  expect ( getData ( '/test/safe' ,  {  TestHeader : '123456'  } ) ) . resolves . toEqual ( { 
56+         success : true , 
57+       } ) 
58+ 
59+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe' ,  { 
60+         method : 'GET' , 
61+         headers : { 
62+           TestHeader : '123456' , 
63+           'Content-Type' : 'application/json' , 
64+         } , 
65+       } ) 
66+     } ) 
67+ 
68+     it ( 'should throw if response is not OK' ,  async  ( )  =>  { 
69+       fetchMock . mockImplementation ( ( )  =>  { 
70+         return  Promise . resolve ( { 
71+           ok : false , 
72+           statusText : 'Failed' , 
73+           json : ( )  =>  ( {  code : 1337 ,  message : 'something went wrong'  } ) , 
74+         } ) 
75+       } ) 
76+ 
77+       await  expect ( getData ( '/test/safe?q=123' ) ) . rejects . toThrow ( '1337: something went wrong' ) 
78+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  {  method : 'GET'  } ) 
79+     } ) 
80+ 
81+     it ( 'should throw the response text for 50x errors' ,  async  ( )  =>  { 
82+       fetchMock . mockImplementation ( ( )  =>  { 
83+         return  Promise . resolve ( { 
84+           ok : false , 
85+           statusText : 'Failed' , 
86+           json : ( )  =>  null , 
87+         } ) 
88+       } ) 
89+ 
90+       await  expect ( getData ( '/test/safe?q=123' ) ) . rejects . toThrow ( 'Failed' ) 
91+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  {  method : 'GET'  } ) 
4392    } ) 
93+   } ) 
4494
95+   describe ( 'fetchData' ,  ( )  =>  { 
4596    it ( 'should make a post request' ,  async  ( )  =>  { 
4697      fetchMock . mockImplementation ( ( )  =>  { 
4798        return  Promise . resolve ( { 
@@ -62,7 +113,7 @@ describe('utils', () => {
62113      } ) 
63114    } ) 
64115
65-     it ( 'should forward headers' ,  async  ( )  =>  { 
116+     it ( 'should forward headers with a POST request ' ,  async  ( )  =>  { 
66117      fetchMock . mockImplementation ( ( )  =>  { 
67118        return  Promise . resolve ( { 
68119          ok : true , 
@@ -103,31 +154,43 @@ describe('utils', () => {
103154        } , 
104155      } ) 
105156    } ) 
157+   } ) 
106158
107-     it ( 'should throw if response is not OK' ,  async  ( )  =>  { 
159+   describe ( 'deleteData' ,  ( )  =>  { 
160+     it ( 'should make a DELETE request' ,  async  ( )  =>  { 
108161      fetchMock . mockImplementation ( ( )  =>  { 
109162        return  Promise . resolve ( { 
110-           ok : false , 
111-           statusText :  'Failed' , 
112-           json : ( )  =>  ( {  code :  1337 ,   message :  'something went wrong'  } ) , 
163+           ok : true , 
164+           text :  ( )   =>   Promise . resolve ( '{"success": "true"}' ) , 
165+           json : ( )  =>  Promise . resolve ( {  success :  true  } ) , 
113166        } ) 
114167      } ) 
115168
116-       await  expect ( fetchData ( '/test/safe?q=123' ) ) . rejects . toThrow ( '1337: something went wrong' ) 
117-       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  undefined ) 
169+       await  expect ( deleteData ( '/test/safe' ) ) . resolves . toEqual ( {  success : true  } ) 
170+ 
171+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe' ,  { 
172+         method : 'DELETE' , 
173+       } ) 
118174    } ) 
119175
120-     it ( 'should throw the response text for 50x errors ' ,  async  ( )  =>  { 
176+     it ( 'should make a DELETE request and pass headers ' ,  async  ( )  =>  { 
121177      fetchMock . mockImplementation ( ( )  =>  { 
122178        return  Promise . resolve ( { 
123-           ok : false , 
124-           statusText :  'Failed' , 
125-           json : ( )  =>  null , 
179+           ok : true , 
180+           text :  ( )   =>   Promise . resolve ( '{"success": "true"}' ) , 
181+           json : ( )  =>  Promise . resolve ( {   success :  true   } ) , 
126182        } ) 
127183      } ) 
128184
129-       await  expect ( fetchData ( '/test/safe?q=123' ) ) . rejects . toThrow ( 'Failed' ) 
130-       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe?q=123' ,  undefined ) 
185+       await  expect ( deleteData ( '/test/safe' ,  {  TestHeader : '123456'  } ) ) . resolves . toEqual ( {  success : true  } ) 
186+ 
187+       expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe' ,  { 
188+         method : 'DELETE' , 
189+         headers : { 
190+           TestHeader : '123456' , 
191+           'Content-Type' : 'application/json' , 
192+         } , 
193+       } ) 
131194    } ) 
132195  } ) 
133196} ) 
0 commit comments