test: inspect CHRNX editor block state
This commit is contained in:
2 files changed
+95
-62
No files matched your search
@@ -63,60 +63,70 @@ async function waitForEditor( page ) {
|
||||
.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,
|
||||
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 ( ! pattern ) {
|
||||
throw new Error( `Registered pattern not found: ${ block.attributes.slug }` );
|
||||
if ( ! stage ) {
|
||||
return [];
|
||||
}
|
||||
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 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' ] );
|
||||
}
|
||||
|
||||
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 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 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 ),
|
||||
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 ) {
|
||||
@@ -133,6 +143,7 @@ async function expectNoRecoveryUi( page ) {
|
||||
|
||||
function expectPatternContract( snapshot, pattern ) {
|
||||
expect( snapshot.invalid ).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 );
|
||||
@@ -173,9 +184,9 @@ for ( const pattern of patterns ) {
|
||||
} );
|
||||
await page.goto( `/wp-admin/post.php?post=${ seededPage.id }&action=edit` );
|
||||
await closeWelcomeGuide( page );
|
||||
await waitForEditor( page );
|
||||
await waitForPatternEditorState( page );
|
||||
|
||||
expectPatternContract( await editorSnapshot( page, [], true ), pattern );
|
||||
expectPatternContract( await editorSnapshot( page ), pattern );
|
||||
await expectNoRecoveryUi( page );
|
||||
expect( persistenceRequests ).toEqual( [] );
|
||||
} );
|
||||
@@ -199,20 +210,31 @@ for ( const pattern of patterns ) {
|
||||
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 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 }` );
|
||||
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 }` );
|
||||
}
|
||||
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 );
|
||||
window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( selectedPattern.blocks );
|
||||
}, pattern.patternSlug );
|
||||
await waitForPatternEditorState( page );
|
||||
|
||||
expectPatternContract( await editorSnapshot( page, insertedRootIds ), pattern );
|
||||
expectPatternContract( await editorSnapshot( page ), pattern );
|
||||
await expectNoRecoveryUi( page );
|
||||
expect( await getTemplateRowCount( page ) ).toBe( templatesBefore );
|
||||
expect( persistenceRequests ).toEqual( [] );
|
||||
|
||||
@@ -507,6 +507,17 @@ test( 'Music and Podcast patterns own one ordered three-layer landing compositio
|
||||
);
|
||||
} );
|
||||
|
||||
test( 'editor pattern validity is derived only from the live block-editor store', () => {
|
||||
const editorValidity = read( 'tests/e2e/pattern-editor-validity.spec.mjs' );
|
||||
|
||||
assert.doesNotMatch( editorValidity, /resolvePatternReferences|shouldResolvePatterns/ );
|
||||
assert.doesNotMatch(
|
||||
editorValidity,
|
||||
/editorSnapshot[\s\S]*?wp\.apiFetch\(\s*\{\s*path:\s*'\/wp\/v2\/block-patterns\/patterns'/,
|
||||
);
|
||||
assert.match( editorValidity, /const roots = store\.getBlocks\(\)/ );
|
||||
} );
|
||||
|
||||
test( 'character stages define the shared mockup effect and responsive geometry', () => {
|
||||
const components = read( 'assets/css/components.css' );
|
||||
const visualStates = read( 'tests/e2e/visual-states.spec.mjs' );
|
||||
|
||||
Reference in new issue
Block a user