Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# next

## Breaking changes

* Align notification response formats #146

*__note:__ the # at the end of lines are the pull request numbers on GitHub*

# 2.2.0
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
"ws": "^1.1.1"
},
"devDependencies": {
"ora": "^0.4.0",
"webpack": "^1.13.1",
"codecov": "^1.0.1",
"eslint": "^3.7.1",
"eslint-friendly-formatter": "^2.0.6",
"codecov": "^1.0.1",
"istanbul": "0.4.5",
"istanbul-middleware": "0.2.2",
"mocha": "3.2.0",
"ora": "^0.4.0",
"proxyquire": "^1.7.10",
"rewire": "^2.5.2",
"should": "11.1.2",
"sinon": "^1.17.5"
"should-sinon": "0.0.5",
"sinon": "^1.17.5",
"webpack": "^1.13.1"
}
}
4 changes: 2 additions & 2 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function Document(kuzzleDataCollection, documentId, content) {
},
dataCollection: {
value: kuzzleDataCollection,
enumerable: true
enumerable: false
},
kuzzle: {
value: kuzzleDataCollection.kuzzle,
enumerable: true
enumerable: false
},
// writable properties
id: {
Expand Down
15 changes: 14 additions & 1 deletion src/Room.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var
uuid = require('uuid');
uuid = require('uuid'),
Document = require('./Document');

/**
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.
Expand Down Expand Up @@ -319,6 +320,18 @@ function notificationCallback (data) {
return this.kuzzle.emitEvent('jwtTokenExpired');
}

if (data.controller === 'document') {
data.type = 'document';
data.document = new Document(this.collection, data.result._id, data.result._source);
delete data.result;
}

if (data.controller === 'realtime') {
data.type = 'user';
data.user = {count: data.result.count};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not seem to match the PR description.

The result field is removed, replaced by a count property.

Here, the result field is replaced by a user property, seemingly containing a count property.
Am I next to the plaque?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question for me, but I did not follow the discussion about that, so I may be wrong...
Can you either fix that, or fix the PR description, depending what is really wanted ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial PR description has been kept for history but the actual implementation matches the one we have aggreed on.

So, no result anymore, but a new field named as the new type property. In this case, it is normal we have a user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the ref: here is the updated one: #147 (comment)

delete data.result;
}

if (this.kuzzle.requestHistory[data.requestId]) {
if (this.subscribeToSelf) {
this.callback(null, data);
Expand Down
File renamed without changes.
File renamed without changes.
78 changes: 61 additions & 17 deletions test/kuzzleRoom/methods.test.js → test/Room/methods.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var
'use strict';

const
should = require('should'),
rewire = require('rewire'),
sinon = require('sinon'),
Document = require('../../src/Document'),
Kuzzle = rewire('../../src/Kuzzle'),
Room = rewire('../../src/Room');

describe('Room methods', function () {
var
let
expectedQuery,
error,
result,
Expand Down Expand Up @@ -378,46 +381,87 @@ describe('Room methods', function () {
});

describe('#notificationCallback', function () {
var
let
notifCB = Room.__get__('notificationCallback'),
room,
called;
room;

beforeEach(function () {
called = false;
error = result = undefined;
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
dataCollection = kuzzle.collection('foo');
room = new Room(dataCollection);
room.callback = function (err, res) { called = true; error = err; result = res; };
room.callback = sinon.spy();
});

it('should call back with an error if query returns an error', function () {
notifCB.call(room, {error: 'foobar', result: {}});
should(result).be.undefined();
should(error).be.exactly('foobar');
should(room.callback)
.be.calledOnce()
.be.calledWith('foobar');
should(room.callback.firstCall.args)
.have.length(1);
});

it('should handle document notifications', () => {
notifCB.call(room, {
controller: 'document',
result: {
_id: 'id',
_source: {
foo: 'bar'
}
}
});

should(room.callback)
.be.calledOnce()
.be.calledWithMatch(null, {
controller: 'document',
type: 'document',
document: {
id: 'id',
content: {
foo: 'bar'
}
}
});
should(room.callback.firstCall.args[1].document)
.be.an.instanceOf(Document);
});

it('should return the result when one is provided', function () {
var msg = {error: null, result: {foo: 'bar'}};
notifCB.call(room, msg);
should(result).match(msg);
should(error).be.null();
it('should handle user notifications', () => {
notifCB.call(room, {
controller: 'realtime',
result: { count: 3 }
});

should(room.callback)
.be.calledOnce()
.be.calledWithMatch(null, {
type: 'user',
user: {
count: 3
}
});
});

it('should delete the result from history if emitted by this instance', function () {
it('should delete the result from history if emitted by this instance', () => {
room.subscribeToSelf = true;
kuzzle.requestHistory.bar = {};
notifCB.call(room, {error: null, result: {}, action: 'foo', requestId: 'bar'});
should(called).be.true();

should(room.callback)
.be.calledOnce();
should(kuzzle.requestHistory).be.empty();
});

it('should not forward the message if subscribeToSelf is false and the response comes from a query emitted by this instance', function () {
room.subscribeToSelf = false;
kuzzle.requestHistory.bar = {};
notifCB.call(room, {error: null, result: {}, requestId: 'bar', action: 'foo'});
should(called).be.false();

should(room.callback)
.have.callCount(0);
should(kuzzle.requestHistory).be.empty();
});

Expand Down
4 changes: 4 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--bail
--require should-sinon
--reporter spec
--recursive
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
entry: './src/Kuzzle.js',
output: {
path: './dist',
filename: 'kuzzle.js',
filename: 'Kuzzle.js',
library: 'Kuzzle',
libraryTarget: 'umd'
},
Expand Down