Skip to content

Commit

Permalink
Fixes setup() for Dynamic Services.
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallswain committed Feb 13, 2015
1 parent d881599 commit 78dbec3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
14 changes: 9 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ module.exports = {
},

service: function(location, service, options) {
location = stripSlashes(location);

if(!service) {
return this.services[stripSlashes(location)];
return this.services[location];
}

var protoService = Proto.extend(service);
var self = this;

location = stripSlashes(location);

// Add all the mixins
_.each(this.mixins, function (fn) {
fn.call(self, protoService);
Expand All @@ -46,8 +46,12 @@ module.exports = {
});

// If already _setup, just add this single service.
if (this._setup && this.addService) {
this.addService(protoService, stripSlashes(location));
if (this._setup) {
// If we're using a socket provider, register the service on it.
if (this.addService) {
this.addService(protoService, location);
}
protoService.setup(this, location);
}

this.services[location] = protoService;
Expand Down
16 changes: 14 additions & 2 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,27 @@ describe('Feathers application', function () {
});
});

it('Registers a service, wraps it, and adds the event mixin.', function (done) {
it('Registers a service, wraps it, runs service.setup(), and adds the event mixin.', function (done) {
var dummyService = {
setup: function(app, path){
this.path = path;
},

create: function (data, params, callback) {
callback(null, data);
}
};

var dynamicService;

var app = feathers().use('/dummy', dummyService);
var server = app.listen(7887);
var server = app.listen(7887, function(){
app.use('/dumdum', dummyService);
dynamicService = app.service('dumdum');

assert.ok(wrappedService.path === 'dummy', 'Wrapped service setup method ran.');
assert.ok(dynamicService.path === 'dumdum', 'Dynamic service setup method ran.');
});
var wrappedService = app.service('dummy');

assert.ok(Proto.isPrototypeOf(wrappedService), 'Service got wrapped as Uberproto object');
Expand Down
7 changes: 6 additions & 1 deletion test/providers/primus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,15 @@ describe('Primus provider', function () {
socket.send('tasks::remove', 1, {}, function() {});
socket.send('tasks::remove', 23, {}, function() {});

var ready = false;

socket.on('tasks removed', function (data) {
service.removed = oldRemoved;
assert.equal(data.id, 23);
done();
if (ready) {
done();
}
ready = true;
});
});
});
Expand Down
7 changes: 6 additions & 1 deletion test/providers/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,15 @@ describe('SocketIO provider', function () {
socket.emit('tasks::remove', 1, {}, function() {});
socket.emit('tasks::remove', 23, {}, function() {});

var ready = false;

socket.on('tasks removed', function (data) {
service.removed = oldRemoved;
assert.equal(data.id, 23);
done();
if (ready) {
done();
}
ready = true;
});
});
});
Expand Down

0 comments on commit 78dbec3

Please sign in to comment.