refactor: compose CHRNX music and podcast stages
This commit is contained in:
8 files changed
+543
-19
No files matched your search
@@ -0,0 +1,220 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
const patterns = [
|
||||
{
|
||||
label: 'Music',
|
||||
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',
|
||||
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 editorSnapshot( page, rootClientIds = [], resolvePatternReferences = false ) {
|
||||
return page.evaluate(
|
||||
async ( { insertedRootIds, shouldResolvePatterns } ) => {
|
||||
const store = window.wp.data.select( 'core/block-editor' );
|
||||
let roots = insertedRootIds.length
|
||||
? insertedRootIds.map( ( clientId ) => store.getBlock( clientId ) ).filter( Boolean )
|
||||
: store.getBlocks();
|
||||
if ( shouldResolvePatterns ) {
|
||||
const registered = await window.wp.apiFetch( { path: '/wp/v2/block-patterns/patterns' } );
|
||||
roots = roots.flatMap( ( block ) => {
|
||||
if ( block.name !== 'core/pattern' ) {
|
||||
return [ block ];
|
||||
}
|
||||
const pattern = registered.find(
|
||||
( candidate ) => candidate.name === block.attributes.slug,
|
||||
);
|
||||
if ( ! pattern ) {
|
||||
throw new Error( `Registered pattern not found: ${ block.attributes.slug }` );
|
||||
}
|
||||
return window.wp.blocks.parse( pattern.content );
|
||||
} );
|
||||
}
|
||||
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 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 ) => block.name ),
|
||||
layerOrder: stage ? stage.innerBlocks.map( layerName ) : [],
|
||||
layerMarkup: stage
|
||||
? Object.fromEntries(
|
||||
stage.innerBlocks.map( ( block ) => [
|
||||
layerName( block ),
|
||||
window.wp.blocks.serialize( block ),
|
||||
] ),
|
||||
)
|
||||
: {},
|
||||
serialized,
|
||||
};
|
||||
},
|
||||
{ insertedRootIds: rootClientIds, shouldResolvePatterns: resolvePatternReferences },
|
||||
);
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
function expectPatternContract( snapshot, pattern ) {
|
||||
expect( snapshot.invalid ).toEqual( [] );
|
||||
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 getTemplateRowCount( 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;
|
||||
} );
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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 waitForEditor( page );
|
||||
|
||||
expectPatternContract( await editorSnapshot( page, [], true ), pattern );
|
||||
await expectNoRecoveryUi( page );
|
||||
expect( 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 );
|
||||
|
||||
const insertedRootIds = await page.evaluate( async ( slug ) => {
|
||||
const registered = await window.wp.apiFetch( { path: '/wp/v2/block-patterns/patterns' } );
|
||||
const selected = registered.find( ( candidate ) => candidate.name === slug );
|
||||
if ( ! selected ) {
|
||||
throw new Error( `Registered pattern not found: ${ slug }` );
|
||||
}
|
||||
const blocks = window.wp.blocks.parse( selected.content );
|
||||
window.wp.data.dispatch( 'core/block-editor' ).resetBlocks( [] );
|
||||
window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( blocks );
|
||||
return blocks.map( ( block ) => block.clientId );
|
||||
}, pattern.patternSlug );
|
||||
|
||||
expectPatternContract( await editorSnapshot( page, insertedRootIds ), pattern );
|
||||
await expectNoRecoveryUi( page );
|
||||
expect( await getTemplateRowCount( page ) ).toBe( templatesBefore );
|
||||
expect( persistenceRequests ).toEqual( [] );
|
||||
} );
|
||||
}
|
||||
Reference in new issue
Block a user