Skip to content

Commit f14aba3

Browse files
committed
Preload: add testing for post editor and pages in site editor
1 parent 2af83af commit f14aba3

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

test/e2e/specs/site-editor/preload.spec.js

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,41 @@
33
*/
44
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
55

6-
test.describe( 'Preload', () => {
6+
function recordRequests( page ) {
7+
const requests = [];
8+
9+
function onRequest( request ) {
10+
if (
11+
request.resourceType() === 'document' &&
12+
request.url().startsWith( 'blob:' )
13+
) {
14+
// Stop recording when the iframe is initialized.
15+
page.off( 'request', onRequest );
16+
} else if ( request.resourceType() === 'fetch' ) {
17+
const url = request.url();
18+
const urlObject = new URL( url );
19+
const path =
20+
urlObject.searchParams.get( 'rest_route' ) ??
21+
urlObject.pathname;
22+
const method = request.method();
23+
requests.push( [ method, path ] );
24+
}
25+
}
26+
27+
page.on( 'request', onRequest );
28+
29+
return requests;
30+
}
31+
32+
const post = {
33+
status: 'publish',
34+
title: 'A post',
35+
content: `<!-- wp:paragraph -->
36+
<p>Post content</p>
37+
<!-- /wp:paragraph -->`,
38+
};
39+
40+
test.describe( 'Preload: should make no requests before the iframe is loaded', () => {
741
test.beforeAll( async ( { requestUtils } ) => {
842
await requestUtils.activateTheme( 'emptytheme' );
943
await requestUtils.resetPreferences();
@@ -13,42 +47,39 @@ test.describe( 'Preload', () => {
1347
await requestUtils.activateTheme( 'twentytwentyone' );
1448
} );
1549

16-
test( 'Should make no requests before the iframe is loaded', async ( {
17-
page,
18-
admin,
19-
} ) => {
20-
const requests = [];
21-
22-
function onRequest( request ) {
23-
if (
24-
request.resourceType() === 'document' &&
25-
request.url().startsWith( 'blob:' )
26-
) {
27-
// Stop recording when the iframe is initialized.
28-
page.off( 'request', onRequest );
29-
} else if ( request.resourceType() === 'fetch' ) {
30-
const url = request.url();
31-
const urlObject = new URL( url );
32-
const restRoute = urlObject.searchParams.get( 'rest_route' );
33-
if ( restRoute ) {
34-
requests.push( restRoute );
35-
} else {
36-
requests.push( url );
37-
}
38-
}
39-
}
40-
41-
page.on( 'request', onRequest );
42-
50+
test( 'Site Editor Root', async ( { page, admin } ) => {
51+
const requests = recordRequests( page );
4352
await admin.visitSiteEditor();
44-
4553
// To do: these should all be removed or preloaded.
4654
expect( requests ).toEqual( [
4755
// There are two separate settings OPTIONS requests. We should fix
4856
// so the one for canUser and getEntityRecord are reused.
49-
'/wp/v2/settings',
57+
[ 'OPTIONS', '/wp/v2/settings' ],
5058
// Seems to be coming from `enableComplementaryArea`.
51-
'/wp/v2/users/me',
59+
[ 'POST', '/wp/v2/users/me' ],
60+
] );
61+
} );
62+
63+
test( 'Post Editor', async ( { page, admin, requestUtils } ) => {
64+
const requests = recordRequests( page );
65+
const { id } = await requestUtils.createPost( post );
66+
await admin.editPost( id );
67+
expect( requests ).toEqual( [] );
68+
} );
69+
70+
test( 'Site Editor Page', async ( { page, admin, requestUtils } ) => {
71+
const requests = recordRequests( page );
72+
const { id } = await requestUtils.createPage( post );
73+
await admin.visitSiteEditor( {
74+
postType: 'page',
75+
postId: id,
76+
canvas: 'edit',
77+
} );
78+
expect( requests ).toEqual( [
79+
[ 'GET', '/wp/v2/types/page' ],
80+
[ 'OPTIONS', '/wp/v2/settings' ],
81+
[ 'GET', '/wp/v2/taxonomies' ],
82+
[ 'POST', '/wp/v2/users/me' ],
5283
] );
5384
} );
5485
} );

0 commit comments

Comments
 (0)