Skip to content

Commit

Permalink
API Fetch: Fix error on empty OPTIONS preload data (#14714)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored and youknowriad committed Mar 30, 2019
1 parent 580fedd commit 8d8a285
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
6 changes: 5 additions & 1 deletion packages/api-fetch/src/middlewares/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const createPreloadingMiddleware = ( preloadedData ) => ( options, next ) => {

if ( parse && 'GET' === method && preloadedData[ path ] ) {
return Promise.resolve( preloadedData[ path ].body );
} else if ( 'OPTIONS' === method && preloadedData[ method ][ path ] ) {
} else if (
'OPTIONS' === method &&
preloadedData[ method ] &&
preloadedData[ method ][ path ]
) {
return Promise.resolve( preloadedData[ method ][ path ] );
}
}
Expand Down
35 changes: 22 additions & 13 deletions packages/api-fetch/src/middlewares/test/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ describe( 'Preloading Middleware', () => {
} );
} );

it( 'should move to the next middleware if no preloaded data', () => {
const preloadedData = {};
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData );
const requestOptions = {
method: 'GET',
path: 'wp/v2/posts',
};
describe.each( [
[ 'GET' ],
[ 'OPTIONS' ],
] )( '%s', ( method ) => {
describe.each( [
[ 'all empty', {} ],
[ 'method empty', { [ method ]: {} } ],
] )( '%s', ( label, preloadedData ) => {
it( 'should move to the next middleware if no preloaded data', () => {
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData );
const requestOptions = {
method,
path: 'wp/v2/posts',
};

const callback = ( options ) => {
expect( options ).toBe( requestOptions );
return true;
};
const callback = ( options ) => {
expect( options ).toBe( requestOptions );
return true;
};

const ret = prelooadingMiddleware( requestOptions, callback );
expect( ret ).toBe( true );
const ret = prelooadingMiddleware( requestOptions, callback );
expect( ret ).toBe( true );
} );
} );
} );
} );

0 comments on commit 8d8a285

Please sign in to comment.