From 78dbec33b759f089d12bafcc5e758de7774d6ad2 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 11 Feb 2015 22:21:12 -0700 Subject: [PATCH] Fixes setup() for Dynamic Services. --- lib/application.js | 14 +++++++++----- test/application.test.js | 16 ++++++++++++++-- test/providers/primus.test.js | 7 ++++++- test/providers/socketio.test.js | 7 ++++++- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/application.js b/lib/application.js index 16a9a0b5d4..5441d90a22 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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); @@ -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; diff --git a/test/application.test.js b/test/application.test.js index bbd0e0282d..0dff4714ed 100644 --- a/test/application.test.js +++ b/test/application.test.js @@ -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'); diff --git a/test/providers/primus.test.js b/test/providers/primus.test.js index ca1069e07b..abf2401912 100644 --- a/test/providers/primus.test.js +++ b/test/providers/primus.test.js @@ -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; }); }); }); diff --git a/test/providers/socketio.test.js b/test/providers/socketio.test.js index 674b73f914..f60085f262 100644 --- a/test/providers/socketio.test.js +++ b/test/providers/socketio.test.js @@ -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; }); }); });