Skip to content

Commit

Permalink
Add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Jan 10, 2025
1 parent 815003e commit 9cb46f6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -1792,10 +1792,6 @@ public function do_print() {
* @return static
*/
public function do_register() {
if ( $this->is_registered() ) {
return $this;
}

$this->register_asset( $this->get_url(), $this->get_dependencies(), $this->get_version(), $this->is_js() ? $this->is_in_footer() : $this->get_media() );
$this->set_as_registered();

Expand Down Expand Up @@ -1823,24 +1819,26 @@ public function do_register() {
* Set the asset enqueue status to false.
*
* @since 1.0.0
* @since TBD - Actually dequeues the asset.
*
* @return static
*/
public function set_as_unenqueued() {
$this->is_enqueued = false;
return $this;
return $this->dequeue_asset();
}

/**
* Set the asset registration status to false.
*
* @since 1.0.0
* @since TBD - Actually deregisters the asset.
*
* @return static
*/
public function set_as_unregistered() {
$this->is_registered = false;
return $this;
return $this->deregister_asset();
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/wpunit/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,81 @@ public function test_add_to_group_path_changes_resolution_path_to_group(): void

$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );
}

public function test_it_can_enqueue_multiple_times_overwritting_previous(): void {
Config::reset();
Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
Config::set_relative_asset_path( 'tests/_data/' );

$checker = static fn( string $handle ): array => wp_scripts()->registered[ $handle ]->deps;

$asset = Asset::add( 'test-script-unique-654321', 'fake.js', '1.0.0', codecept_data_dir() );
$asset->enqueue_on( 'a_random_action_1234' );
$asset->set_dependencies( 'jquery', 'jquery-ui-core' );

do_action( 'a_random_action_1234' );

$asset->enqueue();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) );
$this->assertTrue( $asset->asset_is( 'enqueued' ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $asset->get_dependencies() );

$asset->set_dependencies( 'jquery' );
// It's not going to register again! We need to set it as unregistered first.
$asset->enqueue();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) );
$this->assertTrue( $asset->asset_is( 'enqueued' ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery' ], $asset->get_dependencies() );

$asset->set_as_unregistered();
$asset->enqueue();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) );
$this->assertTrue( $asset->asset_is( 'enqueued' ) );
$this->assertEquals( [ 'jquery' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery' ], $asset->get_dependencies() );
}

public function test_it_can_register_multiple_times_overwritting_previous(): void {
Config::reset();
Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
Config::set_relative_asset_path( 'tests/_data/' );

$checker = static fn( string $handle ): array => wp_scripts()->registered[ $handle ]->deps;

$asset = new Asset( 'test-script-unique-123456', 'fake.js', '1.0.0', codecept_data_dir() );

$asset->set_dependencies( 'jquery', 'jquery-ui-core' );
$asset->register();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) );
$this->assertTrue( $asset->asset_is( 'registered' ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $asset->get_dependencies() );

$asset->set_dependencies( 'jquery' );
// It's not going to register again! We need to set it as unregistered first.
$asset->register();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) );
$this->assertTrue( $asset->asset_is( 'registered' ) );
$this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery' ], $asset->get_dependencies() );

$asset->set_as_unregistered();
$asset->register();

$this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) );
$this->assertTrue( $asset->asset_is( 'registered' ) );
$this->assertEquals( [ 'jquery' ], $checker( $asset->get_slug() ) );
$this->assertEquals( [ 'jquery' ], $asset->get_dependencies() );
}
}

0 comments on commit 9cb46f6

Please sign in to comment.