Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit399a78e

Browse files
authored
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 catchthose as well, failing the test.Add a `source` field to the payload JSON and check for it before treating themessage as coming from our own test to make sure the test passes even with suchbrowser extensions installed.Closesgh-5478
1 parent7cdd837 commit399a78e

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

‎test/data/event/onbeforeunload.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
<script>
55
functionreport(event){
66
varpayload={
7+
source:"jQuery onbeforeunload iframe test",
78
event:event.type
89
};
9-
returnparent.postMessage(JSON.stringify(payload),"*");
10+
returnparent.postMessage(JSON.stringify(payload),"*");
1011
}
1112

1213
jQuery(window).on("beforeunload",function(event){
1314
report(event);
14-
}).on("load",function(event){
15-
setTimeout(function(){
15+
}).on("load",function(event){
16+
setTimeout(function(){
1617
window.location.reload();
17-
},50);
18-
});
18+
},50);
19+
});
1920
</script>
2021
</html>

‎test/unit/event.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,13 +1448,22 @@ QUnit[ /(ipad|iphone|ipod)/i.test( navigator.userAgent ) ? "skip" : "test" ](
14481448
vardone=assert.async();
14491449

14501450
window.onmessage=function(event){
1451-
varpayload=JSON.parse(event.data);
1451+
try{
1452+
varpayload=JSON.parse(event.data);
14521453

1453-
assert.ok(payload.event,"beforeunload","beforeunload event");
1454+
// Ignore unrelated messages
1455+
if(payload.source==="jQuery onbeforeunload iframe test"){
1456+
assert.ok(payload.event,"beforeunload","beforeunload event");
14541457

1455-
iframe.remove();
1456-
window.onmessage=null;
1457-
done();
1458+
iframe.remove();
1459+
window.onmessage=null;
1460+
done();
1461+
}
1462+
}catch(e){
1463+
1464+
// Messages may come from other sources, like browser extensions;
1465+
// some may not be valid JSONs and thus cannot be `JSON.parse`d.
1466+
}
14581467
};
14591468

14601469
iframe.appendTo("#qunit-fixture");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp