From 8d8a2854d797460fb04f472f648d5f25a0f8f264 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Sat, 30 Mar 2019 09:52:33 -0400 Subject: [PATCH] API Fetch: Fix error on empty OPTIONS preload data (#14714) --- .../api-fetch/src/middlewares/preloading.js | 6 +++- .../src/middlewares/test/preloading.js | 35 ++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index 020cb913fced1..4de356243b736 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -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 ] ); } } diff --git a/packages/api-fetch/src/middlewares/test/preloading.js b/packages/api-fetch/src/middlewares/test/preloading.js index d697af3b42949..e2d8a1f4d0c68 100644 --- a/packages/api-fetch/src/middlewares/test/preloading.js +++ b/packages/api-fetch/src/middlewares/test/preloading.js @@ -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 ); + } ); + } ); } ); } );