Skip to content

Commit

Permalink
Add test coverage for 'after' insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Feb 13, 2025
1 parent e852b5b commit b3d7aa8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/e2e-tests/plugins/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function gutenberg_test_insert_hooked_blocks( $hooked_blocks, $position, $anchor
}

if (
( 'core/heading' === $anchor_block && 'before' === $position ) ||
( 'core/heading' === $anchor_block && 'after' === $position ) ||
( 'core/post-content' === $anchor_block && 'last_child' === $position ) ||
( 'core/block' === $anchor_block && 'first_child' === $position )
) {
Expand All @@ -28,7 +28,7 @@ function gutenberg_test_insert_hooked_blocks( $hooked_blocks, $position, $anchor

function gutenberg_test_set_hooked_block_inner_html( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) {
if (
( 'core/heading' === $anchor_block['blockName'] && 'before' === $relative_position ) ||
( 'core/heading' === $anchor_block['blockName'] && 'after' === $relative_position ) ||
( 'core/post-content' === $anchor_block['blockName'] && 'last_child' === $relative_position ) ||
( 'core/block' === $anchor_block['blockName'] && 'first_child' === $relative_position )
) {
Expand Down
34 changes: 26 additions & 8 deletions test/e2e/specs/editor/plugins/block-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

const anchorBlockMarkup = `<!-- wp:paragraph {"className":"dummy-paragraph"} -->
const anchorBlockMarkup = `<!-- wp:heading -->
<h2 class="wp-block-heading">This is a dummy heading</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"className":"dummy-paragraph"} -->
<p class="dummy-paragraph">This is a dummy paragraph.</p>
<!-- /wp:paragraph -->`;

Expand Down Expand Up @@ -38,7 +41,7 @@ test.describe( 'Block Hooks API', () => {
await requestUtils.deleteAllPosts();
} );

test( 'should insert hooked block as last child of Post Content block on frontend', async ( {
test( 'should insert hooked blocks into post content on frontend', async ( {
page,
} ) => {
await page.goto( `/?p=${ post.id }` );
Expand All @@ -48,18 +51,27 @@ test.describe( 'Block Hooks API', () => {
)
).toHaveCount( 1 );
// Verify that the hook block is inserted after the test paragraph.
await expect( page.locator( '.entry-content p' ) ).toHaveClass( [
await expect( page.locator( '.entry-content > *' ) ).toHaveClass( [
'wp-block-heading',
getHookedBlockClassName( 'after', 'core/heading' ),
'dummy-paragraph',
getHookedBlockClassName( 'last_child', 'core/post-content' ),
] );
} );

test( 'should insert hooked block as last child of Post Content block in editor', async ( {
test( 'should insert hooked blocks into post content in editor', async ( {
admin,
editor,
page,
} ) => {
const expectedHookedBlock = {
const expectedHookedBlockAfterHeading = {
name: 'core/paragraph',
attributes: {
className: getHookedBlockClassName( 'after', 'core/heading' ),
},
};

const expectedHookedBlockLastChild = {
name: 'core/paragraph',
attributes: {
className: getHookedBlockClassName(
Expand All @@ -73,8 +85,10 @@ test.describe( 'Block Hooks API', () => {
await expect
.poll( editor.getBlocks )
.toMatchObject( [
{ name: 'core/heading' },
expectedHookedBlockAfterHeading,
{ name: 'core/paragraph' },
expectedHookedBlock,
expectedHookedBlockLastChild,
] );

const hookedBlock = editor.canvas.getByText(
Expand All @@ -98,7 +112,9 @@ test.describe( 'Block Hooks API', () => {
await expect
.poll( editor.getBlocks )
.toMatchObject( [
expectedHookedBlock,
{ name: 'core/heading' },
expectedHookedBlockAfterHeading,
expectedHookedBlockLastChild,
{ name: 'core/paragraph' },
] );

Expand All @@ -110,7 +126,9 @@ test.describe( 'Block Hooks API', () => {
)
).toHaveCount( 1 );
// Verify that the hooked block is now before the test paragraph.
await expect( page.locator( '.entry-content p' ) ).toHaveClass( [
await expect( page.locator( '.entry-content > *' ) ).toHaveClass( [
'wp-block-heading',
getHookedBlockClassName( 'after', 'core/heading' ),
getHookedBlockClassName( 'last_child', 'core/post-content' ),
'dummy-paragraph',
] );
Expand Down

0 comments on commit b3d7aa8

Please sign in to comment.