Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make editor.getBlocks to return only testing-related properties #54901

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions packages/e2e-test-utils-playwright/src/editor/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
} ) );
}
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved

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 ]
);
}