Skip to content

Commit bc3e901

Browse files
committed
Merge pull request #24 from kuzzleio/unitTests
code coverage
2 parents a108843 + 55c8cfe commit bc3e901

18 files changed

+2129
-405
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: node_js
33
node_js:
44
- '4.2'
55
script:
6-
- grunt && npm test
6+
- grunt && npm test && cat ./coverage/lcov.info | ./node_modules/.bin/codecov
77
before_install: npm install -g grunt-cli
88
install: npm install
99
after_deploy:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build Status](https://travis-ci.org/kuzzleio/sdk-javascript.svg?branch=master)](https://travis-ci.org/kuzzleio/sdk-javascript) [![Dependency Status](https://david-dm.org/kuzzleio/sdk-javascript.svg)](https://david-dm.org/kuzzleio/sdk-javascript)
1+
[![Build Status](https://travis-ci.org/kuzzleio/sdk-javascript.svg?branch=master)](https://travis-ci.org/kuzzleio/sdk-javascript) [![codecov.io](http://codecov.io/github/kuzzleio/sdk-javascript/coverage.svg?branch=master)](http://codecov.io/github/kuzzleio/sdk-javascript?branch=master) [![Dependency Status](https://david-dm.org/kuzzleio/sdk-javascript.svg)](https://david-dm.org/kuzzleio/sdk-javascript)
22

33

44
Kuzzle

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"browserify": "12.0.1",
32+
"codecov": "^1.0.1",
3233
"grunt": "0.4.5",
3334
"grunt-browserify": "^4.0.1",
3435
"grunt-contrib-jshint": "^0.11.3",

src/kuzzle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,15 @@ module.exports = Kuzzle = function (url, options, cb) {
234234
Kuzzle.prototype.connect = function (cb) {
235235
var self = this;
236236

237-
if (['initializing', 'ready', 'loggedOff'].indexOf(this.state) === -1) {
237+
if (['initializing', 'ready', 'loggedOff', 'error', 'offline'].indexOf(this.state) === -1) {
238238
if (cb) {
239239
cb(null, self);
240240
}
241241
return self;
242242
}
243243

244+
self.state = 'connecting';
245+
244246
self.socket = io(self.url, {
245247
reconnection: self.autoReconnect,
246248
reconnectionDelay: self.reconnectionDelay,

src/kuzzleDataCollection.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,18 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
468468
* To subscribe to the entire data collection, simply provide an empty filter.
469469
*
470470
* @param {object} filters - Filters in Kuzzle DSL format
471-
* @param {responseCallback} cb - called for each new notification
472471
* @param {object} [options] - subscriptions options
472+
* @param {responseCallback} cb - called for each new notification
473473
* @returns {*} KuzzleRoom object
474474
*/
475-
KuzzleDataCollection.prototype.subscribe = function (filters, cb, options) {
475+
KuzzleDataCollection.prototype.subscribe = function (filters, options, cb) {
476476
var room;
477477

478+
if (!cb && typeof options === 'function') {
479+
cb = options;
480+
options = null;
481+
}
482+
478483
this.kuzzle.callbackRequired('KuzzleDataCollection.subscribe', cb);
479484

480485
room = new KuzzleRoom(this, options);
@@ -562,10 +567,6 @@ KuzzleDataCollection.prototype.updateDocument = function (documentId, content, o
562567
KuzzleDataCollection.prototype.documentFactory = function (id, content) {
563568
var document = content._source ? new KuzzleDocument(this, id, content._source) : new KuzzleDocument(this, id, content);
564569

565-
if (content._version) {
566-
document.version = content._version;
567-
}
568-
569570
return document;
570571
};
571572

src/kuzzleDataMapping.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* The KuzzleDataMapping object allow to get the current mapping of a data collection and to modify it if needed.
1717
*
1818
* @param {object} kuzzleDataCollection - Instance of the inherited KuzzleDataCollection object
19+
* @param {object} mapping - mappings
1920
* @constructor
2021
*/
2122
function KuzzleDataMapping(kuzzleDataCollection, mapping) {

src/kuzzleDocument.js

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ function KuzzleDocument(kuzzleDataCollection, documentId, content) {
3737
value: kuzzleDataCollection.collection,
3838
enumerable: true
3939
},
40-
createdTimestamp: {
41-
value: 'not yet implemented',
42-
enumerable: true
43-
},
4440
dataCollection: {
4541
value: kuzzleDataCollection,
4642
enumerable: true
@@ -49,11 +45,12 @@ function KuzzleDocument(kuzzleDataCollection, documentId, content) {
4945
value: kuzzleDataCollection.kuzzle,
5046
enumerable: true
5147
},
52-
modifiedTimestamp: {
53-
value: 'not yet implemented',
54-
enumerable: true
55-
},
5648
// writable properties
49+
id: {
50+
value: undefined,
51+
enumerable: true,
52+
writable: true
53+
},
5754
content: {
5855
value: {},
5956
writable: true,
@@ -78,6 +75,10 @@ function KuzzleDocument(kuzzleDataCollection, documentId, content) {
7875
}
7976

8077
if (content) {
78+
if (content._version) {
79+
this.version = content._version;
80+
delete content._version;
81+
}
8182
this.setContent(content, true);
8283
}
8384

@@ -161,7 +162,7 @@ KuzzleDocument.prototype.delete = function (options, cb) {
161162
}
162163

163164
if (!this.id) {
164-
throw new Error('KuzzleDocument.delete: cannot delete a document that has not been saved into Kuzzle');
165+
throw new Error('KuzzleDocument.delete: cannot delete a document without a document ID');
165166
}
166167

167168
if (cb) {
@@ -244,39 +245,32 @@ KuzzleDocument.prototype.refresh = function (options, cb) {
244245
KuzzleDocument.prototype.save = function (options, cb) {
245246
var
246247
data = this.toJSON(),
247-
self = this,
248-
queryCB;
248+
self = this;
249249

250250
if (options && cb === undefined && typeof options === 'function') {
251251
cb = options;
252252
options = null;
253253
}
254254

255-
queryCB = function (error, result) {
255+
if (self.refreshing) {
256+
self.queue.push({action: 'save', args: [options, cb]});
257+
return self;
258+
}
259+
260+
data.persist = true;
261+
262+
self.kuzzle.query(this.collection, 'write', 'createOrUpdate', data, options, function (error, result) {
256263
if (error) {
257264
return cb ? cb(error) : false;
258265
}
259266

260-
if (!self.id) {
261-
Object.defineProperty(self, 'id', {
262-
value: result._id,
263-
enumerable: true
264-
});
265-
}
267+
self.id = result._id;
268+
self.version = result._version;
266269

267270
if (cb) {
268271
cb(null, self);
269272
}
270-
};
271-
272-
if (self.refreshing) {
273-
self.queue.push({action: 'save', args: [options, cb]});
274-
return self;
275-
}
276-
277-
data.persist = true;
278-
279-
self.kuzzle.query(this.collection, 'write', 'createOrUpdate', data, options, queryCB);
273+
});
280274

281275
return self;
282276
};
@@ -350,18 +344,13 @@ KuzzleDocument.prototype.subscribe = function (options, cb) {
350344

351345
this.kuzzle.callbackRequired('KuzzleDocument.subscribe', cb);
352346

353-
if (this.refreshing) {
354-
this.queue.push({action: 'subscribe', args: [cb]});
355-
return this;
356-
}
357-
358347
if (!this.id) {
359348
throw new Error('KuzzleDocument.subscribe: cannot subscribe to a document if no ID has been provided');
360349
}
361350

362351
filters = { ids: { values: [this.id] } };
363352

364-
return this.dataCollection.subscribe(filters, cb, options);
353+
return this.dataCollection.subscribe(filters, options, cb);
365354
};
366355

367356
/**
@@ -382,7 +371,7 @@ KuzzleDocument.prototype.setHeaders = function (content, replace) {
382371
/**
383372
* internal function used to dequeue calls which were put on hold while refreshing the content of this document
384373
*/
385-
function dequeue() {
374+
function dequeue () {
386375
var element;
387376

388377
while (this.queue.length > 0) {

src/kuzzleRoom.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ var uuid = require('node-uuid');
2222
* @constructor
2323
*/
2424
function KuzzleRoom(kuzzleDataCollection, options) {
25-
if (!kuzzleDataCollection) {
26-
throw new Error('KuzzleRoom: missing parameters');
27-
}
28-
2925
// Define properties
3026
Object.defineProperties(this, {
3127
// private properties
@@ -173,20 +169,16 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
173169
subscribeQuery = this.kuzzle.addHeaders({body: self.filters}, this.headers);
174170

175171
self.kuzzle.query(this.collection, 'subscribe', 'on', subscribeQuery, {metadata: this.metadata}, function (error, response) {
172+
delete self.kuzzle.subscriptions.pending[self.id];
173+
self.subscribing = false;
174+
176175
if (error) {
177-
/*
178-
If we've already subscribed to this room, Kuzzle returns the actual roomID.
179-
We'll simply ignore the error and acts as if we successfully subscribed.
180-
*/
181-
if (error.details && error.details.roomId) {
182-
self.roomId = error.details.roomId;
183-
} else {
184-
throw new Error('Error during Kuzzle subscription: ' + error.message);
185-
}
186-
} else {
187-
self.roomId = response.roomId;
176+
self.queue = [];
177+
throw new Error('Error during Kuzzle subscription: ' + error.message);
188178
}
189179

180+
self.roomId = response.roomId;
181+
190182
if (!self.kuzzle.subscriptions[self.roomId]) {
191183
self.kuzzle.subscriptions[self.roomId] = {};
192184
}
@@ -196,10 +188,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
196188
self.notifier = notificationCallback.bind(self);
197189
self.kuzzle.socket.on(self.roomId, self.notifier);
198190

199-
self.subscribing = false;
200-
delete self.kuzzle.subscriptions.pending[self.id];
201-
202-
self.dequeue();
191+
dequeue.call(self);
203192
});
204193

205194
return this;
@@ -241,7 +230,7 @@ KuzzleRoom.prototype.unsubscribe = function () {
241230
}
242231
clearInterval(interval);
243232
}
244-
}, 500);
233+
}, 100);
245234
}
246235
} else {
247236
delete self.kuzzle.subscriptions[room][self.id];
@@ -267,19 +256,6 @@ KuzzleRoom.prototype.setHeaders = function (content, replace) {
267256
return this;
268257
};
269258

270-
/**
271-
* Dequeue actions performed while subscription was being renewed
272-
*/
273-
KuzzleRoom.prototype.dequeue = function () {
274-
var element;
275-
276-
while (this.queue.length > 0) {
277-
element = this.queue.shift();
278-
279-
this[element.action].apply(this, element.args);
280-
}
281-
};
282-
283259
/**
284260
* Callback called by socket.io when a message is sent to the subscribed room ID
285261
* Calls the registered callback if the notification passes the subscription filters
@@ -312,7 +288,7 @@ function notificationCallback (data) {
312288
}
313289

314290
self.kuzzle.eventListeners[globalEvent].forEach(function (listener) {
315-
listener(self.subscriptionId, data.result);
291+
listener.fn(self.subscriptionId, data.result);
316292
});
317293
}
318294
} else if (self.kuzzle.requestHistory[data.result.requestId]) {
@@ -325,4 +301,18 @@ function notificationCallback (data) {
325301
}
326302
}
327303

304+
305+
/**
306+
* Dequeue actions performed while subscription was being renewed
307+
*/
308+
function dequeue () {
309+
var element;
310+
311+
while (this.queue.length > 0) {
312+
element = this.queue.shift();
313+
314+
this[element.action].apply(this, element.args);
315+
}
316+
}
317+
328318
module.exports = KuzzleRoom;

0 commit comments

Comments
 (0)