diff --git a/src/event/Evented.js b/src/event/Evented.js index 47682f6..67bc848 100644 --- a/src/event/Evented.js +++ b/src/event/Evented.js @@ -193,7 +193,7 @@ module.exports = Trait( 'Evented', else if ( typeof listener !== 'function' ) { throw TypeError( "Event listener must be a function" ); } - else if ( this._hooksEvent( ev, listener ) ) + else if ( this.hooksEvent( ev, listener ) ) { throw Error( `Listener has already been bound to event \`${ev}'` @@ -260,10 +260,10 @@ module.exports = Trait( 'Evented', * * @return {boolean} whether LISTENER hooks event EV */ - _hooksEvent( ev, listener ) + hooksEvent( ev, listener ) { let levdata = listener[ _evid ]; - return ( levdata && ( levdata[ ev ] !== undefined ) ); + return !!( levdata && ( levdata[ ev ] !== undefined ) ); }, diff --git a/test/event/EventedTest.js b/test/event/EventedTest.js index 850c67d..405f069 100644 --- a/test/event/EventedTest.js +++ b/test/event/EventedTest.js @@ -597,5 +597,54 @@ describe( 'event.Evented', () => } ); } ); } ); + + + describe( '#hooksEvent', () => + { + var ev = 'foo', + f; + + beforeEach( () => + { + // ensure fresh listener + f = () => {}; + } ); + + + it( 'recognizes listener for a hooked event', () => + { + stub.evDefineEvents( [ ev ] ) + .on( ev, f ); + + expect( stub.hooksEvent( ev, f ) ).to.be.true; + } ); + + + it( 'does not recognize listener for non-hooked event', () => + { + expect( stub.hooksEvent( ev, f ) ).to.be.false; + } ); + + + /** + * Technically this test is sufficient to make the previous two + * redundant, but they are retained for clairty in debugging. + */ + it( 'does not recognize listeners of other events', () => + { + var ev2 = 'bar', + f2 = () => {}; + + stub.evDefineEvents( [ ev, ev2 ] ) + .on( ev, f ) + .on( ev2, f2 ); + + expect( stub.hooksEvent( ev, f ) ).to.be.true; + expect( stub.hooksEvent( ev2, f ) ).to.be.false; + + expect( stub.hooksEvent( ev, f2 ) ).to.be.false; + expect( stub.hooksEvent( ev2, f2 ) ).to.be.true; + } ); + } ); } );