Skip to content

Commit

Permalink
Add tests for Admin_Settings::admin_enqueue_scripts(). (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
costdev authored Dec 4, 2024
1 parent da1837c commit a3bd455
Showing 1 changed file with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Class AdminSettings_AdminEnqueueScriptsTest
*
* @package AspireUpdate
*/

/**
* Tests for Admin_Settings::admin_enqueue_scripts()
*
* @covers \AspireUpdate\Admin_Settings::admin_enqueue_scripts
*/
class AdminSettings_AdminEnqueueScriptsTest extends AdminSettings_UnitTestCase {

/**
* Dequeues assets before each test runs.
*
* @return void
*/
public function set_up() {
parent::set_up();

wp_dequeue_style( 'aspire_update_settings_css' );
wp_dequeue_script( 'aspire_update_settings_js' );
}
/**
* Test that the stylesheet is enqueued on the settings screen.
*/
public function test_should_enqueue_style() {
$admin_settings = new AspireUpdate\Admin_Settings();

if ( is_multisite() ) {
$hook = 'index_page_aspireupdate-settings';
} else {
$hook = 'dashboard_page_aspireupdate-settings';
}

$admin_settings->admin_enqueue_scripts( $hook );

$this->assertTrue( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Test that the stylesheet is not enqueued on other screens.
*/
public function test_should_not_enqueue_style() {
$admin_settings = new AspireUpdate\Admin_Settings();

if ( is_multisite() ) {
$hook = 'plugins-network';
} else {
$hook = 'plugins';
}

$admin_settings->admin_enqueue_scripts( $hook );

$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Test that the script is enqueued and localized on the settings screen.
*/
public function test_should_enqueue_and_localize_script() {
$admin_settings = new AspireUpdate\Admin_Settings();

if ( is_multisite() ) {
$hook = 'index_page_aspireupdate-settings';
} else {
$hook = 'dashboard_page_aspireupdate-settings';
}

$admin_settings->admin_enqueue_scripts( $hook );

$this->assertTrue(
wp_script_is( 'aspire_update_settings_js' ),
'The script is not enqueued.'
);

$this->assertNotEmpty(
$GLOBALS['wp_scripts']->get_data( 'aspire_update_settings_js', 'data' ),
'The script is not localized.'
);
}

/**
* Test that the script is not enqueued on other screens.
*/
public function test_should_not_enqueue_script() {
$admin_settings = new AspireUpdate\Admin_Settings();

if ( is_multisite() ) {
$hook = 'plugins-network';
} else {
$hook = 'plugins';
}

$admin_settings->admin_enqueue_scripts( $hook );

$this->assertFalse( wp_script_is( 'aspire_update_settings_js' ) );
}
}

0 comments on commit a3bd455

Please sign in to comment.