Jared K. Smith 650bf54
/* istanbul ignore next */
Jared K. Smith 650bf54
describe('Ultron', function () {
Jared K. Smith 650bf54
  'use strict';
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  var EventEmitter = require('eventemitter3')
Jared K. Smith 650bf54
    , EE = require('events').EventEmitter
Jared K. Smith 650bf54
    , assume = require('assume')
Jared K. Smith 650bf54
    , Ultron = require('./')
Jared K. Smith 650bf54
    , ultron
Jared K. Smith 650bf54
    , ee;
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  beforeEach(function () {
Jared K. Smith 650bf54
    ee = new EventEmitter();
Jared K. Smith 650bf54
    ultron = new Ultron(ee);
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  afterEach(function () {
Jared K. Smith 650bf54
    ultron.destroy();
Jared K. Smith 650bf54
    ee.removeAllListeners();
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  it('is exposed as a function', function () {
Jared K. Smith 650bf54
    assume(Ultron).is.a('function');
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  it('can be initialized without the new keyword', function () {
Jared K. Smith 650bf54
    assume(Ultron(ee)).is.instanceOf(Ultron);
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  it('assigns a unique id to every instance', function () {
Jared K. Smith 650bf54
    for (var i = 0; i < 100; i++) {
Jared K. Smith 650bf54
      assume(ultron.id).does.not.equal((new Ultron()).id);
Jared K. Smith 650bf54
    }
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  it('allows removal through the event emitter', function () {
Jared K. Smith 650bf54
    function foo() {}
Jared K. Smith 650bf54
    function bar() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    ultron.on('foo', foo);
Jared K. Smith 650bf54
    ultron.once('foo', bar);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    assume(foo.__ultron).equals(ultron.id);
Jared K. Smith 650bf54
    assume(bar.__ultron).equals(ultron.id);
Jared K. Smith 650bf54
    assume(ee.listeners('foo').length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    ee.removeListener('foo', foo);
Jared K. Smith 650bf54
    assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    ee.removeListener('foo', bar);
Jared K. Smith 650bf54
    assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  describe('#on', function () {
Jared K. Smith 650bf54
    it('assigns a listener', function () {
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0]).equals(foo);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('tags the assigned function', function () {
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', function () {});
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('also passes in the context', function (next) {
Jared K. Smith 650bf54
      var context = 1313;
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', function (a, b, c) {
Jared K. Smith 650bf54
        assume(a).equals('a');
Jared K. Smith 650bf54
        assume(b).equals('b');
Jared K. Smith 650bf54
        assume(c).equals('c');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        assume(this).equals(context);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        next();
Jared K. Smith 650bf54
      }, context);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c');
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('works with regular eventemitters as well', function (next) {
Jared K. Smith 650bf54
      var ee = new EE()
Jared K. Smith 650bf54
        , ultron = new Ultron(ee);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', function (a, b, c) {
Jared K. Smith 650bf54
        assume(a).equals('a');
Jared K. Smith 650bf54
        assume(b).equals('b');
Jared K. Smith 650bf54
        assume(c).equals('c');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        next();
Jared K. Smith 650bf54
      });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c');
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  describe('#once', function () {
Jared K. Smith 650bf54
    it('assigns a listener', function () {
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      ultron.once('foo', foo);
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0]).equals(foo);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('tags the assigned function', function () {
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.once('foo', function () {});
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('also passes in the context', function (next) {
Jared K. Smith 650bf54
      var context = 1313;
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.once('foo', function (a, b, c) {
Jared K. Smith 650bf54
        assume(a).equals('a');
Jared K. Smith 650bf54
        assume(b).equals('b');
Jared K. Smith 650bf54
        assume(c).equals('c');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        assume(this).equals(context);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        next();
Jared K. Smith 650bf54
      }, context);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c');
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('works with regular eventemitters as well', function (next) {
Jared K. Smith 650bf54
      var ee = new EE()
Jared K. Smith 650bf54
        , ultron = new Ultron(ee);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.once('foo', function (a, b, c) {
Jared K. Smith 650bf54
        assume(a).equals('a');
Jared K. Smith 650bf54
        assume(b).equals('b');
Jared K. Smith 650bf54
        assume(c).equals('c');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
        next();
Jared K. Smith 650bf54
      });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c');
Jared K. Smith 650bf54
      ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  describe('#remove', function () {
Jared K. Smith 650bf54
    it('removes only our assigned `on` listeners', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('foo', bar);
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove('foo');
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0]).equals(foo);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('removes only our assigned `once` listeners', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.once('foo', foo);
Jared K. Smith 650bf54
      ultron.once('foo', bar);
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove('foo');
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('foo')[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.removeAllListeners();
Jared K. Smith 650bf54
      ultron.destroy();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee = new EE();
Jared K. Smith 650bf54
      ultron = new Ultron(ee);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.once('foo', foo);
Jared K. Smith 650bf54
      ultron.once('foo', bar);
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove('foo');
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 6cd1e85
      var actual = ee.listeners('foo')[0];
Jared K. Smith 6cd1e85
      //
Jared K. Smith 6cd1e85
      // In Node.js 8+ `EventEmitter#listeners()` returns the original listener
Jared K. Smith 6cd1e85
      // also for once listeners.
Jared K. Smith 6cd1e85
      //
Jared K. Smith 6cd1e85
      assume(actual.listener || actual).equals(foo);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('removes all assigned events if called without args', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ultron.remove()).equals(ultron);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('bar', bar);
Jared K. Smith 6cd1e85
      ultron.on('baz', bar);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(1);
Jared K. Smith 6cd1e85
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(0);
Jared K. Smith 6cd1e85
      assume(ee.listeners('baz').length).equals(0);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.removeAllListeners();
Jared K. Smith 650bf54
      ultron.destroy();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee = new EE();
Jared K. Smith 650bf54
      ultron = new Ultron(ee);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ultron.remove()).equals(ultron);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('bar', bar);
Jared K. Smith 6cd1e85
      ultron.on('baz', bar);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(1);
Jared K. Smith 6cd1e85
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(0);
Jared K. Smith 6cd1e85
      assume(ee.listeners('baz').length).equals(0);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('removes multiple listeners based on args', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
      function baz() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('bar', bar);
Jared K. Smith 650bf54
      ultron.on('baz', baz);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove('foo', 'bar');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 6cd1e85
    it('removes multiple listeners if first arg is separated string', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
      function baz() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('bar', bar);
Jared K. Smith 650bf54
      ultron.on('baz', baz);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove('foo, bar');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    if ('undefined' !== typeof Symbol) it('works with ES6 symbols', function () {
Jared K. Smith 650bf54
      var s = Symbol('s');
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
      function baz() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.on(s, foo);
Jared K. Smith 650bf54
      ultron.on(s, bar);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove(s);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.once(s, bar);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove(s);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on(s, bar);
Jared K. Smith 650bf54
      ultron.on(s, baz);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(3);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove();
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.removeAllListeners();
Jared K. Smith 650bf54
      ultron.destroy();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee = new EE();
Jared K. Smith 650bf54
      ultron = new Ultron(ee);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ee.on(s, foo);
Jared K. Smith 650bf54
      ultron.on(s, bar);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove(s);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.once(s, bar);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(2);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove(s);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on(s, bar);
Jared K. Smith 650bf54
      ultron.on(s, baz);
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(3);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.remove();
Jared K. Smith 650bf54
      assume(ee.listeners(s).length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners(s)[0]).equals(foo);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
  describe('#destroy', function () {
Jared K. Smith 650bf54
    it('removes all listeners', function () {
Jared K. Smith 650bf54
      function foo() {}
Jared K. Smith 650bf54
      function bar() {}
Jared K. Smith 650bf54
      function baz() {}
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.on('foo', foo);
Jared K. Smith 650bf54
      ultron.on('bar', bar);
Jared K. Smith 650bf54
      ultron.on('baz', baz);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(1);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(1);
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      ultron.destroy();
Jared K. Smith 650bf54
Jared K. Smith 650bf54
      assume(ee.listeners('foo').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('bar').length).equals(0);
Jared K. Smith 650bf54
      assume(ee.listeners('baz').length).equals(0);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('removes the .ee reference', function () {
Jared K. Smith 650bf54
      assume(ultron.ee).equals(ee);
Jared K. Smith 650bf54
      ultron.destroy();
Jared K. Smith 650bf54
      assume(ultron.ee).equals(null);
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
Jared K. Smith 650bf54
    it('returns booleans for state indication', function () {
Jared K. Smith 650bf54
      assume(ultron.destroy()).is.true();
Jared K. Smith 650bf54
      assume(ultron.destroy()).is.false();
Jared K. Smith 650bf54
      assume(ultron.destroy()).is.false();
Jared K. Smith 650bf54
      assume(ultron.destroy()).is.false();
Jared K. Smith 650bf54
    });
Jared K. Smith 650bf54
  });
Jared K. Smith 650bf54
});