parent
74ccbff716
commit
875c43cee4
|
@ -235,6 +235,19 @@ module.exports = Trait( 'Evented',
|
|||
},
|
||||
|
||||
|
||||
/**
|
||||
* Alias for #on
|
||||
*
|
||||
* @see {#on}
|
||||
*/
|
||||
addListener( ev, listener )
|
||||
{
|
||||
// we proxy to `on` (instead of the reverse) because `on` is
|
||||
// traditionally more often used, so we want to save overhead there
|
||||
return this.on( ev, listener );
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Removes a previously hooked listener in constant time, preventing it
|
||||
* from being invoked on future emits of event EV
|
||||
|
|
|
@ -141,48 +141,54 @@ describe( 'event.Evented', () =>
|
|||
} );
|
||||
|
||||
|
||||
describe( '#on', () =>
|
||||
/**
|
||||
* `addListener` is an alias for `on` (consistent with Node.js' API)
|
||||
*/
|
||||
[ 'on', 'addListener' ].forEach( on =>
|
||||
{
|
||||
it( 'does not allow hooking undeclared events', () =>
|
||||
describe( ( '#' + on ), () =>
|
||||
{
|
||||
var badevent = 'bazbar';
|
||||
it( 'does not allow hooking undeclared events', () =>
|
||||
{
|
||||
var badevent = 'bazbar';
|
||||
|
||||
expect( () => stub.on( badevent, ()=>{} ) )
|
||||
.to.throw( Error, badevent );
|
||||
} );
|
||||
expect( () => stub[ on ]( badevent, ()=>{} ) )
|
||||
.to.throw( Error, badevent );
|
||||
} );
|
||||
|
||||
|
||||
it( 'allows hooking declared events', () =>
|
||||
{
|
||||
var name = 'testev';
|
||||
stub.evDefineEvents( [ name ] )
|
||||
.on( name, ()=>{} );
|
||||
} );
|
||||
it( 'allows hooking declared events', () =>
|
||||
{
|
||||
var name = 'testev';
|
||||
stub.evDefineEvents( [ name ] )
|
||||
[ on ]( name, ()=>{} );
|
||||
} );
|
||||
|
||||
|
||||
it( 'returns self', () =>
|
||||
{
|
||||
var name = 'testev';
|
||||
it( 'returns self', () =>
|
||||
{
|
||||
var name = 'testev';
|
||||
|
||||
var ret = stub.evDefineEvents( [ name ] )
|
||||
.on( name, ()=>{} );
|
||||
var ret = stub.evDefineEvents( [ name ] )
|
||||
[ on ]( name, ()=>{} );
|
||||
|
||||
expect( ret ).to.equal( stub );
|
||||
} );
|
||||
expect( ret ).to.equal( stub );
|
||||
} );
|
||||
|
||||
|
||||
it( 'requires that listener is a function', () =>
|
||||
{
|
||||
var ev = 'foo';
|
||||
stub.evDefineEvents( [ ev ] );
|
||||
it( 'requires that listener is a function', () =>
|
||||
{
|
||||
var ev = 'foo';
|
||||
stub.evDefineEvents( [ ev ] );
|
||||
|
||||
// OK
|
||||
expect( () => stub.on( ev, ()=>{} ) )
|
||||
.to.not.throw( TypeError );
|
||||
// OK
|
||||
expect( () => stub[ on ]( ev, ()=>{} ) )
|
||||
.to.not.throw( TypeError );
|
||||
|
||||
// bad
|
||||
expect( () => stub.on( ev, "kittens" ) )
|
||||
.to.throw( TypeError );
|
||||
// bad
|
||||
expect( () => stub[ on ]( ev, "kittens" ) )
|
||||
.to.throw( TypeError );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
||||
|
|
Loading…
Reference in New Issue