From 5df7c37bf0cf8d0e7ef73c1d8c1a806fec30f61d Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Fri, 28 Feb 2014 11:21:52 +0100 Subject: [PATCH] Throws exception instead of returning null --- www/scripts/common/websockets/connection.js | 2 +- .../tests/specs/websockets/connection.js | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/www/scripts/common/websockets/connection.js b/www/scripts/common/websockets/connection.js index 9229f5b16c..cc274f6b3f 100644 --- a/www/scripts/common/websockets/connection.js +++ b/www/scripts/common/websockets/connection.js @@ -22,7 +22,7 @@ define([ }, close: function() { if (false === this.isConnected()) { - return; + throw "Not connected to websocket"; } activeSession.close(); activeSession = null; diff --git a/www/scripts/tests/specs/websockets/connection.js b/www/scripts/tests/specs/websockets/connection.js index 2c01148b2c..69ddce49de 100644 --- a/www/scripts/tests/specs/websockets/connection.js +++ b/www/scripts/tests/specs/websockets/connection.js @@ -20,7 +20,7 @@ define([ this.wsConnection = connection; var $this = this; var cbSuccess = function (session) { - $this.wsConnection.setSession($this.session); + activeSession = $this.session; }; window.ab = { connect: function(url, cbSuccess, cbError) { @@ -30,25 +30,32 @@ define([ }); afterEach(function () { - this.wsConnection.close(); + if (this.wsConnection.isConnected()) { + this.wsConnection.close(); + } }); it("should have a session", function () { this.wsConnection.connect(); - assert.ok(this.wsConnection.hasSession()); - }); - - it("should retrieve the session", function () { - this.wsConnection.connect(); - assert.equal(this.wsConnection.getSession().hello, this.session.hello); + assert.ok(this.wsConnection.isConnected()); }); it("should close the session", function () { this.wsConnection.connect(); - assert.ok(this.wsConnection.hasSession()); + assert.ok(this.wsConnection.isConnected()); this.wsConnection.close(); - assert.ok(!this.wsConnection.hasSession()); - assert.equal(this.wsConnection.getSession(), null); + assert.ok(!this.wsConnection.isConnected()); + }); + + it("should warn if you close the session and you are not connected", function () { + var throws = false; + try { + this.wsConnection.close(); + } catch (e) { + throws = true; + } + + assert.ok(throws); }); it("should not connect anymore after first connect", function () { @@ -66,13 +73,13 @@ define([ it("should call session subscribe once", function () { this.wsConnection.connect(); this.wsConnection.subscribe(); - expect(this.wsConnection.getSession().subscribe.should.have.callCount(1)).to.be.ok; + expect(this.session.subscribe.should.have.callCount(1)).to.be.ok; }); it("should call session unsubscribe once", function () { this.wsConnection.connect(); this.wsConnection.unsubscribe(); - expect(this.wsConnection.getSession().unsubscribe.should.have.callCount(1)).to.be.ok; + expect(this.session.unsubscribe.should.have.callCount(1)).to.be.ok; }); }); });