Skip to content

Commit

Permalink
Update test_wp_service_worker_offline_page_reload
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Apr 15, 2022
1 parent e5e6f3b commit 54ed60b
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions tests/test-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Tests for template.php
*/
class Test_Template extends TestCase {

/**
* Test if function adding script on GET method, is_offline() or is_500 is true
*
Expand All @@ -21,8 +22,7 @@ public function test_wp_service_worker_offline_page_reload() {
$this->assertEquals( 10, has_action( 'error_footer', 'wp_service_worker_offline_page_reload' ) );

// Check when method is GET but not offline or 500.
$actual_script = wp_service_worker_offline_page_reload();
$this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' );
$actual_script = get_echo( 'wp_service_worker_offline_page_reload' );
$this->assertFalse( is_offline() );
$this->assertFalse( is_500() );
$this->assertEmpty( $actual_script );
Expand All @@ -31,36 +31,20 @@ public function test_wp_service_worker_offline_page_reload() {
$error_template_url = add_query_arg( 'wp_error_template', 'offline', home_url( '/', 'relative' ) );
$this->go_to( $error_template_url );

ob_start();
wp_service_worker_offline_page_reload();
$actual_script = ob_get_clean();
$this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' );
$actual_script = get_echo( 'wp_service_worker_offline_page_reload' );
$this->assertTrue( is_offline() );
$this->assertFalse( is_500() );
$this->assertStringContainsString( '<script type="module">', $actual_script );
$this->assertStringContainsString( 'await fetch(location.href, {method: \'HEAD\'})', $actual_script );
$this->assertStringContainsString( 'await fetch', $actual_script );

// Check if script is added when 500.
$error_template_url = add_query_arg( 'wp_error_template', '500', home_url( '/', 'relative' ) );
$this->go_to( $error_template_url );

ob_start();
wp_service_worker_offline_page_reload();
$actual_script = ob_get_clean();
$this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' );
$actual_script = get_echo( 'wp_service_worker_offline_page_reload' );
$this->assertFalse( is_offline() );
$this->assertTrue( is_500() );
$this->assertStringContainsString( '<script type="module">', $actual_script );
$this->assertStringContainsString( 'await fetch(location.href, {method: \'HEAD\'})', $actual_script );

$this->go_to( home_url( '/', 'relative' ) );

// Check when method is not GET.
$_SERVER['REQUEST_METHOD'] = 'POST';
$actual_script = wp_service_worker_offline_page_reload();
$this->assertEquals( $_SERVER['REQUEST_METHOD'], 'POST' );
$this->assertFalse( is_offline() );
$this->assertFalse( is_500() );
$this->assertEmpty( $actual_script );
$this->assertStringContainsString( 'await fetch', $actual_script );
}
}

0 comments on commit 54ed60b

Please sign in to comment.