Add WS Connection object

This commit is contained in:
Nicolas Le Goff
2014-02-26 19:34:50 +01:00
parent 0a2d4fe04d
commit 1d8c0e86cf
6 changed files with 158 additions and 27 deletions

View File

@@ -8,6 +8,7 @@
<body>
<div id="mocha"></div>
<script src="./../../../tmp-assets/mocha/mocha.js"></script>
<script src="../../assets/sinon/sinon.js"></script>
<script src="../../assets/requirejs/require.js"></script>
<script src="../../scripts/tests/main.js"></script>
<script>
@@ -17,7 +18,8 @@
'specs/admin/taskmanager',
'specs/login/home',
'specs/models',
'specs/validator'
'specs/validator',
'specs/websockets/connection'
], runMocha);
</script>
</body>

View File

@@ -9,7 +9,8 @@ require.config({
underscore: "../assets/underscore-amd/underscore",
backbone: "../assets/backbone-amd/backbone",
i18n: "../assets/i18next/i18next.amd-1.6.3",
bootstrap: "../assets/bootstrap/js/bootstrap.min"
bootstrap: "../assets/bootstrap/js/bootstrap.min",
sinonchai: "../assets/sinon-chai/sinon-chai"
},
shim: {
bootstrap: ["jquery"],

View File

@@ -0,0 +1,74 @@
define([
'chai',
'sinonchai',
'underscore',
'common/websockets/connection'
], function (chai, sinonchai, _, connection) {
var expect = chai.expect;
var assert = chai.assert;
var should = chai.should();
chai.use(sinonchai);
describe("Connection", function () {
describe("Functionnal", function () {
beforeEach(function () {
this.session = {"hello":"session"};
this.session.close = sinon.spy();
this.session.subscribe = sinon.spy();
this.session.unsubscribe = sinon.spy();
this.wsConnection = connection.getInstance();
var $this = this;
var cbSuccess = function (session) {
$this.wsConnection.setSession($this.session);
};
window.ab = {
connect: function(url, cbSuccess, cbError) {
cbSuccess($this.session);
}
}
});
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);
});
it("should close the session", function () {
this.wsConnection.connect();
assert.ok(this.wsConnection.hasSession());
this.wsConnection.close();
assert.ok(!this.wsConnection.hasSession());
assert.equal(this.wsConnection.getSession(), null);
});
it("should not connect anymore after first connect", function () {
this.wsConnection.connect();
ab.connect = sinon.spy();
this.wsConnection.connect();
this.wsConnection.connect();
this.wsConnection.connect();
expect(ab.connect.should.have.callCount(0)).to.be.ok;
});
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;
});
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;
});
});
});
});