diff --git a/package.json b/package.json index 820e7a94f..d5533cfc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "2.1.0", + "version": "2.1.1", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { diff --git a/src/kuzzle.js b/src/kuzzle.js index 11a634172..ffe67c309 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -109,15 +109,18 @@ module.exports = Kuzzle = function (host, options, cb) { }, host: { value: host, + writable: true, enumerable: true }, wsPort: { value: (options && typeof options.wsPort === 'number') ? options.wsPort : 7513, - enumerable: true + enumerable: true, + writable: true }, ioPort: { value: (options && typeof options.ioPort === 'number') ? options.ioPort : 7512, - enumerable: true + enumerable: true, + writable: true }, sslConnection: { value: (options && typeof options.sslConnection === 'boolean') ? options.sslConnection : false, @@ -314,10 +317,12 @@ module.exports = Kuzzle = function (host, options, cb) { Kuzzle.prototype.connect = function () { var self = this; - if (!self.network) { - self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection); + if (self.network) { + self.disconnect(); } + self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection); + if (['initializing', 'ready', 'disconnected', 'error', 'offline'].indexOf(this.state) === -1) { if (self.connectCB) { self.connectCB(null, self); @@ -1003,6 +1008,7 @@ Kuzzle.prototype.disconnect = function () { this.state = 'disconnected'; this.network.close(); + this.network = null; for (collection in this.collections) { if (this.collections.hasOwnProperty(collection)) { diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index 8c834d197..3556773d2 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -69,8 +69,8 @@ describe('Kuzzle constructor', () => { should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: false, configurable: false }); should(kuzzle).have.propertyWithDescriptor('jwtToken', { enumerable: true, writable: true, configurable: false }); should(kuzzle).have.propertyWithDescriptor('offlineQueueLoader', { enumerable: true, writable: true, configurable: false }); - should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: false, configurable: false }); - should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: false, configurable: false }); + should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: true, configurable: false }); + should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: true, configurable: false }); should(kuzzle).have.propertyWithDescriptor('sslConnection', { enumerable: true, writable: false, configurable: false }); }); @@ -291,6 +291,20 @@ describe('Kuzzle constructor', () => { }, 10); }); + it('should first disconnect if it was connected', function () { + var + kuzzle = new Kuzzle('nowhere', {connect: 'manual'}), + disconnectStub = sinon.stub(); + + kuzzle.disconnect = disconnectStub; + kuzzle.connect(); + + should(kuzzle.disconnect.called).be.false(); + + kuzzle.connect(); + should(kuzzle.disconnect.called).be.true(); + }); + describe('=> on connection error', () => { beforeEach(function () { networkStub.onConnectError = function (cb) { @@ -382,22 +396,25 @@ describe('Kuzzle constructor', () => { it('should dequeue requests automatically on a connection success', function (done) { var - dequeued = false, - kuzzle, - KuzzleRewired = rewire(kuzzleSource), - revert = KuzzleRewired.__set__('dequeue', function () { dequeued = true; }); + dequeueStub = sinon.stub(), + kuzzle; this.timeout(500); - kuzzle = new KuzzleRewired('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false}, () => { - should(kuzzle.state).be.exactly('connected'); - should(dequeued).be.true(); - revert(); - done(); - }); + kuzzle = new Kuzzle('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false}); - kuzzle.network = networkStub; + kuzzle.queuing = true; + kuzzle.offlineQueue.push({query: 'foo'}); + kuzzle.addListener('offlineQueuePop', dequeueStub); kuzzle.connect(); + + setTimeout(function () { + should(dequeueStub.called).be.true(); + should(kuzzle.state).be.exactly('connected'); + should(kuzzle.queuing).be.false(); + should(kuzzle.offlineQueue).be.empty(); + done(); + }, 20); }); }); diff --git a/test/kuzzle/methods.test.js b/test/kuzzle/methods.test.js index 37a1766c9..de4a5df86 100644 --- a/test/kuzzle/methods.test.js +++ b/test/kuzzle/methods.test.js @@ -399,7 +399,7 @@ describe('Kuzzle methods', function () { kuzzle.collections = { foo: {}, bar: {}, baz: {} }; kuzzle.disconnect(); - should(kuzzle.network.close.called).be.true(); + should(kuzzle.network).be.null(); should(kuzzle.collections).be.empty(); should(function () { kuzzle.isValid(); }).throw(Error); });