]> source.dussan.org Git - jquery.git/commitdiff
Tests: Make the beforeunload event tests work regardless of extensions
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Wed, 24 Apr 2024 22:24:55 +0000 (00:24 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Apr 2024 22:24:55 +0000 (00:24 +0200)
Some browser extensions, like React DevTools, send messages to the content area.
Since our beforeunload event test listens for all messages, it used to catch
those as well, failing the test.

Add a `source` field to the payload JSON and check for it before treating the
message as coming from our own test to make sure the test passes even with such
browser extensions installed.

Closes gh-5478

test/data/event/onbeforeunload.html
test/unit/event.js

index 11ad1964a39480e273aa75583fc3ee7bb0952b40..30053967a8d3f654c065f106beeeecf951558de5 100644 (file)
@@ -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>
index 38a805b37d38c6101e571c159eda5a2fd5dd4146..ac309d22dee6be20335e6a267bbf645481afc32f 100644 (file)
@@ -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" );