Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed May 28, 2016
1 parent 933f03e commit 2453d79
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 50 deletions.
6 changes: 5 additions & 1 deletion src/interface/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
159 changes: 112 additions & 47 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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', {
Expand All @@ -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', {

Expand All @@ -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: {
Expand All @@ -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');
});
});
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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');
});
});
});
});

0 comments on commit 2453d79

Please # to comment.