Skip to content

Commit

Permalink
Tests: Use more specific assertions in WP_Script_Modules tests.
Browse files Browse the repository at this point in the history
Follow-up to [57269], [57327], [57593].

See #60705.

git-svn-id: https://develop.svn.wordpress.org/trunk@58420 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jun 16, 2024
1 parent dc6c8c8 commit fa7902c
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function test_wp_dequeue_script_module() {
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 1, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}


Expand All @@ -149,8 +149,8 @@ public function test_wp_deregister_script_module() {
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 1, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}

/**
Expand All @@ -168,7 +168,7 @@ public function test_wp_deregister_unexistent_script_module() {
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 0, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['unexistent'] ) );
$this->assertArrayNotHasKey( 'unexistent', $enqueued_script_modules );
}

/**
Expand All @@ -190,13 +190,13 @@ public function test_wp_deregister_already_deregistered_script_module() {
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 0, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );

$this->script_modules->deregister( 'foo' ); // Dequeued.
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 0, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
}

/**
Expand All @@ -218,7 +218,7 @@ public function test_wp_enqueue_script_module_works_before_register() {

$this->assertCount( 1, $enqueued_script_modules );
$this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
$this->assertFalse( isset( $enqueued_script_modules['bar'] ) );
$this->assertArrayNotHasKey( 'bar', $enqueued_script_modules );
}

/**
Expand All @@ -242,8 +242,8 @@ public function test_wp_dequeue_script_module_works_before_register() {
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 1, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}

/**
Expand All @@ -266,7 +266,7 @@ public function test_wp_import_map_dependencies() {

$this->assertCount( 1, $import_map );
$this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
$this->assertFalse( isset( $import_map['no-dep'] ) );
$this->assertArrayNotHasKey( 'no-dep', $import_map );
}

/**
Expand Down Expand Up @@ -340,7 +340,7 @@ public function test_wp_import_map_recursive_dependencies() {
$this->assertStringStartsWith( '/dynamic-dep.js', $import_map['dynamic-dep'] );
$this->assertStringStartsWith( '/nested-static-dep.js', $import_map['nested-static-dep'] );
$this->assertStringStartsWith( '/nested-dynamic-dep.js', $import_map['nested-dynamic-dep'] );
$this->assertFalse( isset( $import_map['no-dep'] ) );
$this->assertArrayNotHasKey( 'no-dep', $import_map );
}

/**
Expand Down Expand Up @@ -409,9 +409,9 @@ public function test_wp_enqueue_preloaded_static_dependencies() {
$this->assertCount( 2, $preloaded_script_modules );
$this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
$this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_script_modules['nested-static-dep'] );
$this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
$this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
$this->assertFalse( isset( $preloaded_script_modules['nested-dynamic-dep'] ) );
$this->assertArrayNotHasKey( 'dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'nested-dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'no-dep', $preloaded_script_modules );
}

/**
Expand Down Expand Up @@ -445,9 +445,9 @@ public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies

$this->assertCount( 1, $preloaded_script_modules );
$this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
$this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
$this->assertFalse( isset( $preloaded_script_modules['nested-static-dep'] ) );
$this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
$this->assertArrayNotHasKey( 'dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'nested-dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'no-dep', $preloaded_script_modules );
}

/**
Expand Down Expand Up @@ -476,8 +476,8 @@ public function test_wp_preloaded_dependencies_filter_enqueued_script_modules()
$preloaded_script_modules = $this->get_preloaded_script_modules();

$this->assertCount( 1, $preloaded_script_modules );
$this->assertTrue( isset( $preloaded_script_modules['dep'] ) );
$this->assertFalse( isset( $preloaded_script_modules['enqueued-dep'] ) );
$this->assertArrayHasKey( 'dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'enqueued-dep', $preloaded_script_modules );
}

/**
Expand Down Expand Up @@ -507,8 +507,8 @@ public function test_wp_enqueued_script_modules_with_dependants_add_import_map()
$import_map = $this->get_import_map();

$this->assertCount( 2, $import_map );
$this->assertTrue( isset( $import_map['dep'] ) );
$this->assertTrue( isset( $import_map['enqueued-dep'] ) );
$this->assertArrayHasKey( 'dep', $import_map );
$this->assertArrayHasKey( 'enqueued-dep', $import_map );
}

/**
Expand All @@ -531,7 +531,7 @@ public function test_get_src() {
);

$result = $get_src->invoke( $this->script_modules, 'module_with_version' );
$this->assertEquals( 'http://example.com/module.js?ver=1.0', $result );
$this->assertSame( 'http://example.com/module.js?ver=1.0', $result );

$this->script_modules->register(
'module_without_version',
Expand All @@ -541,7 +541,7 @@ public function test_get_src() {
);

$result = $get_src->invoke( $this->script_modules, 'module_without_version' );
$this->assertEquals( 'http://example.com/module.js', $result );
$this->assertSame( 'http://example.com/module.js', $result );

$this->script_modules->register(
'module_with_wp_version',
Expand All @@ -551,7 +551,7 @@ public function test_get_src() {
);

$result = $get_src->invoke( $this->script_modules, 'module_with_wp_version' );
$this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );
$this->assertSame( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );

$this->script_modules->register(
'module_with_existing_query_string',
Expand All @@ -561,7 +561,7 @@ public function test_get_src() {
);

$result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
$this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result );
$this->assertSame( 'http://example.com/module.js?foo=bar&ver=1.0', $result );

// Filter the version to include the ID in the final URL, to test the filter, this should affect the tests below.
add_filter(
Expand All @@ -574,10 +574,10 @@ function ( $src, $id ) {
);

$result = $get_src->invoke( $this->script_modules, 'module_without_version' );
$this->assertEquals( 'http://example.com/module.js?script_module_id=module_without_version', $result );
$this->assertSame( 'http://example.com/module.js?script_module_id=module_without_version', $result );

$result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
$this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0&script_module_id=module_with_existing_query_string', $result );
$this->assertSame( 'http://example.com/module.js?foo=bar&ver=1.0&script_module_id=module_with_existing_query_string', $result );
}

/**
Expand Down Expand Up @@ -606,13 +606,13 @@ public function test_version_is_propagated_correctly() {
$this->script_modules->enqueue( 'foo' );

$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
$this->assertSame( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );

$import_map = $this->get_import_map();
$this->assertEquals( '/dep.js?ver=2.0', $import_map['dep'] );
$this->assertSame( '/dep.js?ver=2.0', $import_map['dep'] );

$preloaded_script_modules = $this->get_preloaded_script_modules();
$this->assertEquals( '/dep.js?ver=2.0', $preloaded_script_modules['dep'] );
$this->assertSame( '/dep.js?ver=2.0', $preloaded_script_modules['dep'] );
}

/**
Expand All @@ -630,7 +630,7 @@ public function test_wp_enqueue_script_module_doesnt_register_without_a_valid_sr
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 0, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
}

/**
Expand Down Expand Up @@ -666,7 +666,7 @@ public function test_wp_enqueue_script_module_registers_with_valid_src_the_secon
$enqueued_script_modules = $this->get_enqueued_script_modules();

$this->assertCount( 0, $enqueued_script_modules );
$this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );

$this->script_modules->enqueue( 'foo', '/foo.js' ); // Valid src.

Expand Down Expand Up @@ -695,7 +695,7 @@ public function test_wp_enqueue_script_module_registers_all_params() {
$import_map = $this->get_import_map();

$this->assertCount( 1, $enqueued_script_modules );
$this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
$this->assertSame( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
$this->assertCount( 1, $import_map );
$this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
}
Expand All @@ -708,7 +708,7 @@ public function test_wp_enqueue_script_module_registers_all_params() {
public function test_wp_print_import_map_has_no_polyfill_when_no_modules_registered() {
$import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map' ) );

$this->assertEquals( '', $import_map_polyfill );
$this->assertSame( '', $import_map_polyfill );
}

/**
Expand All @@ -730,6 +730,6 @@ public function test_wp_print_import_map_has_polyfill_when_modules_registered()
$p->next_tag( array( 'tag' => 'SCRIPT' ) );
$id = $p->get_attribute( 'id' );

$this->assertEquals( 'wp-load-polyfill-importmap', $id );
$this->assertSame( 'wp-load-polyfill-importmap', $id );
}
}

0 comments on commit fa7902c

Please sign in to comment.