Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add script modules test
  • Loading branch information
luisherranz committed Oct 20, 2025
commit 13d361eaece6b107ec5055aa26c48ec05e8997f8
46 changes: 45 additions & 1 deletion tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public function set_up() {
parent::set_up();
$this->interactivity = new WP_Interactivity_API();
wp_default_script_modules();
$this->interactivity->add_hooks();
}

/**
* Tear down.
*/
public function tear_down() {
global $wp_script_modules;
parent::tear_down();
$wp_script_modules = null;
}

public function charset_iso_8859_1() {
Expand Down Expand Up @@ -236,7 +246,6 @@ public function test_register_script_modules_deprecated() {
* @return MockAction
*/
private function get_script_data_filter_result( ?Closure $callback = null ): MockAction {
$this->interactivity->add_hooks();
wp_enqueue_script_module( '@wordpress/interactivity' );
$filter = new MockAction();
add_filter( 'script_module_data_@wordpress/interactivity', array( $filter, 'filter' ) );
Expand Down Expand Up @@ -1723,4 +1732,39 @@ public function test_invalid_directive_names_are_ignored() {
$this->assertStringNotContainsString( 'class="dis:allowed"', $processed_html );
$this->assertStringNotContainsString( 'class="[disallowed]"', $processed_html );
}

/**
* Tests that add_client_navigation_support_to_script_module marks a
* script module for client navigation.
*
* @ticket 64122
*
* @covers WP_Interactivity_API::add_client_navigation_support_to_script_module
* @covers WP_Interactivity_API::add_load_on_client_navigation_attribute_to_script_modules
*/
public function test_add_client_navigation_support_to_script_module() {
$this->interactivity->add_client_navigation_support_to_script_module( 'marked-module' );

wp_register_script_module( 'marked-module', '/marked.js' );
wp_register_script_module( 'unmarked-module', '/unmarked.js' );
wp_enqueue_script_module( 'marked-module' );
wp_enqueue_script_module( 'unmarked-module' );

$output = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );

$p = new WP_HTML_Tag_Processor( $output );

// First module: marked-module should have the attribute.
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
$this->assertSame( 'marked-module-js-module', $p->get_attribute( 'id' ) );
$this->assertSame(
'{"loadOnClientNavigation":true}',
$p->get_attribute( 'data-wp-router-options' )
);

// Second module: unmarked-module should NOT have the attribute.
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
$this->assertSame( 'unmarked-module-js-module', $p->get_attribute( 'id' ) );
$this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
}
}