Skip to content

Commit 13d361e

Browse files
committed
Add script modules test
1 parent be46bfd commit 13d361e

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public function set_up() {
2626
parent::set_up();
2727
$this->interactivity = new WP_Interactivity_API();
2828
wp_default_script_modules();
29+
$this->interactivity->add_hooks();
30+
}
31+
32+
/**
33+
* Tear down.
34+
*/
35+
public function tear_down() {
36+
global $wp_script_modules;
37+
parent::tear_down();
38+
$wp_script_modules = null;
2939
}
3040

3141
public function charset_iso_8859_1() {
@@ -236,7 +246,6 @@ public function test_register_script_modules_deprecated() {
236246
* @return MockAction
237247
*/
238248
private function get_script_data_filter_result( ?Closure $callback = null ): MockAction {
239-
$this->interactivity->add_hooks();
240249
wp_enqueue_script_module( '@wordpress/interactivity' );
241250
$filter = new MockAction();
242251
add_filter( 'script_module_data_@wordpress/interactivity', array( $filter, 'filter' ) );
@@ -1723,4 +1732,39 @@ public function test_invalid_directive_names_are_ignored() {
17231732
$this->assertStringNotContainsString( 'class="dis:allowed"', $processed_html );
17241733
$this->assertStringNotContainsString( 'class="[disallowed]"', $processed_html );
17251734
}
1735+
1736+
/**
1737+
* Tests that add_client_navigation_support_to_script_module marks a
1738+
* script module for client navigation.
1739+
*
1740+
* @ticket 64122
1741+
*
1742+
* @covers WP_Interactivity_API::add_client_navigation_support_to_script_module
1743+
* @covers WP_Interactivity_API::add_load_on_client_navigation_attribute_to_script_modules
1744+
*/
1745+
public function test_add_client_navigation_support_to_script_module() {
1746+
$this->interactivity->add_client_navigation_support_to_script_module( 'marked-module' );
1747+
1748+
wp_register_script_module( 'marked-module', '/marked.js' );
1749+
wp_register_script_module( 'unmarked-module', '/unmarked.js' );
1750+
wp_enqueue_script_module( 'marked-module' );
1751+
wp_enqueue_script_module( 'unmarked-module' );
1752+
1753+
$output = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
1754+
1755+
$p = new WP_HTML_Tag_Processor( $output );
1756+
1757+
// First module: marked-module should have the attribute.
1758+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
1759+
$this->assertSame( 'marked-module-js-module', $p->get_attribute( 'id' ) );
1760+
$this->assertSame(
1761+
'{"loadOnClientNavigation":true}',
1762+
$p->get_attribute( 'data-wp-router-options' )
1763+
);
1764+
1765+
// Second module: unmarked-module should NOT have the attribute.
1766+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
1767+
$this->assertSame( 'unmarked-module-js-module', $p->get_attribute( 'id' ) );
1768+
$this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
1769+
}
17261770
}

0 commit comments

Comments
 (0)