@@ -17,22 +17,18 @@ const failWithTypeError = Promise.resolve({
1717const failWithBadStatus = Promise . resolve ( {
1818 json : ( ) =>
1919 Promise . resolve ( {
20- status : 'qwerty' , // Only 'ok' with succeed
20+ error : 'Something went wrong' ,
2121 } ) ,
2222} ) ;
2323
2424const failWithEmptyStatus = Promise . resolve ( {
2525 json : ( ) =>
26- Promise . resolve ( {
27- // No status defined will assume non-'ok'
28- } ) ,
26+ Promise . resolve ( { } ) ,
2927} ) ;
3028
3129const successResEmptyData = Promise . resolve ( {
3230 json : ( ) =>
33- Promise . resolve ( {
34- status : 'ok' ,
35- } ) ,
31+ Promise . resolve ( { } ) ,
3632} ) ;
3733
3834test ( 'Fails with TypeError if `res.json` is undefined' , async ( ) => {
@@ -42,31 +38,29 @@ test('Fails with TypeError if `res.json` is undefined', async () => {
4238 const fetchStub = jest . fn ( ) . mockImplementation ( ( ) => failWithTypeError ) ;
4339 ( fetch as any ) = fetchStub ;
4440
45- const expectedError = createServiceError ( { status : 'qwerty' } ) ;
41+ const expectedError = createServiceError ( 'TypeError: Cannot read properties of undefined (reading \'json\')' ) ;
4642
4743 await expect ( get ( URL , API_KEY ) ) . rejects . toThrow ( expectedError ) ;
4844} ) ;
4945
50- test ( 'Fails with non-OK status in response JSON ' , async ( ) => {
46+ test ( 'Returns response with error field ' , async ( ) => {
5147 const fetchStub = jest . fn ( ) . mockImplementation ( ( ) => failWithBadStatus ) ;
5248 ( fetch as any ) = fetchStub ;
5349
54- const expectedError = createServiceError ( { status : 'qwerty' } ) ;
55-
56- await expect ( get ( URL , API_KEY ) ) . rejects . toThrow ( expectedError ) ;
50+ await expect ( get ( URL , API_KEY ) ) . resolves . toEqual ( {
51+ error : 'Something went wrong' ,
52+ } ) ;
5753} ) ;
5854
59- test ( 'Succeeds with empty data in response JSON, returning `{}` as fallback ' , async ( ) => {
55+ test ( 'Succeeds with empty data in response JSON, returning response without status ' , async ( ) => {
6056 const fetchStub = jest . fn ( ) . mockImplementation ( ( ) => successResEmptyData ) ;
6157 ( fetch as any ) = fetchStub ;
6258 await expect ( get ( URL , API_KEY ) ) . resolves . toEqual ( { } ) ;
6359} ) ;
6460
65- test ( 'Fails with empty status in response' , async ( ) => {
61+ test ( 'Returns empty response object ' , async ( ) => {
6662 const fetchStub = jest . fn ( ) . mockImplementation ( ( ) => failWithEmptyStatus ) ;
6763 ( fetch as any ) = fetchStub ;
6864
69- const expectedError = createServiceError ( { status : 'qwerty' } ) ;
70-
71- expect ( get ( URL , API_KEY ) ) . rejects . toThrow ( expectedError ) ;
65+ await expect ( get ( URL , API_KEY ) ) . resolves . toEqual ( { } ) ;
7266} ) ;
0 commit comments