Skip to content
Open

Release #1242

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions assets/src/media/__tests__/admin-page.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Tests for the Cloud Library admin page script.
*/

jest.mock( '../scss/admin-page.scss', () => ({}) );

const SITE_URL = 'https://example.test';

describe( 'Cloud Library admin page (issue #1211)', () => {
let iframe;
let loader;

beforeAll( () => {
window.optmlAdminPage = { siteUrl: SITE_URL };

// The script is printed in the head, before the iframe markup exists.
require( '../admin-page.js' );

document.body.innerHTML = '<div class="om-dam-loader"></div><iframe id="om-dam" style="display: none;"></iframe>';
iframe = document.getElementById( 'om-dam' );
loader = document.querySelector( '.om-dam-loader' );
});

test( 'reveals the iframe when it loads after the script ran', () => {
iframe.dispatchEvent( new Event( 'load' ) );

expect( loader.style.display ).toBe( 'none' );
expect( iframe.style.display ).toBe( '' );
});

test( 'answers getUrl from an iframe added after the script ran', () => {
const postMessage = jest.spyOn( iframe.contentWindow, 'postMessage' ).mockImplementation( () => {});

window.dispatchEvent( new MessageEvent( 'message', { data: { type: 'om-dam', action: 'getUrl' }}) );

expect( postMessage ).toHaveBeenCalledWith({ siteUrl: SITE_URL, type: 'om-dam', context: 'browse' }, '*' );
});
});
14 changes: 9 additions & 5 deletions assets/src/media/admin-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import './scss/admin-page.scss';
const { optmlAdminPage } = window;

const { siteUrl } = optmlAdminPage;
const iframe = document.getElementById( 'om-dam' );

iframe.addEventListener( 'load', function() {
// This runs in the head, before #om-dam exists: `load` does not bubble, so catch it in the capture phase.
document.addEventListener( 'load', function( event ) {
if ( 'om-dam' !== event.target.id ) {
return;
}

document.querySelector( '.om-dam-loader' ).style.display = 'none';
iframe.style.display = '';
});
event.target.style.display = '';
}, true );

window.addEventListener( 'message', function( event ) {
if ( ! event.data ) {
Expand All @@ -31,5 +35,5 @@ window.addEventListener( 'message', function( event ) {
return;
}

iframe.contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' );
document.getElementById( 'om-dam' ).contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' );
});
4 changes: 2 additions & 2 deletions inc/dam.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,12 @@ public function render_dashboard_page() {
display: none !important;
}
</style>
<iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>" allow="clipboard-read; clipboard-write"></iframe>
<div class="om-dam-loader">
<img src="<?php echo esc_url( OPTML_URL . 'assets/img/logo.png' ); ?>" alt="Optimole Logo"
class="om-dam-logo">
<p><?php echo esc_html__( 'Loading', 'optimole-wp' ); ?>...</p>
</div>
<iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>" allow="clipboard-read; clipboard-write"></iframe>
<?php
}

Expand Down Expand Up @@ -521,7 +521,7 @@ public function enqueue_admin_page_scripts() {
OPTML_URL . 'assets/build/media/admin-page.js',
$asset_file['dependencies'],
$asset_file['version'],
true
false
);

wp_localize_script(
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"make-pot": "docker run --user root --rm --volume $(pwd):/var/www/html/optimole-wp wordpress:cli bash -c 'php -d memory_limit=512M $(which wp) --version --allow-root && wp i18n make-pot optimole-wp ./optimole-wp/languages/optimole-wp.pot --include=inc,assets/src --allow-root --domain=optimole-wp'"
},
"devDependencies": {
"@playwright/test": "^1.50.1",
"@playwright/test": "^1.61.1",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/exec": "^5.0.0",
"@semantic-release/git": "^9.0.0",
Expand Down
13 changes: 13 additions & 0 deletions tests/test-dam.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ public function test_iframe_url_all_cloud_sites_enabled() {
$this->assertEmpty( $data['sites'] );
}

public function test_dashboard_page_renders_loader_before_iframe_issue_1211() {
ob_start();
$this->dam->render_dashboard_page();
$html = ob_get_clean();

$loader = strpos( $html, 'class="om-dam-loader"' );
$iframe = strpos( $html, 'id="om-dam"' );

$this->assertNotFalse( $loader );
$this->assertNotFalse( $iframe );
$this->assertLessThan( $iframe, $loader );
}

public function test_insert_attachments() {
foreach ( $this->inserted_ids as $index => $id ) {
$this->assertIsInt( $id );
Expand Down
Loading