Skip to content

Commit

Permalink
Preload: add testing for post editor and pages in site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 18, 2024
1 parent 2af83af commit f14aba3
Showing 1 changed file with 62 additions and 31 deletions.
93 changes: 62 additions & 31 deletions test/e2e/specs/site-editor/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,41 @@
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Preload', () => {
function recordRequests( page ) {
const requests = [];

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 path =
urlObject.searchParams.get( 'rest_route' ) ??
urlObject.pathname;
const method = request.method();
requests.push( [ method, path ] );
}
}

page.on( 'request', onRequest );

return requests;
}

const post = {
status: 'publish',
title: 'A post',
content: `<!-- wp:paragraph -->
<p>Post content</p>
<!-- /wp:paragraph -->`,
};

test.describe( 'Preload: should make no requests before the iframe is loaded', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
await requestUtils.resetPreferences();
Expand All @@ -13,42 +47,39 @@ test.describe( 'Preload', () => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );

test( 'Should make no requests before the iframe is loaded', async ( {
page,
admin,
} ) => {
const requests = [];

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 );

test( 'Site Editor Root', async ( { page, admin } ) => {
const requests = recordRequests( page );
await admin.visitSiteEditor();

// 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',
[ 'OPTIONS', '/wp/v2/settings' ],
// Seems to be coming from `enableComplementaryArea`.
'/wp/v2/users/me',
[ 'POST', '/wp/v2/users/me' ],
] );
} );

test( 'Post Editor', async ( { page, admin, requestUtils } ) => {
const requests = recordRequests( page );
const { id } = await requestUtils.createPost( post );
await admin.editPost( id );
expect( requests ).toEqual( [] );
} );

test( 'Site Editor Page', async ( { page, admin, requestUtils } ) => {
const requests = recordRequests( page );
const { id } = await requestUtils.createPage( post );
await admin.visitSiteEditor( {
postType: 'page',
postId: id,
canvas: 'edit',
} );
expect( requests ).toEqual( [
[ 'GET', '/wp/v2/types/page' ],
[ 'OPTIONS', '/wp/v2/settings' ],
[ 'GET', '/wp/v2/taxonomies' ],
[ 'POST', '/wp/v2/users/me' ],
] );
} );
} );

0 comments on commit f14aba3

Please sign in to comment.