Skip to content

Commit 8309bfd

Browse files
committed
Added unit tests for Dropdown_Item block class.
1 parent fe9d86c commit 8309bfd

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Class handles unit tests for GatherPress\Core\Blocks\Dropdown_Item.
4+
*
5+
* @package GatherPress\Core
6+
* @since 1.0.0
7+
*/
8+
9+
namespace GatherPress\Tests\Core\Blocks;
10+
11+
use GatherPress\Core\Blocks\Dropdown_Item;
12+
use PMC\Unit_Test\Base;
13+
14+
/**
15+
* Class Test_Dropdown_Item.
16+
*
17+
* @coversDefaultClass \GatherPress\Core\Blocks\Dropdown_Item
18+
*/
19+
class Test_Dropdown_Item extends Base {
20+
/**
21+
* Tests the setup_hooks method.
22+
*
23+
* Verifies that the appropriate filters are registered during setup,
24+
* ensuring the hooks are properly configured for the Dropdown_Item block.
25+
*
26+
* @since 1.0.0
27+
* @covers ::__construct
28+
* @covers ::setup_hooks
29+
*
30+
* @return void
31+
*/
32+
public function test_setup_hooks(): void {
33+
$instance = Dropdown_Item::get_instance();
34+
$render_block_hook = sprintf( 'render_block_%s', Dropdown_Item::BLOCK_NAME );
35+
$hooks = array(
36+
array(
37+
'type' => 'filter',
38+
'name' => $render_block_hook,
39+
'priority' => 10,
40+
'callback' => array( $instance, 'apply_dropdown_attributes' ),
41+
),
42+
);
43+
44+
$this->assert_hooks( $hooks, $instance );
45+
}
46+
47+
/**
48+
* Tests apply_dropdown_attributes with an empty href.
49+
*
50+
* @since 1.0.0
51+
* @covers ::apply_dropdown_attributes
52+
*
53+
* @return void
54+
*/
55+
public function test_apply_dropdown_attributes_empty_href(): void {
56+
$instance = Dropdown_Item::get_instance();
57+
$block_content = '<a href="" class="dropdown-link">Click me</a>';
58+
$result = $instance->apply_dropdown_attributes( $block_content );
59+
60+
$this->assertStringContainsString( 'data-wp-interactive="gatherpress"', $result );
61+
$this->assertStringContainsString( 'data-wp-on--click="actions.linkHandler"', $result );
62+
$this->assertStringContainsString( 'tabindex="0"', $result );
63+
$this->assertStringContainsString( 'role="button"', $result );
64+
}
65+
66+
/**
67+
* Tests apply_dropdown_attributes with a hash href.
68+
*
69+
* @since 1.0.0
70+
* @covers ::apply_dropdown_attributes
71+
*
72+
* @return void
73+
*/
74+
public function test_apply_dropdown_attributes_hash_href(): void {
75+
$instance = Dropdown_Item::get_instance();
76+
$block_content = '<a href="#" class="dropdown-link">Click me</a>';
77+
$result = $instance->apply_dropdown_attributes( $block_content );
78+
79+
$this->assertStringContainsString( 'data-wp-interactive="gatherpress"', $result );
80+
$this->assertStringContainsString( 'data-wp-on--click="actions.linkHandler"', $result );
81+
$this->assertStringContainsString( 'tabindex="0"', $result );
82+
$this->assertStringContainsString( 'role="button"', $result );
83+
}
84+
85+
/**
86+
* Tests apply_dropdown_attributes with a valid URL href.
87+
*
88+
* @since 1.0.0
89+
* @covers ::apply_dropdown_attributes
90+
*
91+
* @return void
92+
*/
93+
public function test_apply_dropdown_attributes_valid_href(): void {
94+
$instance = Dropdown_Item::get_instance();
95+
$block_content = '<a href="https://example.com" class="dropdown-link">Click me</a>';
96+
$result = $instance->apply_dropdown_attributes( $block_content );
97+
98+
$this->assertStringNotContainsString( 'data-wp-interactive', $result );
99+
$this->assertStringNotContainsString( 'data-wp-on--click', $result );
100+
$this->assertStringNotContainsString( 'tabindex="0"', $result );
101+
$this->assertStringNotContainsString( 'role="button"', $result );
102+
$this->assertStringContainsString( 'href="https://example.com"', $result );
103+
}
104+
105+
/**
106+
* Tests apply_dropdown_attributes with no anchor tag.
107+
*
108+
* @since 1.0.0
109+
* @covers ::apply_dropdown_attributes
110+
*
111+
* @return void
112+
*/
113+
public function test_apply_dropdown_attributes_no_anchor(): void {
114+
$instance = Dropdown_Item::get_instance();
115+
$block_content = '<div class="dropdown-link">Click me</div>';
116+
$result = $instance->apply_dropdown_attributes( $block_content );
117+
118+
$this->assertSame( $block_content, $result );
119+
}
120+
}

0 commit comments

Comments
 (0)