jstonic/test/event/EventableTest.js

66 lines
2.4 KiB
JavaScript

/**
* Tests 1-N message dispatch interface
*
* Copyright (C) 2014 Mike Gerwitz
*
* This file is part of jsTonic.
*
* jstonic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var Sut = require( '../../' ).event.Eventable,
expect = require( 'chai' ).expect;
describe( 'event.Eventable', () =>
{
/**
* One critical design decision of Eventable was to produce an interface
* that is compatible with Node.js' EventEmitter prototype---this allows
* existing Node libraries that make use of it to be used wherever an
* Eventable is expected, without any frustrating adapter code that
* would deter developers.
*
* Note an important distinction here: Eventable defines a *subset* of
* the methods implemented by EventEmitter; so implementors of Eventable
* may provide other methods in addition, and EventEmitter likewise does
* the same; the only goal we care about is that EventEmitter can be
* used polymorphically with regards to Eventable. Therefore, while it's
* true that EventEmitter can be used wherever Eventable is expected,
* the reverse is not necessarily true.
*
* This test was created when the Event interface had already been
* declared "frozen", so the below methods should be correct.
*/
it( 'is compatible with Node.js Event prototype', () =>
{
let f = ( ev, listener ) => {};
let njs_ev = {
addListener: f,
on: f,
once: f,
removeListener: f,
removeAllListeners: f,
setMaxListeners: f,
listeners: f,
emit: f,
};
// via ease.js ECMAScript interop
expect( Sut.isCompatible( njs_ev ) ).to.be.true;
} );
} );