diff --git a/src/interface/server.js b/src/interface/server.js index 1e9cbf76..f0c1f454 100644 --- a/src/interface/server.js +++ b/src/interface/server.js @@ -18,7 +18,11 @@ export function listen(options) { }; if (options.once) { - options.handler = util.once(options.handler); + let handler = options.handler; + options.handler = util.once(function() { + removeRequestListener(options) + return handler.apply(this, arguments); + }); } let override = options.override || CONFIG.MOCK_MODE; diff --git a/test/child.js b/test/child.js index 850f5bcf..c9a784f5 100644 --- a/test/child.js +++ b/test/child.js @@ -8,11 +8,11 @@ window.console.karma = function() { }; postRobot.on('sendMessageToParent', function(source, data) { - postRobot.sendToParent(data.messageName); + return postRobot.sendToParent(data.messageName, data.data); }); postRobot.on('setupListener', function(source, data) { - postRobot.on(data.messageName, { override: true }, function() { + postRobot.once(data.messageName, function() { return data.data; }); }); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 9119a651..2826e853 100644 --- a/test/test.js +++ b/test/test.js @@ -26,7 +26,7 @@ var childWindow = createPopup('child.htm'); var otherChildFrame = createIframe('child.htm'); -describe('post-robot', function() { +describe('[post-robot] happy cases', function() { it('should set up a simple server and listen for a request', function(done) { @@ -75,26 +75,6 @@ describe('post-robot', function() { }).catch(done); }); - it('should get an error when messaging with an unknown name', function() { - - return postRobot.send(childFrame, 'doesntexist').then(function(data) { - throw new Error('Expected success handler to not be called'); - }, function(err) { - assert.ok(err); - }); - }); - - it('should get a callback error when messaging with an unknown name', function(done) { - - postRobot.send(childFrame, 'doesntexist', function(err, data) { - assert.ok(err); - if (data) { - throw new Error('Expected data to be blank'); - } - done(); - }); - }); - it('should pass a function across windows and be able to call it later', function(done) { postRobot.send(childFrame, 'setupListener', { @@ -112,7 +92,9 @@ describe('post-robot', function() { }); }); - it('should work when referencing the child by id', function() { + it.skip('should be able to proxy messsages from one window to another', function() { + + postRobot.proxy(childFrame, otherChildFrame); return postRobot.send(childFrame, 'setupListener', { @@ -123,15 +105,23 @@ describe('post-robot', function() { }).then(function() { - return postRobot.send('childframe', 'foo').then(function(data) { + return postRobot.send(otherChildFrame, 'sendMessageToParent', { + messageName: 'foo', + + + }).then(function(data) { assert.equal(data.foo, 'bar'); }); }); }); +}); - it('should work with a child window', function() { - return postRobot.send(childWindow, 'setupListener', { +describe('[post-robot] options', function() { + + it('should work when referencing the child by id', function() { + + return postRobot.send(childFrame, 'setupListener', { messageName: 'foo', data: { @@ -140,7 +130,7 @@ describe('post-robot', function() { }).then(function() { - return postRobot.send(childWindow, 'foo').then(function(data) { + return postRobot.send('childframe', 'foo').then(function(data) { assert.equal(data.foo, 'bar'); }); }); @@ -159,28 +149,43 @@ describe('post-robot', function() { }).then(function() { return postRobot.send(childFrame, 'sendMessageToParent', { messageName: 'foobu' - }) - }).then(function() { - assert.equal(count, 1); + }).then(function() { + throw new Error('Expected success handler to not be called'); + }, function() { + assert.equal(count, 1); + }); }); }); - it('should error out if you try to register the same listener name twice', function() { + it.skip('should be able to re-register the same once handler after the first is called', function() { - postRobot.on('onceonly', function() { - // pass + var count = 0; + + postRobot.once('foobu', { override: true }, function(source, data) { + count += data.add; }); - try { - postRobot.on('onceonly', function() { - // pass + return postRobot.send(childFrame, 'sendMessageToParent', { + messageName: 'foobu', + data: { + add: 2 + } + }).then(function() { + + postRobot.once('foobu', { override: true }, function() { + count += data.add; }); - } catch (err) { - assert.ok(err); - return; - } - throw new Error('Expected error handler to be called'); + return postRobot.send(childFrame, 'sendMessageToParent', { + messageName: 'foobu', + data: { + add: 3 + } + }); + + }).then(function() { + assert.equal(count, 5); + }); }); it('should allow you to register the same listener twice providing it is to different windows', function() { @@ -207,19 +212,60 @@ describe('post-robot', function() { }).then(function() { return postRobot.send(childFrame, 'sendMessageToParent', { messageName: 'specificchildlistener' - }) - }).then(function() { - assert.equal(count, 1); + }).then(function() { + throw new Error('Expected success handler to not be called'); + }, function(err) { + assert.ok(err); + assert.equal(count, 1); + }); }); }); +}); + + +describe('[post-robot] error cases', function() { + + it('should get an error when messaging with an unknown name', function() { + + return postRobot.send(childFrame, 'doesntexist').then(function(data) { + throw new Error('Expected success handler to not be called'); + }, function(err) { + assert.ok(err); + }); + }); + + it('should get a callback error when messaging with an unknown name', function(done) { + + postRobot.send(childFrame, 'doesntexist', function(err, data) { + assert.ok(err); + if (data) { + throw new Error('Expected data to be blank'); + } + done(); + }); + }); + + it('should error out if you try to register the same listener name twice', function() { + + postRobot.on('onceonly', function() { + // pass + }); - /* + try { + postRobot.on('onceonly', function() { + // pass + }); + } catch (err) { + assert.ok(err); + return; + } + + throw new Error('Expected error handler to be called'); + }); it('should fail when postMessage or global methods are not available', function(done) { - delete window.postMessage; delete window.__postRobot__; - delete window.frames; Object.defineProperty(window, 'postMessage', { value: function() { @@ -235,6 +281,25 @@ describe('post-robot', function() { messageName: 'nowayin' }); }); +}); + + +describe('[post-robot] popup tests', function() { + + it('should work with a child window', function() { - */ + return postRobot.send(childWindow, 'setupListener', { + + messageName: 'foo', + data: { + foo: 'bar' + } + + }).then(function() { + + return postRobot.send(childWindow, 'foo').then(function(data) { + assert.equal(data.foo, 'bar'); + }); + }); + }); }); \ No newline at end of file