Skip to content

Commit

Permalink
Errors: Check context in wrapFn
Browse files Browse the repository at this point in the history
  • Loading branch information
cvazac authored and nicjansma committed Apr 4, 2018
1 parent 0f1ced9 commit 1e79cd4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion plugins/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,11 @@
try {
var args = Array.prototype.slice.call(arguments);
var callbackFn = args[callbackIndex];
var targetObj = useCallingObject ? this : that;

// Determine the calling object: if 'this' is the Boomerang frame, we should swap it
// to the correct top level window context. If Boomerang isn't running in a frame,
// BOOMR.window will still point to the top-level window.
var targetObj = useCallingObject ? (this === window ? BOOMR.window : this) : that;
var wrappedFn = impl.wrap(callbackFn, targetObj, via);

args[callbackIndex] = wrappedFn;
Expand Down
2 changes: 1 addition & 1 deletion tests/boomerang-test-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@

t.configureTestEnvironment = function() {
// setup Mocha
window.mocha.globals(["BOOMR", "PageGroupVariable", "mochaResults", "BOOMR_configt"]);
window.mocha.globals(["BOOMR", "PageGroupVariable", "mochaResults", "BOOMR_configt", "_bmrEvents"]);
window.mocha.checkLeaks();

// set globals
Expand Down
14 changes: 14 additions & 0 deletions tests/page-templates/14-errors/40-wrap-context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= header %>
<%= boomerangSnippet %>
<script src="40-wrap-context.js" type="text/javascript"></script>
<script>
BOOMR_test.init({
testAfterOnBeacon: 1,
Errors: {
enabled: true,
monitorEvents: true,
sendAfterOnload: true
}
});
</script>
<%= footer %>
21 changes: 21 additions & 0 deletions tests/page-templates/14-errors/40-wrap-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*eslint-env mocha*/

describe("e2e/14-errors/40-wrap-context", function() {
it("Should infer BOOMR.window as context for unbound addEventListener calls", function(done) {
if (typeof window.EventTarget === "undefined") {
return this.skip();
}

assert.equal(BOOMR.window, top);
assert.equal(BOOMR.window, window.parent);
(function(){
addEventListener("foo", function(){
// this assert is redundant, if we get our callback, it means it fired on the right window
assert.equal(this, BOOMR.window);
done();
});
this.dispatchEvent(new Event("foo"));
}).apply(BOOMR.window);
});

});

0 comments on commit 1e79cd4

Please sign in to comment.