Skip to content

Commit

Permalink
Preload: fix e2e test (WordPress#67497)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and im3dabasia committed Dec 4, 2024
1 parent 74095db commit 2619297
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions backport-changelog/6.8/7687.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/7687

* https://github.com/WordPress/gutenberg/pull/66488
* https://github.com/WordPress/gutenberg/pull/67497
3 changes: 3 additions & 0 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {
*/
$context = current_user_can( 'edit_theme_options' ) ? 'edit' : 'view';
$paths[] = "/wp/v2/global-styles/$global_styles_id?context=$context";

// Used by getBlockPatternCategories in useBlockEditorSettings.
$paths[] = '/wp/v2/block-patterns/categories';
}
return $paths;
}
Expand Down
44 changes: 28 additions & 16 deletions test/e2e/specs/site-editor/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,38 @@ test.describe( 'Preload', () => {
page,
admin,
} ) => {
// Do not use `visitSiteEditor` because it waits for the iframe to load.
await admin.visitAdminPage( 'site-editor.php' );

const requests = [];
let isLoaded = false;

page.on( 'request', ( request ) => {
if ( request.resourceType() === 'document' ) {
// The iframe also "requests" a blob document. This is the most
// reliable way to wait for the iframe to start loading.
// `waitForSelector` is always a bit delayed, and we don't want
// to detect requests after the iframe mounts.
isLoaded = true;
} else if ( ! isLoaded && request.resourceType() === 'fetch' ) {
requests.push( request.url() );
function onRequest( request ) {
if (
request.resourceType() === 'document' &&
request.url().startsWith( 'blob:' )
) {
// Stop recording when the iframe is initialized.
page.off( 'request', onRequest );
} else if ( request.resourceType() === 'fetch' ) {
const url = request.url();
const urlObject = new URL( url );
const restRoute = urlObject.searchParams.get( 'rest_route' );
if ( restRoute ) {
requests.push( restRoute );
} else {
requests.push( url );
}
}
} );
}

page.on( 'request', onRequest );

await page.waitForFunction( ( _isLoaded ) => _isLoaded, [ isLoaded ] );
await admin.visitSiteEditor();

expect( requests ).toEqual( [] );
// To do: these should all be removed or preloaded.
expect( requests ).toEqual( [
// There are two separate settings OPTIONS requests. We should fix
// so the one for canUser and getEntityRecord are reused.
'/wp/v2/settings',
// Seems to be coming from `enableComplementaryArea`.
'/wp/v2/users/me',
] );
} );
} );

0 comments on commit 2619297

Please sign in to comment.