test: exercise CHRNX editor workflows

This commit is contained in:
chrnx committed 2026-08-13 07:57:04 -05:00
1 parent 03aca3beff
commit 62df36ef81
3 files changed
+204 -61

No files matched your search

+123 -54
View File
@@ -3,6 +3,7 @@ import { expect, test } from '@playwright/test';
const patterns = [
{
label: 'Music',
patternTitle: 'Music landing',
pageSlug: 'music',
patternSlug: 'chrnx/music-landing',
atmosphereAsset: 'the-no-names-collage.jpeg',
@@ -12,6 +13,7 @@ const patterns = [
},
{
label: 'Podcasts',
patternTitle: 'Podcast landing',
pageSlug: 'podcasts',
patternSlug: 'chrnx/podcast-landing',
atmosphereAsset: 'podcast-frequency-grunge.png',
@@ -105,6 +107,7 @@ async function editorSnapshot( page ) {
block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ),
);
const serialized = window.wp.blocks.serialize( roots );
const stageBlocks = stage ? flatten( [ stage ] ) : [];
const layerName = ( block ) => {
const source = window.wp.blocks.serialize( block );
return [ 'atmosphere', 'content', 'art' ].find( ( layer ) =>
@@ -113,7 +116,15 @@ async function editorSnapshot( page ) {
};
return {
invalid: all.filter( ( block ) => block.isValid === false ).map( ( block ) => block.name ),
invalid: all
.filter( ( block ) => block.isValid === false )
.map( ( block ) => ( {
clientId: block.clientId,
name: block.name,
} ) ),
stageInvalid: stageBlocks
.filter( ( block ) => block.isValid === false )
.map( ( block ) => block.name ),
hasPatternReference: all.some( ( block ) => block.name === 'core/pattern' ),
layerOrder: stage ? stage.innerBlocks.map( layerName ) : [],
layerMarkup: stage
@@ -141,8 +152,21 @@ async function expectNoRecoveryUi( page ) {
).toHaveCount( 0 );
}
function expectPatternContract( snapshot, pattern ) {
expect( snapshot.invalid ).toEqual( [] );
async function recoveryUiCount( page ) {
const canvas = page.locator( 'iframe[name="editor-canvas"]' ).contentFrame();
return (
( await page.getByText( /unexpected or invalid content/i ).count() ) +
( await page.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ).count() ) +
( await canvas.getByText( /unexpected or invalid content/i ).count() ) +
( await canvas.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ).count() )
);
}
function expectPatternContract( snapshot, pattern, { requireDocumentValidity = true } = {} ) {
if ( requireDocumentValidity ) {
expect( snapshot.invalid ).toEqual( [] );
}
expect( snapshot.stageInvalid ).toEqual( [] );
expect( snapshot.hasPatternReference ).toBeFalsy();
expect( snapshot.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] );
expect( snapshot.layerMarkup.atmosphere ).toContain( pattern.atmosphereAsset );
@@ -152,15 +176,81 @@ function expectPatternContract( snapshot, pattern ) {
expect( snapshot.serialized ).toContain( pattern.supportingCopy );
}
async function getTemplateRowCount( page ) {
async function getTemplateSnapshot( page ) {
return page.evaluate( async () => {
const templates = await window.wp.apiFetch( {
path: '/wp/v2/templates?context=edit&per_page=100&_fields=wp_id',
} );
return templates.filter( ( template ) => Number( template.wp_id ) > 0 ).length;
const [ template, templates ] = await Promise.all( [
window.wp.apiFetch( {
path: '/wp/v2/templates/chrnx//page?context=edit&_fields=id,wp_id,source,modified,content',
} ),
window.wp.apiFetch( {
path: '/wp/v2/templates?context=edit&per_page=100&_fields=id,wp_id,source,modified,content',
} ),
] );
return {
template,
persistedRows: templates
.filter( ( candidate ) => Number( candidate.wp_id ) > 0 )
.sort( ( left, right ) => left.id.localeCompare( right.id ) ),
};
} );
}
async function getPageSnapshot( page, pageId ) {
return page.evaluate(
( id ) =>
window.wp.apiFetch( {
path: `/wp/v2/pages/${ id }?context=edit&_fields=id,slug,status,modified,content`,
} ),
pageId,
);
}
function watchPersistenceRequests( page ) {
const persistenceRequests = [];
const writeMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE' ];
const listener = ( request ) => {
if ( writeMethods.includes( request.method() ) ) {
persistenceRequests.push( `${ request.method() } ${ request.url() }` );
}
};
page.on( 'request', listener );
return {
persistenceRequests,
stop: () => page.off( 'request', listener ),
};
}
async function waitForEditorToSettle( page, { expectDirty } ) {
await expect
.poll( () =>
page.evaluate( () => {
const editor = window.wp.data.select( 'core/editor' );
return {
autosaving: editor.isAutosavingPost(),
saving: editor.isSavingPost(),
};
} ),
)
.toEqual( { autosaving: false, saving: false } );
await expect
.poll( () => page.evaluate( () => window.wp.data.select( 'core/editor' ).isEditedPostDirty() ) )
.toBe( expectDirty );
}
async function insertPatternThroughUi( page, pattern ) {
await page.getByRole( 'button', { name: 'Block Inserter' } ).click();
// Searching from Blocks avoids the Site Editor's pattern-tab zoom mode, which
// intentionally scopes insertion to the existing <main> section.
await page.getByRole( 'tab', { name: 'Blocks', exact: true } ).click();
await page.locator( 'input[type="search"]:visible' ).fill( pattern.patternTitle );
const patternOption = page.getByRole( 'option', {
name: pattern.patternTitle,
exact: true,
} );
await expect( patternOption ).toBeVisible();
await patternOption.click();
}
for ( const pattern of patterns ) {
test( `${ pattern.label } Post Editor keeps the seeded pattern valid without saving`, async ( {
page,
@@ -172,71 +262,50 @@ for ( const pattern of patterns ) {
expect( pagesResponse.ok() ).toBeTruthy();
const [ seededPage ] = await pagesResponse.json();
expect( seededPage?.id ).toBeTruthy();
const persistenceRequests = [];
page.on( 'request', ( request ) => {
if (
! [ 'GET', 'HEAD', 'OPTIONS' ].includes( request.method() ) &&
/\/wp-json\/wp\/v2\/(?:pages|templates)|\/wp-admin\/post\.php/.test( request.url() )
) {
persistenceRequests.push( `${ request.method() } ${ request.url() }` );
}
} );
await page.goto( `/wp-admin/post.php?post=${ seededPage.id }&action=edit` );
await closeWelcomeGuide( page );
await waitForPatternEditorState( page );
const pageBefore = await getPageSnapshot( page, seededPage.id );
const guard = watchPersistenceRequests( page );
expectPatternContract( await editorSnapshot( page ), pattern );
await expectNoRecoveryUi( page );
expect( persistenceRequests ).toEqual( [] );
await waitForEditorToSettle( page, { expectDirty: false } );
const pageAfter = await getPageSnapshot( page, seededPage.id );
expect( pageAfter ).toEqual( pageBefore );
await page.close( { runBeforeUnload: false } );
guard.stop();
expect( guard.persistenceRequests ).toEqual( [] );
} );
test( `${ pattern.label } Site Editor inserts the registered pattern validly without saving`, async ( {
page,
} ) => {
await logIn( page );
const persistenceRequests = [];
page.on( 'request', ( request ) => {
if (
! [ 'GET', 'HEAD', 'OPTIONS' ].includes( request.method() ) &&
/\/wp-json\/wp\/v2\/(?:pages|templates)|\/wp-admin\/post\.php/.test( request.url() )
) {
persistenceRequests.push( `${ request.method() } ${ request.url() }` );
}
} );
await page.goto(
'/wp-admin/site-editor.php?postType=wp_template&postId=chrnx%2F%2Fpage&canvas=edit',
);
await closeWelcomeGuide( page );
await waitForEditor( page );
const templatesBefore = await getTemplateRowCount( page );
await expect
.poll(
() =>
page.evaluate( ( slug ) => {
const selectedPattern = window.wp.data
.select( 'core/block-editor' )
.__experimentalGetParsedPattern( slug );
return selectedPattern?.blocks?.length ?? 0;
}, pattern.patternSlug ),
{ timeout: 10_000 },
)
.toBeGreaterThan( 0 );
const templateBefore = await getTemplateSnapshot( page );
const initialEditor = await editorSnapshot( page );
const initialRecoveryCount = await recoveryUiCount( page );
const guard = watchPersistenceRequests( page );
await page.evaluate( ( slug ) => {
const store = window.wp.data.select( 'core/block-editor' );
const selectedPattern = store.__experimentalGetParsedPattern( slug );
if ( ! selectedPattern?.blocks?.length ) {
throw new Error( `Parsed editor pattern not found: ${ slug }` );
}
window.wp.data.dispatch( 'core/block-editor' ).resetBlocks( [] );
window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( selectedPattern.blocks );
}, pattern.patternSlug );
await insertPatternThroughUi( page, pattern );
await waitForPatternEditorState( page );
expectPatternContract( await editorSnapshot( page ), pattern );
await expectNoRecoveryUi( page );
expect( await getTemplateRowCount( page ) ).toBe( templatesBefore );
expect( persistenceRequests ).toEqual( [] );
const insertedEditor = await editorSnapshot( page );
expectPatternContract( insertedEditor, pattern, {
requireDocumentValidity: false,
} );
expect( insertedEditor.invalid ).toEqual( initialEditor.invalid );
expect( await recoveryUiCount( page ) ).toBe( initialRecoveryCount );
await waitForEditorToSettle( page, { expectDirty: true } );
const templateAfter = await getTemplateSnapshot( page );
expect( templateAfter ).toEqual( templateBefore );
await page.close( { runBeforeUnload: false } );
guard.stop();
expect( guard.persistenceRequests ).toEqual( [] );
} );
}