Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload: add testing for post editor and pages in site editor #67511

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 69 additions & 30 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' );
} );
Expand All @@ -12,42 +46,47 @@ 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.beforeEach( async ( { requestUtils } ) => {
await requestUtils.resetPreferences();
} );

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

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