diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index b33bb42e653379..c82bf5677788ce 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -409,15 +409,15 @@ export const getAutosave = export const __experimentalGetTemplateForLink = ( link ) => async ( { dispatch, resolveSelect } ) => { - // Ideally this should be using an apiFetch call - // We could potentially do so by adding a "filter" to the `wp_template` end point. - // Also it seems the returned object is not a regular REST API post type. let template; try { - template = await window - .fetch( addQueryArgs( link, { '_wp-find-template': true } ) ) - .then( ( res ) => res.json() ) - .then( ( { data } ) => data ); + // This is NOT calling a REST endpoint but rather ends up with a response from + // an Ajax function which has a different shape from a WP_REST_Response. + template = await apiFetch( { + url: addQueryArgs( link, { + '_wp-find-template': true, + } ), + } ).then( ( { data } ) => data ); } catch ( e ) { // For non-FSE themes, it is possible that this request returns an error. } diff --git a/packages/edit-site/src/store/test/actions.js b/packages/edit-site/src/store/test/actions.js index 48b7d1ed71d70d..2df1cc72b66115 100644 --- a/packages/edit-site/src/store/test/actions.js +++ b/packages/edit-site/src/store/test/actions.js @@ -168,21 +168,13 @@ describe( 'actions', () => { const ID = 'emptytheme//single'; const SLUG = 'single'; - window.fetch = async ( path ) => { - if ( path === '/?_wp-find-template=true' ) { - return { - json: async () => ( { data: { id: ID, slug: SLUG } } ), - }; - } - - throw { - code: 'unknown_path', - message: `Unknown path: ${ path }`, - }; - }; - apiFetch.setFetchHandler( async ( options ) => { - const { method = 'GET', path } = options; + const { method = 'GET', path, url } = options; + + // Called with url arg in `__experimentalGetTemplateForLink` + if ( url ) { + return { data: { id: ID, slug: SLUG } }; + } if ( method === 'GET' ) { if ( path.startsWith( '/wp/v2/types' ) ) {