Skip to content

Commit

Permalink
Changes WebChannel's test to use a fake transport instead of fake soc…
Browse files Browse the repository at this point in the history
…ket io

It's best to rely on the layer of code that we're directly accessing on the tested code instead of mocking something that is being used indirectly. Also, WebChannel doesn't always use SocketIO, so it's good to test it with a generic transport to make sure that it works with any transport at all, not just SocketIO.
  • Loading branch information
mairatma committed Dec 26, 2014
1 parent 18b4fbe commit d783f38
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 36 deletions.
33 changes: 33 additions & 0 deletions test/fixture/FakeTransport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

function FakeTransport(uri) {
FakeTransport.base(this, 'constructor', uri);
}
lfr.inherits(FakeTransport, lfr.Transport);

FakeTransport.prototype.close = function() {
emitAsync(this, 'close');
return this;
};

FakeTransport.prototype.open = function() {
this.emit('opening');
emitAsync(this, 'open');
return this;
};

FakeTransport.prototype.write = function(message) {
emitAsync(this, 'data', message, 10);
};

function emitAsync(emitter, eventName, data, delay) {
if (!delay) {
delay = 0;
}

setTimeout(function() {
emitter.emit(eventName, data);
}, delay);
}

module.exports = FakeTransport;
86 changes: 50 additions & 36 deletions test/webchannel/WebChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,77 @@
var assert = require('assert');
var sinon = require('sinon');
var createFakeSocketIO = require('../fixture/FakeSocketIO');
var FakeTransport = require('../fixture/FakeTransport');
require('../fixture/sandbox.js');

describe('WebChannel', function() {
before(function() {
global.FakeSocketIO = createFakeSocketIO();
});

beforeEach(function() {
global.io = function() {
return new global.FakeSocketIO();
};
global.window.location = {
origin: 'http://localhost',
pathname: '/pathname'
};
});

it('should not throw error when transport is specified', function() {
assert.doesNotThrow(function() {
new lfr.WebChannel(new lfr.WebSocketTransport(''));
}, Error);
});
describe('default transport', function() {
before(function() {
var FakeSocketIO = createFakeSocketIO();
global.io = function() {
return new FakeSocketIO();
};
});

it('should not throw error when transport is not specified', function() {
assert.doesNotThrow(function() {
new lfr.WebChannel();
after(function() {
global.io = null;
});
});

it('should throw error when web channel cannot resolve transport uri from window location', function() {
global.window.location = null;
assert.throws(function() {
new lfr.WebChannel();
it('should not throw error when transport is not specified', function() {
assert.doesNotThrow(function() {
new lfr.WebChannel();
});
});

it('should default to web socket when transport is not specified', function() {
var channel = new lfr.WebChannel();
var transport = channel.getTransport();
assert.ok(transport instanceof lfr.WebSocketTransport);
assert.strictEqual('http://localhost/pathname', transport.getUri());
});

it('should throw error when web channel cannot resolve transport uri from window location', function() {
global.window.location = null;
assert.throws(function() {
new lfr.WebChannel();
});
});
});

it('should not throw error when transport is specified', function() {
assert.doesNotThrow(function() {
new lfr.WebChannel(new FakeTransport(''));
}, Error);
});

it('should retrieve the specified transport', function() {
var transport = new lfr.WebSocketTransport('uri');
var transport = new FakeTransport('uri');
var channel = new lfr.WebChannel(transport);
assert.strictEqual(transport, channel.getTransport());
});

it('should retrieve the same transport if changed via setter', function() {
var transport = new lfr.WebSocketTransport('uri');
var channel = new lfr.WebChannel();
var transport = new FakeTransport('uri');
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.setTransport(transport);
assert.strictEqual(transport, channel.getTransport());
});

it('should set timeout', function() {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.setTimeoutMs(0);
assert.strictEqual(0, channel.getTimeoutMs());
});

it('should head message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.head(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -70,7 +84,7 @@ describe('WebChannel', function() {
});

it('should patch message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.patch(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -81,7 +95,7 @@ describe('WebChannel', function() {
});

it('should post message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.post(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -92,7 +106,7 @@ describe('WebChannel', function() {
});

it('should put message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.put(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -103,7 +117,7 @@ describe('WebChannel', function() {
});

it('should get message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.get(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -114,7 +128,7 @@ describe('WebChannel', function() {
});

it('should delete message', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
var config = {};
channel.delete(Math.PI, config).then(function(data) {
assert.strictEqual(data.config, config);
Expand All @@ -125,7 +139,7 @@ describe('WebChannel', function() {
});

it('should timeout action', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.setTimeoutMs(0);
channel.get(Math.PI)
.thenCatch(function(reason) {
Expand All @@ -138,7 +152,7 @@ describe('WebChannel', function() {
});

it('should send pending messages when transport reopens', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.post(Math.PI).then(function(data) {
assert.strictEqual(data.data, Math.PI);
assert.strictEqual(data._method, lfr.WebChannel.HttpVerbs.POST);
Expand All @@ -153,7 +167,7 @@ describe('WebChannel', function() {
});

it('should cancel pending messages when transport emits error', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.post(Math.PI).thenCatch(function() {
done();
});
Expand All @@ -164,7 +178,7 @@ describe('WebChannel', function() {
var originalWarningFn = console.warn;
console.warn = sinon.stub();

var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.getTransport().on('data', function() {
assert.strictEqual(1, console.warn.callCount);

Expand All @@ -176,7 +190,7 @@ describe('WebChannel', function() {
});

it('should not remove message from queue when mismatch message id arrives', function(done) {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.get().then(function() {
done();
});
Expand All @@ -189,7 +203,7 @@ describe('WebChannel', function() {
});

it('should dispose web channel', function() {
var channel = new lfr.WebChannel();
var channel = new lfr.WebChannel(new FakeTransport('uri'));
channel.dispose();
assert.ok(!channel.getTransport());
});
Expand Down

0 comments on commit d783f38

Please sign in to comment.