From 0ca24389b23ed3a128d3aa159c39ffa701cc65a7 Mon Sep 17 00:00:00 2001 From: Supasate Choochaisri Date: Wed, 27 Apr 2016 18:15:24 +0700 Subject: [PATCH] Add new feature to emit a `remoteMethodDisabled` event when disabling a remote method. Signed-off-by: Supasate Choochaisri --- lib/application.js | 5 +++++ lib/model.js | 1 + package.json | 1 + test/app.test.js | 16 ++++++++++++++++ test/helpers/loopback-testing-helper.js | 5 ++++- test/model.test.js | 14 ++++++++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/application.js b/lib/application.js index 44e35b1d..cb7575ee 100644 --- a/lib/application.js +++ b/lib/application.js @@ -153,6 +153,11 @@ app.model = function(Model, config) { this.emit('modelRemoted', Model.sharedClass); } + self = this; + Model.on('remoteMethodDisabled', function(model, methodName) { + self.emit('remoteMethodDisabled', model, methodName); + }); + Model.shared = isPublic; Model.app = this; Model.emit('attached', this); diff --git a/lib/model.js b/lib/model.js index f159cdc8..0c0a78d7 100644 --- a/lib/model.js +++ b/lib/model.js @@ -433,6 +433,7 @@ module.exports = function(registry) { Model.disableRemoteMethod = function(name, isStatic) { this.sharedClass.disableMethod(name, isStatic || false); + this.emit('remoteMethodDisabled', this.sharedClass, name); }; Model.belongsToRemoting = function(relationName, relation, define) { diff --git a/package.json b/package.json index 65ac7132..43c4dbd0 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "loopback-boot": "^2.7.0", "mocha": "^2.1.0", "sinon": "^1.13.0", + "sinon-chai": "^2.8.0", "strong-task-emitter": "^0.0.6", "supertest": "^0.15.0" }, diff --git a/test/app.test.js b/test/app.test.js index 8da684f8..04bf6cb3 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -599,6 +599,22 @@ describe('app', function() { expect(remotedClass).to.eql(Color.sharedClass); }); + it('emits a `remoteMethodDisabled` event', function() { + var Color = PersistedModel.extend('color', { name: String }); + Color.shared = true; + var remoteMethodDisabledClass, disabledRemoteMethod; + app.on('remoteMethodDisabled', function(sharedClass, methodName) { + remoteMethodDisabledClass = sharedClass; + disabledRemoteMethod = methodName; + }); + app.model(Color); + app.models.Color.disableRemoteMethod('findOne'); + expect(remoteMethodDisabledClass).to.exist; + expect(remoteMethodDisabledClass).to.eql(Color.sharedClass); + expect(disabledRemoteMethod).to.exist; + expect(disabledRemoteMethod).to.eql('findOne'); + }); + it.onServer('updates REST API when a new model is added', function(done) { app.use(loopback.rest()); request(app).get('/colors').expect(404, function(err, res) { diff --git a/test/helpers/loopback-testing-helper.js b/test/helpers/loopback-testing-helper.js index 286de97e..fdcf189a 100644 --- a/test/helpers/loopback-testing-helper.js +++ b/test/helpers/loopback-testing-helper.js @@ -10,7 +10,10 @@ module.exports = helpers; var assert = require('assert'); var request = require('supertest'); -var expect = require('chai').expect; +var chai = require('chai'); +var expect = chai.expect; +var sinon = require('sinon'); +chai.use(require('sinon-chai')); _beforeEach.withApp = function(app) { if (app.models.User) { diff --git a/test/model.test.js b/test/model.test.js index f9a16e0a..770f75b1 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -613,6 +613,20 @@ describe.onServer('Remote Methods', function() { 'createChangeStream', ]); }); + + it('emits a `remoteMethodDisabled` event', function() { + var app = loopback(); + var model = PersistedModel.extend('TestModelForDisablingRemoteMethod'); + app.dataSource('db', { connector: 'memory' }); + app.model(model, { dataSource: 'db' }); + + var callbackSpy = require('sinon').spy(); + var TestModel = app.models.TestModelForDisablingRemoteMethod; + TestModel.on('remoteMethodDisabled', callbackSpy); + TestModel.disableRemoteMethod('findOne'); + + expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne'); + }); }); describe('Model.getApp(cb)', function() {