Throws exception instead of returning null

This commit is contained in:
Nicolas Le Goff
2014-02-28 11:21:52 +01:00
parent 0299250097
commit 5df7c37bf0
2 changed files with 21 additions and 14 deletions

View File

@@ -22,7 +22,7 @@ define([
}, },
close: function() { close: function() {
if (false === this.isConnected()) { if (false === this.isConnected()) {
return; throw "Not connected to websocket";
} }
activeSession.close(); activeSession.close();
activeSession = null; activeSession = null;

View File

@@ -20,7 +20,7 @@ define([
this.wsConnection = connection; this.wsConnection = connection;
var $this = this; var $this = this;
var cbSuccess = function (session) { var cbSuccess = function (session) {
$this.wsConnection.setSession($this.session); activeSession = $this.session;
}; };
window.ab = { window.ab = {
connect: function(url, cbSuccess, cbError) { connect: function(url, cbSuccess, cbError) {
@@ -30,25 +30,32 @@ define([
}); });
afterEach(function () { afterEach(function () {
if (this.wsConnection.isConnected()) {
this.wsConnection.close(); this.wsConnection.close();
}
}); });
it("should have a session", function () { it("should have a session", function () {
this.wsConnection.connect(); this.wsConnection.connect();
assert.ok(this.wsConnection.hasSession()); assert.ok(this.wsConnection.isConnected());
});
it("should retrieve the session", function () {
this.wsConnection.connect();
assert.equal(this.wsConnection.getSession().hello, this.session.hello);
}); });
it("should close the session", function () { it("should close the session", function () {
this.wsConnection.connect(); this.wsConnection.connect();
assert.ok(this.wsConnection.hasSession()); assert.ok(this.wsConnection.isConnected());
this.wsConnection.close(); this.wsConnection.close();
assert.ok(!this.wsConnection.hasSession()); assert.ok(!this.wsConnection.isConnected());
assert.equal(this.wsConnection.getSession(), null); });
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 () { it("should not connect anymore after first connect", function () {
@@ -66,13 +73,13 @@ define([
it("should call session subscribe once", function () { it("should call session subscribe once", function () {
this.wsConnection.connect(); this.wsConnection.connect();
this.wsConnection.subscribe(); 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 () { it("should call session unsubscribe once", function () {
this.wsConnection.connect(); this.wsConnection.connect();
this.wsConnection.unsubscribe(); 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;
}); });
}); });
}); });