-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Api Fetch: Fix metaboxes save request (parse: false) (#8002)
* Api Fetch: Fix metaboxes save request (parse: false) * Adding unit tests to the different cases of apiFetch
- Loading branch information
1 parent
322ed07
commit b06a335
Showing
3 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import apiFetch from '../'; | ||
|
||
describe( 'apiFetch', () => { | ||
const originalFetch = window.fetch; | ||
beforeAll( () => { | ||
window.fetch = jest.fn(); | ||
} ); | ||
|
||
afterAll( () => { | ||
window.fetch = originalFetch; | ||
} ); | ||
|
||
it( 'should call the API propertly', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 200, | ||
json() { | ||
return Promise.resolve( { message: 'ok' } ); | ||
}, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random' } ).then( ( body ) => { | ||
expect( body ).toEqual( { message: 'ok' } ); | ||
} ); | ||
} ); | ||
|
||
it( 'should return the error message properly', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 400, | ||
json() { | ||
return Promise.resolve( { | ||
code: 'bad_request', | ||
message: 'Bad Request', | ||
} ); | ||
}, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random' } ).catch( ( body ) => { | ||
expect( body ).toEqual( { | ||
code: 'bad_request', | ||
message: 'Bad Request', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
it( 'should return invalid JSON error if no json response', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 200, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random' } ).catch( ( body ) => { | ||
expect( body ).toEqual( { | ||
code: 'invalid_json', | ||
message: 'The response is not a valid JSON response.', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
it( 'should return invalid JSON error if response is not valid', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 200, | ||
json() { | ||
return Promise.reject(); | ||
}, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random' } ).catch( ( body ) => { | ||
expect( body ).toEqual( { | ||
code: 'invalid_json', | ||
message: 'The response is not a valid JSON response.', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
it( 'should not try to parse the response', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 200, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random', parse: false } ).then( ( response ) => { | ||
expect( response ).toEqual( { | ||
status: 200, | ||
} ); | ||
} ); | ||
} ); | ||
|
||
it( 'should not try to parse the error', () => { | ||
window.fetch.mockReturnValue( Promise.resolve( { | ||
status: 400, | ||
} ) ); | ||
|
||
return apiFetch( { path: '/random', parse: false } ).catch( ( response ) => { | ||
expect( response ).toEqual( { | ||
status: 400, | ||
} ); | ||
} ); | ||
} ); | ||
} ); |