Skip to content

Commit 9f35ef8

Browse files
committed
Merge pull request #14 from kuzzleio/unitTests
added unit tests on the main kuzzle object constructor + bugfixes
2 parents e5b3318 + e6c9a82 commit 9f35ef8

File tree

9 files changed

+407
-167
lines changed

9 files changed

+407
-167
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ examples/node_modules/
1616

1717
# Compiled files
1818
browser/
19-
19+
coverage/

.travis.yml

Lines changed: 4 additions & 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
6+
- grunt && npm test
77
before_install: npm install -g grunt-cli
88
install: npm install
99
after_deploy:
@@ -30,3 +30,6 @@ deploy:
3030
on:
3131
repo: kuzzleio/sdk-javascript
3232
branch: master
33+
notifications:
34+
slack:
35+
secure: m0BEda/eXxYkil0zDZ1uNpK13kiNbb5/80I5OqDBz6X7/WH3uhWIV6q38lI929tQKz04eHAfVDaRSoPk1XwZBnb70MgcRmSG/m4/moR7BCnx7D9hHFqhNf0EfdzwEg44dCCVtD2vUvChtYV+jK74sNd4yqeq86YXi33qS7SYLS6kYnRloX0D7sQXDy14FWU2a5+hsrPX8yBoWuPjBergvK6W1dZAG+SfDywaqsrUDs+q64yEHiYV9VlvVZ/oKXZiDnVcB2hi0uulIZ8+iZaMJFEU30uCQiK6w3HG587yx3sVJmprr64ZYHKaFJD2ZAG1td6wJ1ccJsU/ruGkj4+0CeoZUYYBYXYQ4qhQl8F2QT0m+8dWqPwzYpxEoqyvGS9ioZG/YI18V2CE1pEyc72JzEv6c3AgDbtwrLH5s3ViNts7kulk3wwCzuAww+DapSV9ggezo+Bs7OGRpXwh/saeKWUCLqTkLQTjuq2dFVdo85HsC2RrLcSsZLMTOuV/A//FNBDJBlNnuneGP84zGkWGPEja0FFXvm2TS95gh1hd2PMe+QxjygOAvIMLmDmfdlWSLayUeWbqp20EdF7TUHV0dvBucga8s3UvFhIMnnGLS9vcNEIRtVornXznwdYSLbYD0mLXNtitneXTnSAxilThKtGDPOWQFkYkyoFV9FEwKO8=

package.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.0.0-alpha.5",
4-
"description": "A connector for the Kuzzle API",
3+
"version": "1.0.0-alpha.6",
4+
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {
77
"type": "git",
@@ -11,24 +11,33 @@
1111
"keywords": [
1212
"kuzzle",
1313
"baas",
14-
"real-time",
15-
"advanced-search",
14+
"backend",
15+
"realtime",
16+
"advanced search",
1617
"bulk"
1718
],
19+
"scripts": {
20+
"test": "istanbul cover _mocha -- --recursive "
21+
},
1822
"browser": "src/kuzzle.js",
1923
"main": "index.js",
2024
"license": "Apache-2.0",
2125
"dependencies": {
2226
"bluebird": "3.0.5",
23-
"node-uuid": "1.4.3",
27+
"node-uuid": "1.4.7",
2428
"socket.io-client": "1.3.7"
2529
},
2630
"devDependencies": {
2731
"browserify": "12.0.1",
2832
"grunt": "0.4.5",
2933
"grunt-browserify": "^4.0.1",
3034
"grunt-contrib-jshint": "^0.11.3",
35+
"grunt-contrib-uglify": "0.10.1",
3136
"gruntify-eslint": "^1.0.1",
32-
"grunt-contrib-uglify": "0.9.1"
37+
"istanbul": "0.4.0",
38+
"istanbul-middleware": "0.2.1",
39+
"mocha": "2.3.3",
40+
"rewire": "^2.5.0",
41+
"should": "7.1.1"
3342
}
3443
}

src/kuzzle.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var
2727
* @constructor
2828
*/
2929
module.exports = Kuzzle = function (url, options, cb) {
30+
var self = this;
31+
3032
if (!(this instanceof Kuzzle)) {
3133
return new Kuzzle(url, options, cb);
3234
}
@@ -86,11 +88,11 @@ module.exports = Kuzzle = function (url, options, cb) {
8688
},
8789
// read-only properties
8890
autoReconnect: {
89-
value: (options && options.autoReconnect) ? options.autoReconnect : true,
91+
value: (options && typeof options.autoReconnect === 'boolean') ? options.autoReconnect : true,
9092
enumerable: true
9193
},
9294
reconnectionDelay: {
93-
value: (options && options.reconnectionDelay) ? options.reconnectionDelay : 1000,
95+
value: (options && typeof options.reconnectionDelay === 'number') ? options.reconnectionDelay : 1000,
9496
enumerable: true
9597
},
9698
// writable properties
@@ -158,8 +160,8 @@ module.exports = Kuzzle = function (url, options, cb) {
158160

159161
if (options) {
160162
Object.keys(options).forEach(function (opt) {
161-
if (this.hasOwnProperty(opt) && Object.getOwnPropertyDescriptor(this, opt).writable) {
162-
this[opt] = options[opt];
163+
if (self.hasOwnProperty(opt) && Object.getOwnPropertyDescriptor(self, opt).writable) {
164+
self[opt] = options[opt];
163165
}
164166
});
165167

@@ -169,6 +171,7 @@ module.exports = Kuzzle = function (url, options, cb) {
169171
}
170172

171173
// Helper function ensuring that this Kuzzle object is still valid before performing a query
174+
// istanbul ignore next
172175
Object.defineProperty(this, 'isValid', {
173176
value: function () {
174177
if (this.socket === null) {
@@ -178,6 +181,7 @@ module.exports = Kuzzle = function (url, options, cb) {
178181
});
179182

180183
// Helper function copying headers to the query data
184+
// istanbul ignore next
181185
Object.defineProperty(this, 'addHeaders', {
182186
value: function (query, headers) {
183187
Object.keys(headers).forEach(function (header) {
@@ -194,6 +198,7 @@ module.exports = Kuzzle = function (url, options, cb) {
194198
* Some methods (mainly read queries) require a callback function. This function exists to avoid repetition of code,
195199
* and is called by these methods
196200
*/
201+
// istanbul ignore next
197202
Object.defineProperty(this, 'callbackRequired', {
198203
value: function (errorMessagePrefix, callback) {
199204
if (!callback || typeof callback !== 'function') {
@@ -232,7 +237,11 @@ function construct(url, cb) {
232237
throw new Error('URL to Kuzzle can\'t be empty');
233238
}
234239

235-
self.socket = io(url, {reconnection: this.autoReconnect, reconnectionDelay: this.reconnectionDelay});
240+
self.socket = io(url, {
241+
reconnection: this.autoReconnect,
242+
reconnectionDelay: this.reconnectionDelay,
243+
'force new connection': true
244+
});
236245

237246
self.socket.once('connect', function () {
238247
self.state = 'connected';
@@ -242,7 +251,7 @@ function construct(url, cb) {
242251
}
243252
});
244253

245-
self.socket.once('error', function (error) {
254+
self.socket.once('connect_error', function (error) {
246255
self.state = 'error';
247256
self.logout();
248257

@@ -283,8 +292,8 @@ function construct(url, cb) {
283292

284293
// replay queued requests
285294
if (self.autoReplay) {
286-
cleanQueue.call(this);
287-
dequeue.call(this);
295+
cleanQueue.call(self);
296+
dequeue.call(self);
288297
}
289298

290299
// alert listeners
@@ -414,8 +423,6 @@ Kuzzle.prototype.addListener = function(event, listener) {
414423
* @returns {object} this
415424
*/
416425
Kuzzle.prototype.getAllStatistics = function (options, cb) {
417-
this.isValid();
418-
419426
if (!cb && typeof options === 'function') {
420427
cb = options;
421428
options = null;
@@ -470,7 +477,6 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
470477
}
471478
}
472479

473-
this.isValid();
474480
this.callbackRequired('Kuzzle.getStatistics', cb);
475481

476482
if (!timestamp) {
@@ -549,8 +555,6 @@ Kuzzle.prototype.flushQueue = function () {
549555
* @returns {object} this
550556
*/
551557
Kuzzle.prototype.listCollections = function (options, cb) {
552-
this.isValid();
553-
554558
if (!cb && typeof options === 'function') {
555559
cb = options;
556560
options = null;
@@ -593,8 +597,6 @@ Kuzzle.prototype.logout = function () {
593597
* @returns {object} this
594598
*/
595599
Kuzzle.prototype.now = function (options, cb) {
596-
this.isValid();
597-
598600
if (!cb && typeof options === 'function') {
599601
cb = options;
600602
options = null;
@@ -707,8 +709,6 @@ Kuzzle.prototype.removeAllListeners = function (event) {
707709
knownEvents = Object.keys(this.eventListeners),
708710
self = this;
709711

710-
this.isValid();
711-
712712
if (event) {
713713
if (knownEvents.indexOf(event) === -1) {
714714
throw new Error('[' + event + '] is not a known event. Known events: ' + knownEvents.toString());
@@ -733,8 +733,6 @@ Kuzzle.prototype.removeListener = function (event, listenerId) {
733733
knownEvents = Object.keys(this.eventListeners),
734734
self = this;
735735

736-
this.isValid();
737-
738736
if (knownEvents.indexOf(event) === -1) {
739737
throw new Error('[' + event + '] is not a known event. Known events: ' + knownEvents.toString());
740738
}

0 commit comments

Comments
 (0)