Update tests to check block removal

This commit is contained in:
Matt Sherman 2023-08-24 21:33:18 -04:00
parent 44575d6dca
commit d4a688e9b4
4 changed files with 283 additions and 2 deletions

View File

@ -364,4 +364,112 @@ class BlockTemplateTest extends WC_Unit_Test_Case {
'Failed asserting that the template is converted to a formatted template correctly.'
);
}
/**
* Test that removing a block in the template works.
*/
public function test_removing_blocks() {
$template = new BlockTemplate();
$template->add_block(
[
'blockName' => 'test-block-name-c',
'order' => 100,
'attributes' => [
'attr-c1' => 'value-c1',
'attr-c2' => 'value-c2',
],
]
);
$block_b = $template->add_block(
[
'id' => 'b',
'blockName' => 'test-block-name-b',
'order' => 50,
'attributes' => [
'attr-1' => 'value-1',
'attr-2' => 'value-2',
],
]
);
$template->add_block(
[
'id' => 'a',
'blockName' => 'test-block-name-a',
'order' => 10,
'attributes' => [
'attr-1' => 'value-1',
'attr-2' => 'value-2',
],
]
);
$block_b->add_block(
[
'blockName' => 'test-block-name-2',
'order' => 20,
'attributes' => [
'attr-1' => 'value-1',
'attr-2' => 'value-2',
],
]
);
$block_b->add_block(
[
'blockName' => 'test-block-name-1',
'order' => 10,
'attributes' => [
'attr-3' => 'value-3',
'attr-4' => 'value-4',
],
]
);
$block_b->add_block(
[
'blockName' => 'test-block-name-3',
'order' => 30,
]
);
$block_to_insert_in = $template->get_block( 'a' );
$block_to_insert_in->add_block(
[
'blockName' => 'inserted-block',
]
);
$template->remove_block( 'b' );
$this->assertSame(
[
[
'test-block-name-a',
[
'attr-1' => 'value-1',
'attr-2' => 'value-2',
],
[
[
'inserted-block',
[],
],
],
],
[
'test-block-name-c',
[
'attr-c1' => 'value-c1',
'attr-c2' => 'value-c2',
],
],
],
$template->get_formatted_template(),
'Failed asserting that the template is converted to a formatted template correctly.'
);
}
}

View File

@ -105,6 +105,110 @@ class BlockTest extends WC_Unit_Test_Case {
);
}
/**
* Test that removing a block from a block sets the parent and root template to null
* and that the block is removed from the root template.
*/
public function test_remove_block() {
$template = new BlockTemplate();
$block = $template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$child_block = $block->add_block(
[
'id' => 'test-block-id-2',
'blockName' => 'test-block-name-2',
]
);
$block->remove_block( 'test-block-id-2' );
$this->assertNull(
$template->get_block( 'test-block-id-2' ),
'Failed asserting that the child block was removed from the root template.'
);
$this->expectException( \RuntimeException::class );
$child_block->get_parent();
}
/**
* Test that removing a block from a block sets the parent and root template to null
* and that the block is removed from the root template, as well as any descendants.
*/
public function test_remove_nested_block() {
$template = new BlockTemplate();
$block = $template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$child_block = $block->add_block(
[
'id' => 'test-block-id-2',
'blockName' => 'test-block-name-2',
]
);
$template->remove_block( 'test-block-id-2' );
$this->assertNull(
$template->get_block( 'test-block-id-2' ),
'Failed asserting that the nested descendent block was removed from the root template.'
);
$this->expectException( \RuntimeException::class );
$child_block->get_parent();
}
/**
* Test that removing a block from a block sets the parent and root template to null
* and that the block is removed from the root template, as well as any descendants.
*/
public function test_remove_block_and_descendants() {
$template = new BlockTemplate();
$block = $template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$child_block = $block->add_block(
[
'id' => 'test-block-id-2',
'blockName' => 'test-block-name-2',
]
);
$template->remove_block( 'test-block-id' );
$this->assertNull(
$template->get_block( 'test-block-id' ),
'Failed asserting that the child block was removed from the root template.'
);
$this->assertNull(
$template->get_block( 'test-block-id-2' ),
'Failed asserting that the nested descendent block was removed from the root template.'
);
$this->expectException( \RuntimeException::class );
$child_block->get_parent();
}
/**
* Test that adding nested blocks sets the parent and root template correctly.
*/

View File

@ -62,4 +62,29 @@ class CustomBlockTemplateTest extends WC_Unit_Test_Case {
$block = $template->get_block( 'test-block-name' );
$this->assertInstanceOf( CustomBlock::class, $block );
}
/**
* Test that a custom block can be removed as expected.
*/
public function test_remove_custom_block() {
$template = new CustomBlockTemplate();
$template->add_custom_block(
[
'id' => 'test-block-name-1',
'blockName' => 'test-block-name',
]
);
$template->add_custom_block(
[
'id' => 'test-block-name-2',
'blockName' => 'test-block-name',
]
);
$template->remove_block( 'test-block-name-1' );
$this->assertNull( $template->get_block( 'test-block-name-1' ) );
}
}

View File

@ -41,7 +41,8 @@ class CustomBlockTest extends WC_Unit_Test_Case {
$template
);
$block->add_custom_inner_block();
$block->add_custom_inner_block( 'a' );
$block->add_custom_inner_block( 'b' );
$this->assertSame(
[
@ -50,7 +51,15 @@ class CustomBlockTest extends WC_Unit_Test_Case {
[
[
'custom-inner-block',
[],
[
'title' => 'a',
],
],
[
'custom-inner-block',
[
'title' => 'b',
],
],
],
],
@ -58,4 +67,39 @@ class CustomBlockTest extends WC_Unit_Test_Case {
'Failed asserting that the inner block was added'
);
}
/**
* Test that a custom block is removed as expected.
*/
public function test_remove_custom_inner_block() {
$template = new BlockTemplate();
$block = new CustomBlock(
[
'blockName' => 'test-block-name',
],
$template
);
$block->add_custom_inner_block( 'a' );
$block->add_custom_inner_block( 'b' );
$template->remove_block( 'custom-inner-block-1' );
$this->assertSame(
[
'test-block-name',
[],
[
[
'custom-inner-block',
[
'title' => 'b',
],
],
],
],
$block->get_formatted_template(),
'Failed asserting that the inner block was removed'
);
}
}