Skip to content

Commit 8736e52

Browse files
committed
Add block register test
1 parent 13d361e commit 8736e52

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/phpunit/tests/blocks/register.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,107 @@ public function test_success_register_block_script_module_id() {
400400
);
401401
}
402402

403+
/**
404+
* Tests that blocks with supports.interactivity have the
405+
* `data-wp-router-options` directive.
406+
*
407+
* @ticket 64122
408+
*
409+
* @covers ::register_block_script_module_id
410+
*/
411+
public function test_register_block_script_module_id_with_interactivity_true() {
412+
$metadata = array(
413+
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
414+
'viewScriptModule' => 'file:./block.js',
415+
);
416+
417+
$interactivity_true = array_merge(
418+
$metadata,
419+
array(
420+
'name' => 'tests/interactivity-true',
421+
'supports' => array( 'interactivity' => true ),
422+
)
423+
);
424+
$interactive_and_client_navigation = array_merge(
425+
$metadata,
426+
array(
427+
'name' => 'tests/interactive-and-client-navigation',
428+
'supports' => array(
429+
'interactivity' => array(
430+
'interactive' => true,
431+
'clientNavigation' => true,
432+
),
433+
),
434+
)
435+
);
436+
$interactive_and_not_client_navigation = array_merge(
437+
$metadata,
438+
array(
439+
'name' => 'tests/interactive-and-not-client-navigation',
440+
'supports' => array(
441+
'interactivity' => array(
442+
'interactive' => true,
443+
'clientNavigation' => false,
444+
),
445+
),
446+
)
447+
);
448+
$not_interactive_and_client_navigation = array_merge(
449+
$metadata,
450+
array(
451+
'name' => 'tests/not-interactive-and-client-navigation',
452+
'supports' => array(
453+
'interactivity' => array(
454+
'interactive' => false,
455+
'clientNavigation' => true,
456+
),
457+
),
458+
)
459+
);
460+
$no_interactivity = array_merge(
461+
$metadata,
462+
array(
463+
'name' => 'tests/no-interactivity',
464+
'supports' => array(),
465+
)
466+
);
467+
468+
$interactivity_true_module_id = register_block_script_module_id( $interactivity_true, 'viewScriptModule' );
469+
$interactive_and_client_navigation_module_id = register_block_script_module_id( $interactive_and_client_navigation, 'viewScriptModule' );
470+
$interactive_and_not_client_navigation_module_id = register_block_script_module_id( $interactive_and_not_client_navigation, 'viewScriptModule' );
471+
$not_interactive_and_client_navigation_module_id = register_block_script_module_id( $not_interactive_and_client_navigation, 'viewScriptModule' );
472+
$no_interactivity_module_id = register_block_script_module_id( $no_interactivity, 'viewScriptModule' );
473+
wp_enqueue_script_module( $interactivity_true_module_id );
474+
wp_enqueue_script_module( $interactive_and_client_navigation_module_id );
475+
wp_enqueue_script_module( $interactive_and_not_client_navigation_module_id );
476+
wp_enqueue_script_module( $not_interactive_and_client_navigation_module_id );
477+
wp_enqueue_script_module( $no_interactivity_module_id );
478+
479+
$output = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
480+
481+
$p = new WP_HTML_Tag_Processor( $output );
482+
483+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
484+
$this->assertSame( 'tests-interactivity-true-view-script-module-js-module', $p->get_attribute( 'id' ) );
485+
$this->assertSame( '{"loadOnClientNavigation":true}', $p->get_attribute( 'data-wp-router-options' ) );
486+
487+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
488+
$this->assertSame( 'tests-interactive-and-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
489+
$this->assertSame( '{"loadOnClientNavigation":true}', $p->get_attribute( 'data-wp-router-options' ) );
490+
491+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
492+
$this->assertSame( 'tests-interactive-and-not-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
493+
$this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
494+
495+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
496+
$this->assertSame( 'tests-not-interactive-and-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
497+
$this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
498+
499+
$p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
500+
$this->assertSame( 'tests-no-interactivity-view-script-module-js-module', $p->get_attribute( 'id' ) );
501+
$this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
502+
}
503+
403504
/**
404505
* @ticket 50263
405506
*/

0 commit comments

Comments
 (0)