Add unit tests for after_add_block and after_remove_block hooks

This commit is contained in:
Matt Sherman 2023-09-13 10:55:43 -04:00
parent 036158ad4b
commit b3ec4f982f
1 changed files with 130 additions and 0 deletions

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Tests\Internal\Admin\BlockTemplates; namespace Automattic\WooCommerce\Tests\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Internal\Admin\BlockTemplates\BlockTemplate; use Automattic\WooCommerce\Internal\Admin\BlockTemplates\BlockTemplate;
use WC_Unit_Test_Case; use WC_Unit_Test_Case;
@ -35,6 +36,135 @@ class BlockTemplateTest extends WC_Unit_Test_Case {
$this->assertSame( $block, $template->get_block( 'test-block-id' ) ); $this->assertSame( $block, $template->get_block( 'test-block-id' ) );
} }
/**
* Test the after_add_block hooks fires.
*/
public function test_after_add_block_hooks() {
$template = new BlockTemplate();
$hook_called = false;
$after_add_block_hook = function( BlockInterface $block ) use ( &$hook_called ) {
$hook_called = true;
if ( 'test-block-id' === $block->get_id() ) {
$hook_called = true;
}
$root_template = $block->get_root_template();
if ( $root_template->get_block( 'test-block-id-2' ) ) {
// The block was already added, so just return.
// This short-circuiting is needed because this hook will be called two times:
// 1. When the `test-block-id` block is added to the root template.
// 2. When the `test-block-id-2` block is added to the template in this hook.
return;
}
$root_template->add_block(
[
'id' => 'test-block-id-2',
'blockName' => 'test-block-name-2',
]
);
};
add_action( 'woocommerce_block_template_after_add_block', $after_add_block_hook );
$specific_hook_called = false;
$specific_after_add_block_hook = function( BlockInterface $block ) use ( &$specific_hook_called ) {
if ( 'test-block-id' === $block->get_id() ) {
$specific_hook_called = true;
}
};
add_action( 'woocommerce_block_template_area_uncategorized_after_add_block_test-block-id', $specific_after_add_block_hook );
$template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$this->assertTrue(
$hook_called,
'Failed asserting that that the hook was called.'
);
$this->assertTrue(
$specific_hook_called,
'Failed asserting that that the specific hook was called.'
);
$this->assertNotNull(
$template->get_block( 'test-block-id-2' ),
'Failed asserting that the block was added to the template from the hook.'
);
remove_action( 'woocommerce_block_template_after_add_block', $after_add_block_hook );
remove_action( 'woocommerce_block_template_area_uncategorized_after_add_block_test-block-id', $specific_after_add_block_hook );
}
/**
* Test the after_remove_block hooks fires.
*/
public function test_after_remove_block_hooks() {
$template = new BlockTemplate();
$hook_called = false;
$after_remove_block_hook = function( BlockInterface $block ) use ( &$hook_called ) {
$hook_called = true;
if ( 'test-block-id' === $block->get_id() ) {
$hook_called = true;
}
$root_template = $block->get_root_template();
};
add_action( 'woocommerce_block_template_after_remove_block', $after_remove_block_hook );
$specific_hook_called = false;
$specific_after_remove_block_hook = function( BlockInterface $block ) use ( &$specific_hook_called ) {
if ( 'test-block-id' === $block->get_id() ) {
$specific_hook_called = true;
}
};
add_action( 'woocommerce_block_template_area_uncategorized_after_remove_block_test-block-id', $specific_after_remove_block_hook );
$block = $template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$block->remove();
$this->assertTrue(
$hook_called,
'Failed asserting that that the hook was called.'
);
$this->assertTrue(
$specific_hook_called,
'Failed asserting that that the specific hook was called.'
);
$this->assertTrue(
$block->is_detached(),
'Failed asserting that the block was added to the template from the hook.'
);
remove_action( 'woocommerce_block_template_after_remove_block', $after_remove_block_hook );
remove_action( 'woocommerce_block_template_area_uncategorized_after_remove_block_test-block-id', $specific_after_remove_block_hook );
}
/** /**
* Test adding a block throws an exception if a block with the same ID already exists. * Test adding a block throws an exception if a block with the same ID already exists.
*/ */