Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Make the beforeunload event tests work regardless of extensions #5478

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions test/data/event/onbeforeunload.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
<script>
function report( event ) {
var payload = {
source: "jQuery onbeforeunload iframe test",
event: event.type
};
return parent.postMessage( JSON.stringify(payload), "*" );
return parent.postMessage( JSON.stringify( payload ), "*" );
}

jQuery( window ).on( "beforeunload", function( event ) {
report( event );
}).on( "load", function( event ) {
setTimeout(function() {
} ).on( "load", function( event ) {
setTimeout( function() {
window.location.reload();
}, 50);
});
}, 50 );
} );
</script>
</html>
19 changes: 14 additions & 5 deletions test/unit/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,13 +1448,22 @@ QUnit[ /(ipad|iphone|ipod)/i.test( navigator.userAgent ) ? "skip" : "test" ](
var done = assert.async();

window.onmessage = function( event ) {
var payload = JSON.parse( event.data );
try {
var payload = JSON.parse( event.data );

assert.ok( payload.event, "beforeunload", "beforeunload event" );
// Ignore unrelated messages
if ( payload.source === "jQuery onbeforeunload iframe test" ) {
assert.ok( payload.event, "beforeunload", "beforeunload event" );

iframe.remove();
window.onmessage = null;
done();
iframe.remove();
window.onmessage = null;
done();
}
} catch ( e ) {

// Messages may come from other sources, like browser extensions;
// some may not be valid JSONs and thus cannot be `JSON.parse`d.
}
};

iframe.appendTo( "#qunit-fixture" );
Expand Down