From 489792bbadb7af2244608e4fa24eb1eab955a8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Berthommier?= Date: Sun, 19 Aug 2018 19:18:24 +0200 Subject: [PATCH] Add breaking test (#931) * add breaking test * fix bug by replacing makeArguments --- lib/hooks/index.js | 8 ++++++-- test/hooks/hooks.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/hooks/index.js b/lib/hooks/index.js index 289dc8605..0817ada43 100644 --- a/lib/hooks/index.js +++ b/lib/hooks/index.js @@ -6,10 +6,14 @@ const { getHooks, processHooks, enableHooks, - makeArguments, ACTIVATE_HOOKS } = hooks; +const makeArguments = (service, method, hookObject) => service.methods[ method ].reduce((result, value) => ([ + ...result, + hookObject[ value ] +]), []); + const withHooks = function withHooks ({ app, service, @@ -49,7 +53,7 @@ const withHooks = function withHooks ({ } // Otherwise, call it with arguments created from the hook object - const promise = _super(...makeArguments(hookObject)); + const promise = _super(...makeArguments(service, method, hookObject)); if (!isPromise(promise)) { throw new Error(`Service method '${hookObject.method}' for '${hookObject.path}' service must return a promise`); diff --git a/test/hooks/hooks.test.js b/test/hooks/hooks.test.js index 7a3bea7e3..c6f3971a3 100644 --- a/test/hooks/hooks.test.js +++ b/test/hooks/hooks.test.js @@ -312,4 +312,34 @@ describe('hooks basics', () => { }); }); }); + + it('context.data should not change arguments', () => { + const app = feathers().use('/dummy', { + methods: { + custom: ['id', 'params'] + }, + get () {}, + custom (id, params) { + return Promise.resolve([id, params]); + } + }); + + app.service('dummy').hooks({ + before: { + all (context) { + context.test = ['all::before']; + }, + custom (context) { + context.data = { post: 'title' }; + } + } + }); + + const args = [1, { provider: 'rest' }]; + + return app.service('dummy').custom(...args) + .then(result => { + assert.deepEqual(result, args); + }); + }); });