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

Block Hooks: Add end-to-end (e2e) test coverage. #68986

Open
ockham opened this issue Jan 31, 2025 · 1 comment
Open

Block Hooks: Add end-to-end (e2e) test coverage. #68986

ockham opened this issue Jan 31, 2025 · 1 comment
Labels
[Feature] Block hooks [Package] E2E Tests /packages/e2e-tests [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.

Comments

@ockham
Copy link
Contributor

ockham commented Jan 31, 2025

As @gziolo said in #68926 (review), it is becoming more tedious to test Block Hooks manually, specifically that hooked blocks are inserted in different contexts. Additionally, there are plenty of edge cases, e.g. insertion as first or last child into special blocks like the Post Content, Synced Pattern, or Navigation block. These cases are covered by testing instructions in #67272 (Post Content), #68058 (Synced Pattern), and #57754 (Navigation), respectively, but they involve a lot of manual work.

Instead, we should write e2e tests to cover those cases.

@ockham ockham added [Feature] Block hooks [Package] E2E Tests /packages/e2e-tests [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. labels Jan 31, 2025
@ockham
Copy link
Contributor Author

ockham commented Jan 31, 2025

We can probably draw inspiration from the test plugins used in the testing instructions of #67272 and #68058, and maybe generalize them a bit further. E.g. something like

defined( 'ABSPATH' ) || exit;

function insert_hooked_blocks( $hooked_blocks, $position, $anchor_block, $context ) {
	if ( ! $context instanceof WP_Post ) {
		return $hooked_blocks;
	}

	if (
		( $anchor_block === 'core/heading' && $position === 'before' ) ||
		( $anchor_block === 'core/post-content' && $position === 'last_child' ) ||
		( $anchor_block === 'core/block' && $position === 'first_child' )
	) {
		$hooked_blocks[] = 'core/paragraph';
	}

	return $hooked_blocks;
}
add_filter( 'hooked_block_types', 'insert_hooked_blocks', 10, 4 );

function set_hooked_block_inner_html( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) {
	if (
		( $anchor_block['blockName'] === 'core/heading' && 'before' === $relative_position ) ||
		( $anchor_block['blockName'] === 'core/post-content' && 'last_child' === $relative_position ) ||
		( $anchor_block['blockName'] === 'core/block' && 'first_child' === $relative_position ) ||
	) {
                $hooked_block['innerContent'] = array(
                        sprintf(
                                '<p %s>Hooked block successfully inserted in %s position next to `%s` anchor block.</p>' ),
                                /* block wrapper attrs */,
                                $relative_position,
                                $anchor_block
                        );
                )
	}

	return $hooked_block;
}
add_filter( 'hooked_block_core/paragraph', 'set_hooked_block_inner_html', 10, 4 );

Instead of using a Paragraph block, we could also write a little Block Hooks Debug block that displays the relevant information (relative position and anchor).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block hooks [Package] E2E Tests /packages/e2e-tests [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

No branches or pull requests

1 participant