From 2619297d3241110b1df85f99ff0478e97b92e757 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Mon, 2 Dec 2024 21:06:37 +0100 Subject: [PATCH] Preload: fix e2e test (#67497) --- backport-changelog/6.8/7687.md | 1 + lib/compat/wordpress-6.8/preload.php | 3 ++ test/e2e/specs/site-editor/preload.spec.js | 44 ++++++++++++++-------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/backport-changelog/6.8/7687.md b/backport-changelog/6.8/7687.md index f1505645df20c6..0b5af190964df1 100644 --- a/backport-changelog/6.8/7687.md +++ b/backport-changelog/6.8/7687.md @@ -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 diff --git a/lib/compat/wordpress-6.8/preload.php b/lib/compat/wordpress-6.8/preload.php index 879e263f5a1892..0a36ea7f7227d4 100644 --- a/lib/compat/wordpress-6.8/preload.php +++ b/lib/compat/wordpress-6.8/preload.php @@ -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; } diff --git a/test/e2e/specs/site-editor/preload.spec.js b/test/e2e/specs/site-editor/preload.spec.js index 1e93f783a8a91d..2cd61283fbd9e8 100644 --- a/test/e2e/specs/site-editor/preload.spec.js +++ b/test/e2e/specs/site-editor/preload.spec.js @@ -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', + ] ); } ); } );