Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Fetch: Fix error on empty OPTIONS preload data #14714

Merged
merged 1 commit into from
Mar 30, 2019
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
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 );
} );
} );
} );
} );