312 lines
11 KiB
JavaScript
312 lines
11 KiB
JavaScript
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',
|
|
artAsset: 'the-no-names-musician-cutout.png',
|
|
contentCopy: 'The No Names es una banda activa',
|
|
supportingCopy: 'El proceso usa generative AI',
|
|
},
|
|
{
|
|
label: 'Podcasts',
|
|
patternTitle: 'Podcast landing',
|
|
pageSlug: 'podcasts',
|
|
patternSlug: 'chrnx/podcast-landing',
|
|
atmosphereAsset: 'podcast-frequency-grunge.png',
|
|
artAsset: 'alter-ego-podcasts.png',
|
|
contentCopy: 'Conversations about the work behind software, research, music, and games.',
|
|
supportingCopy: '<!-- wp:chrnx/latest-podcast /-->',
|
|
},
|
|
];
|
|
|
|
async function logIn( page ) {
|
|
await page.goto( '/wp-login.php' );
|
|
await page.locator( '#user_login' ).fill( 'admin' );
|
|
await page.locator( '#user_pass' ).fill( 'password' );
|
|
await page.getByRole( 'button', { name: /log in/i } ).click();
|
|
if ( ! new URL( page.url() ).pathname.startsWith( '/wp-admin/' ) ) {
|
|
await page.locator( '#user_login' ).fill( 'admin' );
|
|
await page.locator( '#user_pass' ).fill( 'password' );
|
|
await page.getByRole( 'button', { name: /log in/i } ).click();
|
|
}
|
|
await page.waitForURL( ( url ) => url.pathname.startsWith( '/wp-admin/' ) );
|
|
}
|
|
|
|
async function closeWelcomeGuide( page ) {
|
|
const welcomeDialog = page.getByRole( 'dialog', {
|
|
name: /^Welcome to the (?:site )?editor$/,
|
|
} );
|
|
const visible = await welcomeDialog
|
|
.waitFor( { state: 'visible', timeout: 3000 } )
|
|
.then( () => true )
|
|
.catch( () => false );
|
|
if ( visible ) {
|
|
const close = welcomeDialog.getByRole( 'button', { name: 'Close' } );
|
|
if ( await close.isVisible() ) {
|
|
await close.click();
|
|
} else {
|
|
await welcomeDialog.getByRole( 'button', { name: 'Get started' } ).click();
|
|
}
|
|
}
|
|
}
|
|
|
|
async function waitForEditor( page ) {
|
|
await expect
|
|
.poll( () =>
|
|
page.evaluate( () => {
|
|
const editor = window.wp?.data?.select( 'core/block-editor' );
|
|
return typeof editor?.getBlocks === 'function' && editor.getBlocks().length > 0;
|
|
} ),
|
|
)
|
|
.toBe( true );
|
|
}
|
|
|
|
async function waitForPatternEditorState( page ) {
|
|
await expect
|
|
.poll(
|
|
() =>
|
|
page.evaluate( () => {
|
|
const store = window.wp.data.select( 'core/block-editor' );
|
|
const flatten = ( blocks ) =>
|
|
blocks.flatMap( ( block ) => [ block, ...flatten( block.innerBlocks || [] ) ] );
|
|
const all = flatten( store.getBlocks() );
|
|
const stage = all.find(
|
|
( block ) =>
|
|
block.name === 'core/group' &&
|
|
block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ),
|
|
);
|
|
if ( ! stage ) {
|
|
return [];
|
|
}
|
|
return stage.innerBlocks.map( ( block ) => {
|
|
const source = window.wp.blocks.serialize( block );
|
|
return [ 'atmosphere', 'content', 'art' ].find( ( layer ) =>
|
|
source.includes( `chrnx-character-stage__${ layer }` ),
|
|
);
|
|
} );
|
|
} ),
|
|
{ timeout: 10_000 },
|
|
)
|
|
.toEqual( [ 'atmosphere', 'content', 'art' ] );
|
|
}
|
|
|
|
async function editorSnapshot( page ) {
|
|
return page.evaluate( () => {
|
|
const store = window.wp.data.select( 'core/block-editor' );
|
|
const roots = store.getBlocks();
|
|
const flatten = ( blocks ) =>
|
|
blocks.flatMap( ( block ) => [ block, ...flatten( block.innerBlocks || [] ) ] );
|
|
const all = flatten( roots );
|
|
const stage = all.find(
|
|
( block ) =>
|
|
block.name === 'core/group' &&
|
|
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 ) =>
|
|
source.includes( `chrnx-character-stage__${ layer }` ),
|
|
);
|
|
};
|
|
|
|
return {
|
|
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
|
|
? Object.fromEntries(
|
|
stage.innerBlocks.map( ( block ) => [
|
|
layerName( block ),
|
|
window.wp.blocks.serialize( block ),
|
|
] ),
|
|
)
|
|
: {},
|
|
serialized,
|
|
};
|
|
} );
|
|
}
|
|
|
|
async function expectNoRecoveryUi( page ) {
|
|
await expect( page.getByText( /unexpected or invalid content/i ) ).toHaveCount( 0 );
|
|
await expect( page.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ) ).toHaveCount(
|
|
0,
|
|
);
|
|
const canvas = page.locator( 'iframe' ).contentFrame();
|
|
await expect( canvas.getByText( /unexpected or invalid content/i ) ).toHaveCount( 0 );
|
|
await expect(
|
|
canvas.getByRole( 'button', { name: /attempt (?:block )?recovery/i } ),
|
|
).toHaveCount( 0 );
|
|
}
|
|
|
|
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 );
|
|
expect( snapshot.layerMarkup.content ).toContain( pattern.contentCopy );
|
|
expect( snapshot.layerMarkup.content ).not.toContain( pattern.atmosphereAsset );
|
|
expect( snapshot.layerMarkup.art ).toContain( pattern.artAsset );
|
|
expect( snapshot.serialized ).toContain( pattern.supportingCopy );
|
|
}
|
|
|
|
async function getTemplateSnapshot( page ) {
|
|
return page.evaluate( async () => {
|
|
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,
|
|
} ) => {
|
|
await logIn( page );
|
|
const pagesResponse = await page.request.get(
|
|
`/wp-json/wp/v2/pages?slug=${ pattern.pageSlug }&_fields=id`,
|
|
);
|
|
expect( pagesResponse.ok() ).toBeTruthy();
|
|
const [ seededPage ] = await pagesResponse.json();
|
|
expect( seededPage?.id ).toBeTruthy();
|
|
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 );
|
|
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 );
|
|
await page.goto(
|
|
'/wp-admin/site-editor.php?postType=wp_template&postId=chrnx%2F%2Fpage&canvas=edit',
|
|
);
|
|
await closeWelcomeGuide( page );
|
|
await waitForEditor( page );
|
|
const templateBefore = await getTemplateSnapshot( page );
|
|
const initialEditor = await editorSnapshot( page );
|
|
const initialRecoveryCount = await recoveryUiCount( page );
|
|
const guard = watchPersistenceRequests( page );
|
|
|
|
await insertPatternThroughUi( page, pattern );
|
|
await waitForPatternEditorState( page );
|
|
|
|
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( [] );
|
|
} );
|
|
}
|