cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

test.js (8590B)


      1/* istanbul ignore next */
      2describe('Ultron', function () {
      3  'use strict';
      4
      5  var EventEmitter = require('eventemitter3')
      6    , EE = require('events').EventEmitter
      7    , assume = require('assume')
      8    , Ultron = require('./')
      9    , ultron
     10    , ee;
     11
     12  beforeEach(function () {
     13    ee = new EventEmitter();
     14    ultron = new Ultron(ee);
     15  });
     16
     17  afterEach(function () {
     18    ultron.destroy();
     19    ee.removeAllListeners();
     20  });
     21
     22  it('is exposed as a function', function () {
     23    assume(Ultron).is.a('function');
     24  });
     25
     26  it('can be initialized without the new keyword', function () {
     27    assume(Ultron(ee)).is.instanceOf(Ultron);
     28  });
     29
     30  it('assigns a unique id to every instance', function () {
     31    for (var i = 0; i < 100; i++) {
     32      assume(ultron.id).does.not.equal((new Ultron()).id);
     33    }
     34  });
     35
     36  it('allows removal through the event emitter', function () {
     37    function foo() {}
     38    function bar() {}
     39
     40    ultron.on('foo', foo);
     41    ultron.once('foo', bar);
     42
     43    assume(foo.__ultron).equals(ultron.id);
     44    assume(bar.__ultron).equals(ultron.id);
     45    assume(ee.listeners('foo').length).equals(2);
     46
     47    ee.removeListener('foo', foo);
     48    assume(ee.listeners('foo').length).equals(1);
     49
     50    ee.removeListener('foo', bar);
     51    assume(ee.listeners('foo').length).equals(0);
     52  });
     53
     54  describe('#on', function () {
     55    it('assigns a listener', function () {
     56      assume(ee.listeners('foo').length).equals(0);
     57
     58      function foo() {}
     59
     60      ultron.on('foo', foo);
     61      assume(ee.listeners('foo').length).equals(1);
     62      assume(ee.listeners('foo')[0]).equals(foo);
     63    });
     64
     65    it('tags the assigned function', function () {
     66      assume(ee.listeners('foo').length).equals(0);
     67
     68      ultron.on('foo', function () {});
     69      assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
     70    });
     71
     72    it('also passes in the context', function (next) {
     73      var context = 1313;
     74
     75      ultron.on('foo', function (a, b, c) {
     76        assume(a).equals('a');
     77        assume(b).equals('b');
     78        assume(c).equals('c');
     79
     80        assume(this).equals(context);
     81
     82        next();
     83      }, context);
     84
     85      ee.emit('foo', 'a', 'b', 'c');
     86    });
     87
     88    it('works with regular eventemitters as well', function (next) {
     89      var ee = new EE()
     90        , ultron = new Ultron(ee);
     91
     92      ultron.on('foo', function (a, b, c) {
     93        assume(a).equals('a');
     94        assume(b).equals('b');
     95        assume(c).equals('c');
     96
     97        next();
     98      });
     99
    100      ee.emit('foo', 'a', 'b', 'c');
    101    });
    102  });
    103
    104  describe('#once', function () {
    105    it('assigns a listener', function () {
    106      assume(ee.listeners('foo').length).equals(0);
    107
    108      function foo() {}
    109      ultron.once('foo', foo);
    110      assume(ee.listeners('foo').length).equals(1);
    111      assume(ee.listeners('foo')[0]).equals(foo);
    112    });
    113
    114    it('tags the assigned function', function () {
    115      assume(ee.listeners('foo').length).equals(0);
    116
    117      ultron.once('foo', function () {});
    118      assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
    119    });
    120
    121    it('also passes in the context', function (next) {
    122      var context = 1313;
    123
    124      ultron.once('foo', function (a, b, c) {
    125        assume(a).equals('a');
    126        assume(b).equals('b');
    127        assume(c).equals('c');
    128
    129        assume(this).equals(context);
    130
    131        next();
    132      }, context);
    133
    134      ee.emit('foo', 'a', 'b', 'c');
    135      ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
    136    });
    137
    138    it('works with regular eventemitters as well', function (next) {
    139      var ee = new EE()
    140        , ultron = new Ultron(ee);
    141
    142      ultron.once('foo', function (a, b, c) {
    143        assume(a).equals('a');
    144        assume(b).equals('b');
    145        assume(c).equals('c');
    146
    147        next();
    148      });
    149
    150      ee.emit('foo', 'a', 'b', 'c');
    151      ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
    152    });
    153  });
    154
    155  describe('#remove', function () {
    156    it('removes only our assigned `on` listeners', function () {
    157      function foo() {}
    158      function bar() {}
    159
    160      ee.on('foo', foo);
    161      ultron.on('foo', bar);
    162      assume(ee.listeners('foo').length).equals(2);
    163
    164      ultron.remove('foo');
    165      assume(ee.listeners('foo').length).equals(1);
    166      assume(ee.listeners('foo')[0]).equals(foo);
    167    });
    168
    169    it('removes our private __ultron references', function () {
    170      function once() {}
    171      function on() {}
    172
    173      assume('__ultron' in once).is.false();
    174      assume('__ultron' in on).is.false();
    175
    176      ultron.on('foo', on);
    177      ultron.once('bar', once);
    178
    179      assume('__ultron' in once).is.true();
    180      assume('__ultron' in on).is.true();
    181
    182      ultron.remove('foo, bar');
    183
    184      assume('__ultron' in once).is.false();
    185      assume('__ultron' in on).is.false();
    186
    187      ultron.destroy();
    188
    189      ee = new EE();
    190      ultron = new Ultron(ee);
    191
    192      assume('__ultron' in once).is.false();
    193      assume('__ultron' in on).is.false();
    194
    195      ultron.on('foo', on);
    196      ultron.once('bar', once);
    197
    198      assume('__ultron' in once).is.true();
    199      assume('__ultron' in on).is.true();
    200
    201      ultron.remove('foo, bar');
    202
    203      assume('__ultron' in once).is.false();
    204      assume('__ultron' in on).is.false();
    205    });
    206
    207    it('removes only our assigned `once` listeners', function () {
    208      function foo() {}
    209      function bar() {}
    210
    211      ee.once('foo', foo);
    212      ultron.once('foo', bar);
    213      assume(ee.listeners('foo').length).equals(2);
    214
    215      ultron.remove('foo');
    216      assume(ee.listeners('foo').length).equals(1);
    217      assume(ee.listeners('foo')[0]).equals(foo);
    218    });
    219
    220    it('removes only our assigned `once` listeners from regular EE', function () {
    221      var ee = new EE()
    222        , ultron = new Ultron(ee);
    223
    224      function foo() {}
    225      function bar() {}
    226
    227      ee.once('foo', foo);
    228      ultron.once('foo', bar);
    229      assume(ee.listeners('foo').length).equals(2);
    230
    231      ultron.remove('foo');
    232      assume(ee.listeners('foo').length).equals(1);
    233      assume(ee.listeners('foo')[0].listener).equals(foo);
    234    });
    235
    236    it('removes all assigned events if called without args', function () {
    237      function foo() {}
    238      function bar() {}
    239
    240      ultron.on('foo', foo);
    241      ultron.on('bar', bar);
    242
    243      assume(ee.listeners('foo').length).equals(1);
    244      assume(ee.listeners('bar').length).equals(1);
    245
    246      ultron.remove();
    247
    248      assume(ee.listeners('foo').length).equals(0);
    249      assume(ee.listeners('bar').length).equals(0);
    250    });
    251
    252    it('removes multiple listeners based on args', function () {
    253      function foo() {}
    254      function bar() {}
    255      function baz() {}
    256
    257      ultron.on('foo', foo);
    258      ultron.on('bar', bar);
    259      ultron.on('baz', baz);
    260
    261      assume(ee.listeners('foo').length).equals(1);
    262      assume(ee.listeners('bar').length).equals(1);
    263      assume(ee.listeners('baz').length).equals(1);
    264
    265      ultron.remove('foo', 'bar');
    266
    267      assume(ee.listeners('foo').length).equals(0);
    268      assume(ee.listeners('bar').length).equals(0);
    269      assume(ee.listeners('baz').length).equals(1);
    270    });
    271
    272    it('removes multiple listeners if first arg is seperated string', function () {
    273      function foo() {}
    274      function bar() {}
    275      function baz() {}
    276
    277      ultron.on('foo', foo);
    278      ultron.on('bar', bar);
    279      ultron.on('baz', baz);
    280
    281      assume(ee.listeners('foo').length).equals(1);
    282      assume(ee.listeners('bar').length).equals(1);
    283      assume(ee.listeners('baz').length).equals(1);
    284
    285      ultron.remove('foo, bar');
    286
    287      assume(ee.listeners('foo').length).equals(0);
    288      assume(ee.listeners('bar').length).equals(0);
    289      assume(ee.listeners('baz').length).equals(1);
    290    });
    291  });
    292
    293  describe('#destroy', function () {
    294    it('removes all listeners', function () {
    295      function foo() {}
    296      function bar() {}
    297      function baz() {}
    298
    299      ultron.on('foo', foo);
    300      ultron.on('bar', bar);
    301      ultron.on('baz', baz);
    302
    303      assume(ee.listeners('foo').length).equals(1);
    304      assume(ee.listeners('bar').length).equals(1);
    305      assume(ee.listeners('baz').length).equals(1);
    306
    307      ultron.destroy();
    308
    309      assume(ee.listeners('foo').length).equals(0);
    310      assume(ee.listeners('bar').length).equals(0);
    311      assume(ee.listeners('baz').length).equals(0);
    312    });
    313
    314    it('removes the .ee reference', function () {
    315      assume(ultron.ee).equals(ee);
    316      ultron.destroy();
    317      assume(ultron.ee).equals(null);
    318    });
    319
    320    it('returns booleans for state indication', function () {
    321      assume(ultron.destroy()).is.true();
    322      assume(ultron.destroy()).is.false();
    323      assume(ultron.destroy()).is.false();
    324      assume(ultron.destroy()).is.false();
    325    });
    326  });
    327});