diff --git a/packages/block-directory/src/store/actions.js b/packages/block-directory/src/store/actions.js index ec73abe2ffbdce..9f1e96d5db26ba 100644 --- a/packages/block-directory/src/store/actions.js +++ b/packages/block-directory/src/store/actions.js @@ -51,9 +51,6 @@ export function* installBlockType( block ) { let success = false; yield clearErrorNotice( id ); try { - if ( ! Array.isArray( assets ) || ! assets.length ) { - throw new Error( __( 'Block has no assets.' ) ); - } yield setIsInstalling( block.id, true ); // If we have a wp:plugin link, the plugin is installed but inactive. diff --git a/packages/block-directory/src/store/controls.js b/packages/block-directory/src/store/controls.js index f3f58536fa641d..a5ee96d8e6c9d4 100644 --- a/packages/block-directory/src/store/controls.js +++ b/packages/block-directory/src/store/controls.js @@ -1,50 +1,49 @@ /** * WordPress dependencies */ -import { getPath } from '@wordpress/url'; +import apiFetch from '@wordpress/api-fetch'; /** - * Loads a JavaScript file. + * Load an asset for a block. * - * @param {string} asset The url for this file. + * This function returns a Promise that will resolve once the asset is loaded, + * or in the case of Stylesheets and Inline Javascript, will resolve immediately. + * + * @param {HTMLElement} el A HTML Element asset to inject. * * @return {Promise} Promise which will resolve when the asset is loaded. */ -export const loadScript = ( asset ) => { - if ( ! asset || ! /\.js$/.test( getPath( asset ) ) ) { - return Promise.reject( new Error( 'No script found.' ) ); - } +export const loadAsset = ( el ) => { return new Promise( ( resolve, reject ) => { - const existing = document.querySelector( `script[src="${ asset }"]` ); - if ( existing ) { - existing.parentNode.removeChild( existing ); + /* + * Reconstruct the passed element, this is required as inserting the Node directly + * won't always fire the required onload events, even if the asset wasn't already loaded. + */ + const newNode = document.createElement( el.nodeName ); + + [ 'id', 'rel', 'src', 'href', 'type' ].forEach( ( attr ) => { + if ( el[ attr ] ) { + newNode[ attr ] = el[ attr ]; + } + } ); + + // Append inline `, + 'text/html; charset=UTF-8' + ), + }, ]; function getResponseObject( obj, contentType ) { @@ -180,8 +188,7 @@ describe( 'adding blocks from block directory', () => { // Add the block await addBtn.click(); - // Delay to let block script load - await new Promise( ( resolve ) => setTimeout( resolve, 100 ) ); + await page.waitForSelector( `div[data-type="${ MOCK_BLOCK1.name }"]` ); // The block will auto select and get added, make sure we see it in the content expect( await getEditedPostContent() ).toMatchSnapshot();