From d783f382298f402e208aa58dbf2fa3fbd67e8cd0 Mon Sep 17 00:00:00 2001 From: Maira Bello Date: Tue, 23 Dec 2014 15:24:58 -0300 Subject: [PATCH] Changes WebChannel's test to use a fake transport instead of fake socket 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. --- test/fixture/FakeTransport.js | 33 ++++++++++++++ test/webchannel/WebChannel.js | 86 ++++++++++++++++++++--------------- 2 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 test/fixture/FakeTransport.js diff --git a/test/fixture/FakeTransport.js b/test/fixture/FakeTransport.js new file mode 100644 index 00000000..b87420d5 --- /dev/null +++ b/test/fixture/FakeTransport.js @@ -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; diff --git a/test/webchannel/WebChannel.js b/test/webchannel/WebChannel.js index 0e285d18..6d263a14 100644 --- a/test/webchannel/WebChannel.js +++ b/test/webchannel/WebChannel.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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) { @@ -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); @@ -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(); }); @@ -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); @@ -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(); }); @@ -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()); });