Skip to content

Commit 8050c30

Browse files
committed
Merge pull request #19 from kuzzleio/unitTests
some more tests + bugfixes
2 parents 1460c1c + 21e168e commit 8050c30

File tree

7 files changed

+316
-9
lines changed

7 files changed

+316
-9
lines changed

src/kuzzle.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ module.exports = Kuzzle = function (url, options, cb) {
171171
}
172172

173173
// Helper function ensuring that this Kuzzle object is still valid before performing a query
174-
// istanbul ignore next
175174
Object.defineProperty(this, 'isValid', {
176175
value: function () {
177176
if (this.socket === null) {
@@ -181,7 +180,6 @@ module.exports = Kuzzle = function (url, options, cb) {
181180
});
182181

183182
// Helper function copying headers to the query data
184-
// istanbul ignore next
185183
Object.defineProperty(this, 'addHeaders', {
186184
value: function (query, headers) {
187185
Object.keys(headers).forEach(function (header) {
@@ -198,7 +196,6 @@ module.exports = Kuzzle = function (url, options, cb) {
198196
* Some methods (mainly read queries) require a callback function. This function exists to avoid repetition of code,
199197
* and is called by these methods
200198
*/
201-
// istanbul ignore next
202199
Object.defineProperty(this, 'callbackRequired', {
203200
value: function (errorMessagePrefix, callback) {
204201
if (!callback || typeof callback !== 'function') {
@@ -253,7 +250,6 @@ function construct(url, cb) {
253250

254251
self.socket.once('connect_error', function (error) {
255252
self.state = 'error';
256-
self.logout();
257253

258254
if (cb) {
259255
cb(error);
@@ -604,7 +600,7 @@ Kuzzle.prototype.now = function (options, cb) {
604600

605601
this.callbackRequired('Kuzzle.now', cb);
606602

607-
this.query(null, 'read', 'now', {}, function (err, res) {
603+
this.query(null, 'read', 'now', {}, options, function (err, res) {
608604
if (err) {
609605
return cb(err);
610606
}
@@ -769,7 +765,7 @@ Kuzzle.prototype.replayQueue = function () {
769765
Kuzzle.prototype.setHeaders = function(content, replace) {
770766
var self = this;
771767

772-
if (typeof content !== 'object') {
768+
if (typeof content !== 'object' || Array.isArray(content)) {
773769
throw new Error('Expected a content object, received a ' + typeof content);
774770
}
775771

test/kuzzle/constructor.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe('Kuzzle constructor', () => {
177177
});
178178
});
179179

180-
describe('# on disconnection', () => {
180+
describe('#on disconnection', () => {
181181
var
182182
iostub = function () {
183183
var emitter = new EventEmitter;

test/kuzzle/getStatistics.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var
2+
should = require('should'),
3+
rewire = require('rewire'),
4+
Kuzzle = rewire('../../src/kuzzle');
5+
6+
describe('Kuzzle.getStatistics', function () {
7+
var
8+
kuzzle,
9+
passedOptions,
10+
error,
11+
result,
12+
queryStub = function (collection, controller, action, query, options, cb) {
13+
emitted = true;
14+
should(collection).be.null();
15+
should(controller).be.exactly('admin');
16+
should(action).be.exactly('getAllStats');
17+
should(Object.keys(query).length).be.exactly(0);
18+
19+
if (passedOptions) {
20+
should(options).match(passedOptions);
21+
}
22+
23+
cb(error, result);
24+
},
25+
emitted,
26+
controller;
27+
28+
beforeEach(function () {
29+
kuzzle = new Kuzzle('foo');
30+
emitted = false;
31+
});
32+
33+
it('should throw an error if no callback is provided', function () {
34+
should(function () { kuzzle.getStatistics(); }).throw(Error);
35+
should(emitted).be.false();
36+
should(function () { kuzzle.getStatistics(132); }).throw(Error);
37+
should(emitted).be.false();
38+
should(function () { kuzzle.getStatistics({}); }).throw(Error);
39+
should(emitted).be.false();
40+
should(function () { kuzzle.getStatistics(456, {}); }).throw(Error);
41+
should(emitted).be.false();
42+
});
43+
44+
45+
});

test/kuzzle/listenersManagement.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var
33
rewire = require('rewire'),
44
Kuzzle = rewire('../../src/kuzzle');
55

6-
describe('Kuzzle: listeners management', () => {
6+
describe('Listeners management', () => {
77
var
88
kuzzle,
99
listenerIds;

test/kuzzle/methods.test.js

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
var
2+
should = require('should'),
3+
rewire = require('rewire'),
4+
Kuzzle = rewire('../../src/kuzzle'),
5+
KuzzleDataCollection = require('../../src/kuzzleDataCollection');
6+
7+
describe('Kuzzle methods', function () {
8+
var
9+
expectedQuery,
10+
passedOptions,
11+
error,
12+
result,
13+
queryStub = function (collection, controller, action, query, options, cb) {
14+
emitted = true;
15+
should(collection).be.null();
16+
should(controller).be.exactly(expectedQuery.controller);
17+
should(action).be.exactly(expectedQuery.action);
18+
should(Object.keys(query).length).be.exactly(0);
19+
20+
if (passedOptions) {
21+
should(options).match(passedOptions);
22+
}
23+
24+
cb(error, result);
25+
},
26+
emitted,
27+
kuzzle;
28+
29+
describe('#getAllStatistics', function () {
30+
beforeEach(function () {
31+
kuzzle = new Kuzzle('foo');
32+
kuzzle.query = queryStub;
33+
emitted = false;
34+
passedOptions = null;
35+
error = null;
36+
result = {statistics: {}};
37+
expectedQuery = {
38+
controller: 'admin',
39+
action: 'getAllStats'
40+
};
41+
});
42+
43+
it('should return the kuzzle instance when called', function () {
44+
should(kuzzle.getAllStatistics(function () {})).be.exactly(kuzzle);
45+
});
46+
47+
it('should throw an error if no callback is provided', function () {
48+
should(function () { kuzzle.getAllStatistics(); }).throw(Error);
49+
should(emitted).be.false();
50+
should(function () { kuzzle.getAllStatistics({some: 'options'}); }).throw(Error);
51+
should(emitted).be.false();
52+
});
53+
54+
it('should call the query function with the right arguments', function () {
55+
kuzzle.getAllStatistics(function () {});
56+
should(emitted).be.true();
57+
58+
emitted = false;
59+
passedOptions = {queuable: true, metadata: {foo: 'bar'}};
60+
kuzzle.getAllStatistics(passedOptions, function () {});
61+
});
62+
63+
it('should reformat the statistics frame according to the documentation', function (done) {
64+
this.timeout(50);
65+
66+
result = {
67+
statistics: {
68+
123: { some: 'stats' },
69+
456: { someother: 'stats'}
70+
}
71+
};
72+
73+
kuzzle.getAllStatistics(function (err, res) {
74+
should(err).be.null();
75+
should(res).be.an.Array();
76+
should(res.length).be.exactly(2);
77+
should(res[0]).be.an.Object();
78+
should(Object.keys(res[0]).length).be.exactly(2);
79+
should(res[0].timestamp).be.exactly('123');
80+
should(res[0].some).be.exactly('stats');
81+
should(res[1]).be.an.Object();
82+
should(Object.keys(res[1]).length).be.exactly(2);
83+
should(res[1].timestamp).be.exactly('456');
84+
should(res[1].someother).be.exactly('stats');
85+
done();
86+
});
87+
});
88+
89+
it('should execute the callback with an error if an error occurs', function (done) {
90+
this.timeout(50);
91+
error = 'foobar';
92+
93+
kuzzle.getAllStatistics(function (err, res) {
94+
should(err).be.exactly('foobar');
95+
should(res).be.undefined();
96+
done();
97+
});
98+
});
99+
});
100+
101+
describe('#dataCollectionFactory', function () {
102+
beforeEach(function () {
103+
kuzzle = new Kuzzle('foo');
104+
});
105+
106+
it('should throw an error if the kuzzle instance has been invalidated', function () {
107+
kuzzle.socket = null;
108+
should(function () { kuzzle.dataCollectionFactory('foo'); }).throw(Error);
109+
});
110+
111+
it('should create and store the data collection instance if needed', function () {
112+
var collection = kuzzle.dataCollectionFactory('foo');
113+
114+
should(kuzzle.collections['foo']).not.be.undefined().and.be.instanceof(KuzzleDataCollection);
115+
should(collection).be.instanceof(KuzzleDataCollection);
116+
});
117+
118+
it('should simply pull the collection from the collection history if reinvoked', function () {
119+
kuzzle.collections['foo'] = 'bar';
120+
should(kuzzle.dataCollectionFactory('foo')).be.a.String().and.be.exactly('bar');
121+
});
122+
});
123+
124+
describe('#listCollections', function () {
125+
beforeEach(function () {
126+
kuzzle = new Kuzzle('foo');
127+
kuzzle.query = queryStub;
128+
emitted = false;
129+
passedOptions = null;
130+
error = null;
131+
result = {collections: []};
132+
expectedQuery = {
133+
controller: 'read',
134+
action: 'listCollections'
135+
};
136+
});
137+
138+
it('should return the kuzzle instance when called', function () {
139+
should(kuzzle.listCollections(function () {})).be.exactly(kuzzle);
140+
});
141+
142+
it('should throw an error if no callback has been provided', function () {
143+
should(function () { kuzzle.listCollections(); }).throw(Error);
144+
should(emitted).be.false();
145+
should(function () { kuzzle.listCollections({}); }).throw(Error);
146+
should(emitted).be.false();
147+
});
148+
149+
it('should call query with the right arguments', function (done) {
150+
this.timeout(50);
151+
result = { collections: ['foo', 'bar', 'baz', 'qux'] };
152+
153+
kuzzle.listCollections(function (err, res) {
154+
should(err).be.null();
155+
should(res).be.an.Array().and.match(result.collections);
156+
done();
157+
});
158+
});
159+
160+
it('should execute the callback with an error if one occurs', function (done) {
161+
this.timeout(50);
162+
error = 'foobar';
163+
164+
kuzzle.listCollections(function (err, res) {
165+
should(err).be.exactly('foobar');
166+
should(res).be.undefined();
167+
done();
168+
});
169+
});
170+
});
171+
172+
describe('#logout', function () {
173+
it('should clean up and invalidate the instance if called', function () {
174+
var kuzzle = new Kuzzle('foo');
175+
176+
kuzzle.collections = { foo: {}, bar: {}, baz: {} };
177+
kuzzle.logout();
178+
179+
should(kuzzle.socket).be.null();
180+
should(kuzzle.collections).be.empty();
181+
should(function () { kuzzle.isValid(); }).throw(Error);
182+
});
183+
});
184+
185+
describe('#now', function () {
186+
beforeEach(function () {
187+
kuzzle = new Kuzzle('foo');
188+
kuzzle.query = queryStub;
189+
emitted = false;
190+
passedOptions = null;
191+
error = null;
192+
result = {now: Date.now()};
193+
expectedQuery = {
194+
controller: 'read',
195+
action: 'now'
196+
};
197+
});
198+
199+
it('should return the kuzzle instance when called', function () {
200+
should(kuzzle.now(function () {})).be.exactly(kuzzle);
201+
});
202+
203+
it('should throw an error if called without a callback', function () {
204+
should(function () { kuzzle.now(); }).throw(Error);
205+
should(emitted).be.false();
206+
should(function () { kuzzle.now({}); }).throw(Error);
207+
should(emitted).be.false();
208+
});
209+
210+
it('should call query with the right arguments', function (done) {
211+
this.timeout(50);
212+
213+
kuzzle.now(function (err, res) {
214+
should(err).be.null();
215+
should(res).be.exactly(result.now);
216+
done();
217+
});
218+
});
219+
220+
it('should execute the callback with an error argument if one occurs', function (done) {
221+
this.timeout(50);
222+
error = 'foobar';
223+
224+
kuzzle.now(function (err, res) {
225+
should(err).be.exactly('foobar');
226+
should(res).be.undefined();
227+
done();
228+
});
229+
});
230+
});
231+
232+
describe('#setHeaders', function () {
233+
beforeEach(function () {
234+
kuzzle = new Kuzzle('foo');
235+
});
236+
237+
it('should throw an error if an invalid content object is provided', function () {
238+
should(function () { kuzzle.setHeaders(); }).throw(Error);
239+
should(function () { kuzzle.setHeaders(123); }).throw(Error);
240+
should(function () { kuzzle.setHeaders('foo'); }).throw(Error);
241+
should(function () { kuzzle.setHeaders(['mama', 'mia']); }).throw(Error);
242+
});
243+
244+
it('should set headers properly', function () {
245+
should(kuzzle.setHeaders({foo: 'bar'})).be.exactly(kuzzle);
246+
kuzzle.setHeaders({bar: 'baz', baz: ['bar, baz, qux', 'foo']});
247+
kuzzle.setHeaders({foo: { bar: 'baz'}});
248+
249+
should(kuzzle.headers).match({bar: 'baz', baz: ['bar, baz, qux', 'foo'], foo: { bar: 'baz'}});
250+
});
251+
252+
it('should replace existing headers if asked to', function () {
253+
kuzzle.setHeaders({foo: 'bar'});
254+
kuzzle.setHeaders({}, true);
255+
256+
should(kuzzle.headers).be.empty();
257+
});
258+
});
259+
});

test/kuzzle/offlineQueue.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var
33
rewire = require('rewire'),
44
Kuzzle = rewire('../../src/kuzzle');
55

6-
describe('Kuzzle: offline queue management', () => {
6+
describe('Offline queue management', () => {
77
var sent;
88

99
// deactivate socket.io

test/kuzzle/query.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ describe('Query management', function () {
147147
should(requestObject.collection).be.undefined();
148148
});
149149

150+
it('should add global headers without overwriting any existing query headers', function () {
151+
kuzzle.headers = { foo: 'bar', bar: 'foo' };
152+
kuzzle.query(null, 'controller', 'action', { foo: 'foo', body: {some: 'query'}});
153+
should(requestObject.foo).be.exactly('foo');
154+
should(requestObject.bar).be.exactly('foo');
155+
});
156+
150157
it('should not generate a new request ID if one is already defined', function () {
151158
kuzzle.query('collection', 'controller', 'action', { body: { some: 'query'}, requestId: 'foobar'});
152159
should(requestObject.requestId).be.exactly('foobar');

0 commit comments

Comments
 (0)