Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
update selectBlockByName
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Sep 16, 2021
1 parent 1c268b7 commit 2c9f047
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
9 changes: 3 additions & 6 deletions tests/e2e/specs/backend/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ describe( `${ block.name } Block`, () => {
describe( 'shipping address block attributes', () => {
beforeEach( async () => {
await openDocumentSettingsSidebar();
await page.click(
'.wp-block-woocommerce-checkout-shipping-address-block'
await selectBlockByName(
'woocommerce/checkout-shipping-address-block'
);
} );

Expand Down Expand Up @@ -153,10 +153,7 @@ describe( `${ block.name } Block`, () => {
describe( 'action block attributes', () => {
beforeEach( async () => {
await openDocumentSettingsSidebar();
await page.click(
block.class +
' .wp-block-woocommerce-checkout-actions-block'
);
await selectBlockByName( 'woocommerce/checkout-actions-block' );
} );

describe( 'Return to cart link', () => {
Expand Down
19 changes: 15 additions & 4 deletions tests/utils/select-block-by-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
*/
import { getAllBlocks, selectBlockByClientId } from '@wordpress/e2e-test-utils';

export const selectBlockByName = async ( name ) => {
await selectBlockByClientId(
( await getAllBlocks() ).find( ( block ) => block.name === name )
.clientId
export const selectBlockByName = async ( blockName ) => {
const blocksInEditor = await getAllBlocks();
const flatBlockArray = ( blocks ) =>
blocks
.map( ( block ) => [
{ [ block.name ]: block.clientId },
block.innerBlocks ? flatBlockArray( block.innerBlocks ) : [],
] )
.flat( Infinity );
const blocksObject = Object.fromEntries(
flatBlockArray( blocksInEditor )
.map( ( block ) => Object.entries( block ) )
.flat()
);
const clientId = blocksObject[ blockName ];
return selectBlockByClientId( clientId );
};

0 comments on commit 2c9f047

Please sign in to comment.