diff --git a/packages/e2e-test-utils-playwright/src/editor/get-blocks.ts b/packages/e2e-test-utils-playwright/src/editor/get-blocks.ts index 70a5a03628ee05..f5adb295a00a55 100644 --- a/packages/e2e-test-utils-playwright/src/editor/get-blocks.ts +++ b/packages/e2e-test-utils-playwright/src/editor/get-blocks.ts @@ -3,25 +3,49 @@ */ import type { Editor } from './index'; +type Block = { + name: string; + attributes: Record< string, unknown >; + innerBlocks: Block[]; +}; + /** * Returns the edited blocks. * * @param this + * @param options + * @param options.full Whether to return the full block data or just the name and attributes. * * @return The blocks. */ -export async function getBlocks( this: Editor ) { - return await this.page.evaluate( () => { - const blocks = window.wp.data.select( 'core/block-editor' ).getBlocks(); +export async function getBlocks( this: Editor, { full = false } = {} ) { + return await this.page.evaluate( + ( [ _full ] ) => { + // Remove other unpredictable properties like clientId from blocks for testing purposes. + function recursivelyTransformBlocks( blocks: Block[] ): Block[] { + return blocks.map( ( block ) => ( { + name: block.name, + attributes: block.attributes, + innerBlocks: recursivelyTransformBlocks( + block.innerBlocks + ), + } ) ); + } + + const blocks = window.wp.data + .select( 'core/block-editor' ) + .getBlocks(); - // The editor might still contain an unmodified empty block even when it's technically "empty". - if ( - blocks.length === 1 && - window.wp.blocks.isUnmodifiedDefaultBlock( blocks[ 0 ] ) - ) { - return []; - } + // The editor might still contain an unmodified empty block even when it's technically "empty". + if ( + blocks.length === 1 && + window.wp.blocks.isUnmodifiedDefaultBlock( blocks[ 0 ] ) + ) { + return []; + } - return blocks; - } ); + return _full ? blocks : recursivelyTransformBlocks( blocks ); + }, + [ full ] + ); }