Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 2, 2022
1 parent 8e5705c commit 8b48865
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 26 deletions.
20 changes: 12 additions & 8 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,26 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => {
const isReusable = blocks.length === 1 && isReusableBlock( blocks[ 0 ] );
const isTemplate = blocks.length === 1 && isTemplatePart( blocks[ 0 ] );

function selectForMultipleBlocks( insertedBlocks ) {
if ( insertedBlocks.length > 1 ) {
multiSelect(
insertedBlocks[ 0 ].clientId,
insertedBlocks[ insertedBlocks.length - 1 ].clientId
);
}
}

// Simple block tranformation based on the `Block Transforms` API.
function onBlockTransform( name ) {
const newBlocks = switchToBlockType( blocks, name );
replaceBlocks( clientIds, newBlocks );
multiSelect(
newBlocks[ 0 ].clientId,
newBlocks[ newBlocks.length - 1 ].clientId
);
selectForMultipleBlocks( newBlocks );
}

// Pattern transformation through the `Patterns` API.
function onPatternTransform( transformedBlocks ) {
replaceBlocks( clientIds, transformedBlocks );
multiSelect(
transformedBlocks[ 0 ].clientId,
transformedBlocks[ transformedBlocks.length - 1 ].clientId
);
selectForMultipleBlocks( transformedBlocks );
}

/**
Expand Down
48 changes: 30 additions & 18 deletions packages/e2e-test-utils-playwright/src/editor/transform-block-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,38 @@ import type { Editor } from './index';
* Clicks the default block appender.
*
* @param {Editor} this
* @param {string} name Block name.
* @param {string} name Block name or title.
*/
export async function transformBlockTo( this: Editor, name: string ) {
await this.showBlockToolbar();

if ( name.includes( '/' ) ) {
name = await this.page.evaluate( () => {
// @ts-ignore (Reason: wp isn't typed).
return window.wp.blocks.getBlockType( name ).title;
} );
}

const switcherToggle = await this.page.waitForSelector(
'.block-editor-block-switcher__toggle'
);
await switcherToggle.evaluate( ( element ) => element.scrollIntoView() );
await this.page.waitForSelector( '.block-editor-block-switcher__toggle' );
await switcherToggle.click();
await this.page.waitForSelector(
'.block-editor-block-switcher__container'
);

// Find the block button option within the switcher popover.
const xpath = `//*[contains(@class, "block-editor-block-switcher__popover")]//button[.='${ name }']`;
const insertButton = await this.page.waitForSelector( xpath );
// Clicks may fail if the button is out of view. Assure it is before click.
await insertButton.evaluate( ( element ) => element.scrollIntoView() );
await insertButton.click();

await this.page.evaluate(
( [ blockName ] ) => {
// @ts-ignore (Reason: wp isn't typed)
const clientIds = window.wp.data
.select( 'core/block-editor' )
.getSelectedBlockClientIds();
// @ts-ignore (Reason: wp isn't typed)
const blocks = window.wp.data
.select( 'core/block-editor' )
.getBlocksByClientId( clientIds );
// @ts-ignore (Reason: wp isn't typed)
window.wp.data.dispatch( 'core/block-editor' ).replaceBlocks(
clientIds,
// @ts-ignore (Reason: wp isn't typed)
window.wp.blocks.switchToBlockType( blocks, blockName )
);
},
[ name ]
() =>
// @ts-ignore (Reason: window global).
new Promise( requestIdleCallback )
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>1</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>2</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>1</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>2</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
21 changes: 21 additions & 0 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,4 +1231,25 @@ test.describe( 'List', () => {

expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'selects all transformed output', async ( { editor, page } ) => {
await page.click( 'role=button[name="Add default block"i]' );
await page.keyboard.type( '* 1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '2' );

// Select the whole list.
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );

expect( await editor.getEditedPostContent() ).toMatchSnapshot();

await editor.transformBlockTo( 'Paragraph' );

expect( await editor.getEditedPostContent() ).toMatchSnapshot();

await editor.transformBlockTo( 'List' );

expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );
} );

0 comments on commit 8b48865

Please sign in to comment.