diff --git a/index.js b/index.js index 22aa583..ef0d1ec 100644 --- a/index.js +++ b/index.js @@ -132,17 +132,19 @@ logic.prototype = { return function logic_exec(results) { var execs = provs.map(function(prov, i) { - var name = names[i]; - var event = that._event(names, results, params, options); + var event, ans, name = names[i]; - var ans = typeof that[name] === 'function' && that[name](event); - if (event._isPrevented) { return ans; } + if (typeof that[name] === 'function') { + event = that._event(name, results, params, options); + reply = that[name](event); + if (event._isPrevented) { return reply; } + } return prov(name, params, options); }); - return pzero.when(execs).then(function(ans) { - results.push.apply(results, ans); + return pzero.when(execs).then(function(local) { + results.push.apply(results, local); return results; }); }; diff --git a/test/spec/logic.js b/test/spec/logic.js index 95f8555..8bceaab 100644 --- a/test/spec/logic.js +++ b/test/spec/logic.js @@ -206,6 +206,30 @@ describe('logic', function() { expect( this.provider.getCall(0).args ) .to.eql( ['10', {a: 1}, {b: 2}] ); }); + + it('should call event if logic has handler', function() { + sinon.spy(this.logic, '_event'); + this.logic['10'] = function() {}; + this.logic._exec(['10'])(); + + expect( this.logic._event.calledOnce ).to.be.ok(); + }); + + it('should pass argumetns to _event call', function() { + sinon.spy(this.logic, '_event'); + this.logic['10'] = function() {}; + this.logic._exec(['10'], {a: 1}, {b: 2})([1, 2]); + + expect( this.logic._event.getCall(0).args ) + .to.eql( ['10', [1, 2], {a: 1}, {b: 2}] ); + }); + + it('should not call event if logic hasn\'t handler', function() { + sinon.spy(this.logic, '_event'); + this.logic._exec(['10'])(); + + expect( this.logic._event.called ).not.to.be.ok(); + }); }); });