Skip to content

Commit

Permalink
Fix #448
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSwitch committed Aug 31, 2016
1 parent 172ef0a commit b8bc4f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ hello.utils.extend(hello.utils, {
var res = 'result' in p && p.result ? JSON.parse(p.result) : false;

// Trigger the callback on the parent
parent[p.callback](res);
callback(parent, p.callback)(res);
closeWindow();
}

Expand Down Expand Up @@ -1417,7 +1417,7 @@ hello.utils.extend(hello.utils, {
var str = JSON.stringify(obj);

try {
parent[cb](str);
callback(parent, cb)(str);
}
catch (e) {
// Error thrown whilst executing parent callback
Expand All @@ -1427,6 +1427,16 @@ hello.utils.extend(hello.utils, {
closeWindow();
}

function callback(parent, callbackID) {
if (callbackID.indexOf('_hellojs_') !== 0) {
return function() {
throw 'Could not execute callback ' + callbackID;
};
}

return parent[callbackID];
}

function closeWindow() {

if (window.frameElement) {
Expand Down
18 changes: 9 additions & 9 deletions tests/specs/unit/utils/responseHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ define([], function() {
_accessToken = '1234';

_state = {
callback: 'callbackTestFunc',
callback: '_hellojs_callbackTestFunc',
network: 'network'
};

Expand All @@ -52,7 +52,7 @@ define([], function() {
};

_parent = {
callbackTestFunc: function() {
_hellojs_callbackTestFunc: function() {
// This is the callback function on the parent
}
};
Expand Down Expand Up @@ -82,7 +82,7 @@ define([], function() {
_window.close = spy;

var spy2 = sinon.spy();
_parent.callbackTestFunc = spy2;
_parent._hellojs_callbackTestFunc = spy2;

utils.responseHandler(_window, _parent);

Expand All @@ -108,7 +108,7 @@ define([], function() {
throw 'Error';
});

_parent.callbackTestFunc = spy2;
_parent._hellojs_callbackTestFunc = spy2;

utils.responseHandler(_window, _parent);

Expand All @@ -127,7 +127,7 @@ define([], function() {

it('should close the iframe window', function(done) {

window.testIframeCloses = function() {
window._hellojs_testIframeCloses = function() {
// After the initial load we can expect this to have removed itself;
setTimeout(function() {
expect(frm.parentNode).to.eql(null);
Expand All @@ -137,7 +137,7 @@ define([], function() {

// In this example we load a page containing the responseHandler script on it.
var frm = document.createElement('iframe');
frm.src = 'redirect.html?state={}&network=test&callback=testIframeCloses&access_token=token';
frm.src = 'redirect.html?state={}&network=test&callback=_hellojs_testIframeCloses&access_token=token';
document.body.appendChild(frm);

});
Expand All @@ -149,7 +149,7 @@ define([], function() {
_window.location = mockLocation('http://adodson.com/redirect.html?error=error&error_description=description&state=' + JSON.stringify(_state));

var spy2 = sinon.spy();
_parent.callbackTestFunc = spy2;
_parent._hellojs_callbackTestFunc = spy2;

utils.responseHandler(_window, _parent);

Expand All @@ -173,7 +173,7 @@ define([], function() {
_window.close = spy;

// Remove the global callback function
delete _parent.callbackTestFunc;
delete _parent._hellojs_callbackTestFunc;

// Spy on the store function
var spy2 = sinon.spy();
Expand All @@ -186,7 +186,7 @@ define([], function() {
expect(spy2.calledOnce).to.be.ok();

// Should set the callback name along with the auth response.
expect(spy2.args[0][1]).to.have.property('callback', 'callbackTestFunc');
expect(spy2.args[0][1]).to.have.property('callback', '_hellojs_callbackTestFunc');
});

});
Expand Down

0 comments on commit b8bc4f3

Please sign in to comment.