From 5d83fadb6be83bcda54a87c1c0d73534e5aad6fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Thu, 25 Apr 2024 00:24:55 +0200 Subject: [PATCH] Tests: Make the beforeunload event tests work regardless of extensions 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 (cherry picked from commit 399a78ee9fc5802509df462a2851aef1b60b7fbc) --- test/data/event/onbeforeunload.html | 11 ++++++----- test/unit/event.js | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/test/data/event/onbeforeunload.html b/test/data/event/onbeforeunload.html index 11ad1964a..30053967a 100644 --- a/test/data/event/onbeforeunload.html +++ b/test/data/event/onbeforeunload.html @@ -4,17 +4,18 @@ diff --git a/test/unit/event.js b/test/unit/event.js index ea92ef8a0..f6f6fbce0 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -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" ); -- 2.39.5