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 );
|
.toBe( true );
|
||||||
}
|
}
|
||||||
|
|
||||||
async function editorSnapshot( page, rootClientIds = [], resolvePatternReferences = false ) {
|
async function waitForPatternEditorState( page ) {
|
||||||
return page.evaluate(
|
await expect
|
||||||
async ( { insertedRootIds, shouldResolvePatterns } ) => {
|
.poll(
|
||||||
const store = window.wp.data.select( 'core/block-editor' );
|
() =>
|
||||||
let roots = insertedRootIds.length
|
page.evaluate( () => {
|
||||||
? insertedRootIds.map( ( clientId ) => store.getBlock( clientId ) ).filter( Boolean )
|
const store = window.wp.data.select( 'core/block-editor' );
|
||||||
: store.getBlocks();
|
const flatten = ( blocks ) =>
|
||||||
if ( shouldResolvePatterns ) {
|
blocks.flatMap( ( block ) => [ block, ...flatten( block.innerBlocks || [] ) ] );
|
||||||
const registered = await window.wp.apiFetch( { path: '/wp/v2/block-patterns/patterns' } );
|
const all = flatten( store.getBlocks() );
|
||||||
roots = roots.flatMap( ( block ) => {
|
const stage = all.find(
|
||||||
if ( block.name !== 'core/pattern' ) {
|
( block ) =>
|
||||||
return [ block ];
|
block.name === 'core/group' &&
|
||||||
}
|
block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ),
|
||||||
const pattern = registered.find(
|
|
||||||
( candidate ) => candidate.name === block.attributes.slug,
|
|
||||||
);
|
);
|
||||||
if ( ! pattern ) {
|
if ( ! stage ) {
|
||||||
throw new Error( `Registered pattern not found: ${ block.attributes.slug }` );
|
return [];
|
||||||
}
|
}
|
||||||
return window.wp.blocks.parse( pattern.content );
|
return stage.innerBlocks.map( ( block ) => {
|
||||||
} );
|
const source = window.wp.blocks.serialize( block );
|
||||||
}
|
return [ 'atmosphere', 'content', 'art' ].find( ( layer ) =>
|
||||||
const flatten = ( blocks ) =>
|
source.includes( `chrnx-character-stage__${ layer }` ),
|
||||||
blocks.flatMap( ( block ) => [ block, ...flatten( block.innerBlocks || [] ) ] );
|
);
|
||||||
const all = flatten( roots );
|
} );
|
||||||
const stage = all.find(
|
} ),
|
||||||
( block ) =>
|
{ timeout: 10_000 },
|
||||||
block.name === 'core/group' &&
|
)
|
||||||
block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ),
|
.toEqual( [ 'atmosphere', 'content', 'art' ] );
|
||||||
);
|
}
|
||||||
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 {
|
async function editorSnapshot( page ) {
|
||||||
invalid: all.filter( ( block ) => block.isValid === false ).map( ( block ) => block.name ),
|
return page.evaluate( () => {
|
||||||
layerOrder: stage ? stage.innerBlocks.map( layerName ) : [],
|
const store = window.wp.data.select( 'core/block-editor' );
|
||||||
layerMarkup: stage
|
const roots = store.getBlocks();
|
||||||
? Object.fromEntries(
|
const flatten = ( blocks ) =>
|
||||||
stage.innerBlocks.map( ( block ) => [
|
blocks.flatMap( ( block ) => [ block, ...flatten( block.innerBlocks || [] ) ] );
|
||||||
layerName( block ),
|
const all = flatten( roots );
|
||||||
window.wp.blocks.serialize( block ),
|
const stage = all.find(
|
||||||
] ),
|
( block ) =>
|
||||||
)
|
block.name === 'core/group' &&
|
||||||
: {},
|
block.attributes?.className?.split( /\s+/ ).includes( 'chrnx-character-stage' ),
|
||||||
serialized,
|
);
|
||||||
};
|
const serialized = window.wp.blocks.serialize( roots );
|
||||||
},
|
const layerName = ( block ) => {
|
||||||
{ insertedRootIds: rootClientIds, shouldResolvePatterns: resolvePatternReferences },
|
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 ) {
|
async function expectNoRecoveryUi( page ) {
|
||||||
@@ -133,6 +143,7 @@ async function expectNoRecoveryUi( page ) {
|
|||||||
|
|
||||||
function expectPatternContract( snapshot, pattern ) {
|
function expectPatternContract( snapshot, pattern ) {
|
||||||
expect( snapshot.invalid ).toEqual( [] );
|
expect( snapshot.invalid ).toEqual( [] );
|
||||||
|
expect( snapshot.hasPatternReference ).toBeFalsy();
|
||||||
expect( snapshot.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] );
|
expect( snapshot.layerOrder ).toEqual( [ 'atmosphere', 'content', 'art' ] );
|
||||||
expect( snapshot.layerMarkup.atmosphere ).toContain( pattern.atmosphereAsset );
|
expect( snapshot.layerMarkup.atmosphere ).toContain( pattern.atmosphereAsset );
|
||||||
expect( snapshot.layerMarkup.content ).toContain( pattern.contentCopy );
|
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 page.goto( `/wp-admin/post.php?post=${ seededPage.id }&action=edit` );
|
||||||
await closeWelcomeGuide( page );
|
await closeWelcomeGuide( page );
|
||||||
await waitForEditor( page );
|
await waitForPatternEditorState( page );
|
||||||
|
|
||||||
expectPatternContract( await editorSnapshot( page, [], true ), pattern );
|
expectPatternContract( await editorSnapshot( page ), pattern );
|
||||||
await expectNoRecoveryUi( page );
|
await expectNoRecoveryUi( page );
|
||||||
expect( persistenceRequests ).toEqual( [] );
|
expect( persistenceRequests ).toEqual( [] );
|
||||||
} );
|
} );
|
||||||
@@ -199,20 +210,31 @@ for ( const pattern of patterns ) {
|
|||||||
await closeWelcomeGuide( page );
|
await closeWelcomeGuide( page );
|
||||||
await waitForEditor( page );
|
await waitForEditor( page );
|
||||||
const templatesBefore = await getTemplateRowCount( 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 ) => {
|
await page.evaluate( ( slug ) => {
|
||||||
const registered = await window.wp.apiFetch( { path: '/wp/v2/block-patterns/patterns' } );
|
const store = window.wp.data.select( 'core/block-editor' );
|
||||||
const selected = registered.find( ( candidate ) => candidate.name === slug );
|
const selectedPattern = store.__experimentalGetParsedPattern( slug );
|
||||||
if ( ! selected ) {
|
if ( ! selectedPattern?.blocks?.length ) {
|
||||||
throw new Error( `Registered pattern not found: ${ slug }` );
|
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' ).resetBlocks( [] );
|
||||||
window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( blocks );
|
window.wp.data.dispatch( 'core/block-editor' ).insertBlocks( selectedPattern.blocks );
|
||||||
return blocks.map( ( block ) => block.clientId );
|
|
||||||
}, pattern.patternSlug );
|
}, pattern.patternSlug );
|
||||||
|
await waitForPatternEditorState( page );
|
||||||
|
|
||||||
expectPatternContract( await editorSnapshot( page, insertedRootIds ), pattern );
|
expectPatternContract( await editorSnapshot( page ), pattern );
|
||||||
await expectNoRecoveryUi( page );
|
await expectNoRecoveryUi( page );
|
||||||
expect( await getTemplateRowCount( page ) ).toBe( templatesBefore );
|
expect( await getTemplateRowCount( page ) ).toBe( templatesBefore );
|
||||||
expect( persistenceRequests ).toEqual( [] );
|
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', () => {
|
test( 'character stages define the shared mockup effect and responsive geometry', () => {
|
||||||
const components = read( 'assets/css/components.css' );
|
const components = read( 'assets/css/components.css' );
|
||||||
const visualStates = read( 'tests/e2e/visual-states.spec.mjs' );
|
const visualStates = read( 'tests/e2e/visual-states.spec.mjs' );
|
||||||
|
|||||||
Reference in new issue
Block a user