Skip to content

Commit

Permalink
Api Fetch: Fix metaboxes save request (parse: false) (#8002)
Browse files Browse the repository at this point in the history
* Api Fetch: Fix metaboxes save request (parse: false)

* Adding unit tests to the different cases of apiFetch
  • Loading branch information
youknowriad authored Jul 18, 2018
1 parent 322ed07 commit b06a335
Showing 3 changed files with 118 additions and 9 deletions.
1 change: 1 addition & 0 deletions edit-post/store/effects.js
Original file line number Diff line number Diff line change
@@ -115,6 +115,7 @@ const effects = {
url: window._wpMetaBoxUrl,
method: 'POST',
body: formData,
parse: false,
} )
.then( () => store.dispatch( metaBoxUpdatesSuccess() ) );
},
29 changes: 20 additions & 9 deletions packages/api-fetch/src/index.js
Original file line number Diff line number Diff line change
@@ -44,34 +44,45 @@ function apiFetch( options ) {
};

const parseResponse = ( response ) => {
return parse ? response.json() : response;
if ( parse ) {
return response.json ? response.json() : Promise.reject( response );
}

return response;
};

return responsePromise
.then( checkStatus )
.then( parseResponse )
.catch( ( response ) => {
if ( ! parse ) {
return Promise.reject( response );
throw response;
}

const invalidJsonError = {
code: 'invalid_json',
message: __( 'The response is not a valid JSON response.' ),
};

if ( ! response || ! response.json ) {
throw invalidJsonError;
}

return response.json()
.catch( () => {
throw invalidJsonError;
} )
.then( ( error ) => {
const unknownError = {
code: 'unknown_error',
message: __( 'An unknown error occurred.' ),
};

return Promise.reject( error || unknownError );
} )
.catch( () => {
return Promise.reject( {
code: 'invalid_json',
message: __( 'The response is not a valid JSON response.' ),
} );
throw error || unknownError;
} );
} );
};

const steps = [
raw,
httpV1Middleware,
97 changes: 97 additions & 0 deletions packages/api-fetch/src/test/index.js
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,
} );
} );
} );
} );

0 comments on commit b06a335

Please sign in to comment.