8000 Create an app reference on service event hook object by kaiquewdev · Pull Request #406 · feathersjs/feathers · GitHub
[go: up one dir, main page]

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
2 changes: 2 additions & 0 deletions src/mixins/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function upperCase(name) {
}

export default function(service) {
const app = this;
const isEmitter = typeof service.on === 'function' &&
typeof service.emit === 'function';
const emitter = service._rubberDuck = rubberduck.emitter(service);
Expand Down Expand Up @@ -47,6 +48,7 @@ export default function(service) {
const hook = hookObject(method, 'after', args);
const data = Array.isArray(results[1]) ? results[1] : [ results[1] ];

hook.app = app;
data.forEach(current => service.emit(event, current, hook));
} else {
service.emit('serviceError', results[0]);
Expand Down
25 changes: 25 additions & 0 deletions test/mixins/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,29 @@ describe('Event mixin', () => {
instance.emit('created', { custom: 'event' });
});
});

it('sets hook.app', done => {
const FixtureService = Proto.extend({
update(id, data, params, cb) {
setTimeout(function () {
cb(null, {
id: id,
name: data.name
});
}, 20);
}
});

const instance = create.call(FixtureService);
const dummyApp = { isApp: true };

mixinEvent.call(dummyApp, instance);

instance.on('updated', function (data, hook) {
assert.deepEqual(hook.app, dummyApp);
done();
});

instance.update(12, { name: 'Updated tester' }, {}, function () {});
});
});
0