Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
API Fetch: Fix error on empty OPTIONS preload data
  • Loading branch information
aduth committed Mar 29, 2019
commit a7b34d31f5ef8aa744d305862bd2355585783847
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 );
} );
} );
} );
} );