test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* jshint node: true, unused: true, undef: true */
  2. /* globals it */
  3. var assert = require('assert');
  4. var EvEmitter = require('../ev-emitter');
  5. it( 'should emitEvent', function() {
  6. var emitter = new EvEmitter();
  7. var didPop;
  8. emitter.on( 'pop', function() {
  9. didPop = true;
  10. });
  11. emitter.emitEvent( 'pop' );
  12. assert.ok( didPop, 'event emitted' );
  13. });
  14. it( 'emitEvent should pass argument to listener', function() {
  15. var emitter = new EvEmitter();
  16. var result;
  17. function onPop( arg ) {
  18. result = arg;
  19. }
  20. emitter.on( 'pop', onPop );
  21. emitter.emitEvent( 'pop', [ 1 ] );
  22. assert.equal( result, 1, 'event emitted, arg passed' );
  23. });
  24. it( 'does not allow same listener to be added', function() {
  25. var emitter = new EvEmitter();
  26. var ticks = 0;
  27. function onPop() {
  28. ticks++;
  29. }
  30. emitter.on( 'pop', onPop );
  31. emitter.on( 'pop', onPop );
  32. var _onPop = onPop;
  33. emitter.on( 'pop', _onPop );
  34. emitter.emitEvent('pop');
  35. assert.equal( ticks, 1, '1 tick for same listener' );
  36. });
  37. it( 'should remove listener with .off()', function() {
  38. var emitter = new EvEmitter();
  39. var ticks = 0;
  40. function onPop() {
  41. ticks++;
  42. }
  43. emitter.on( 'pop', onPop );
  44. emitter.emitEvent('pop');
  45. emitter.off( 'pop', onPop );
  46. emitter.emitEvent('pop');
  47. assert.equal( ticks, 1, '.off() removed listener' );
  48. // reset
  49. var ary = [];
  50. ticks = 0;
  51. emitter.allOff();
  52. function onPopA() {
  53. ticks++;
  54. ary.push('a');
  55. if ( ticks == 2 ) {
  56. emitter.off( 'pop', onPopA );
  57. }
  58. }
  59. function onPopB() {
  60. ary.push('b');
  61. }
  62. emitter.on( 'pop', onPopA );
  63. emitter.on( 'pop', onPopB );
  64. emitter.emitEvent('pop'); // a,b
  65. emitter.emitEvent('pop'); // a,b - remove onPopA
  66. emitter.emitEvent('pop'); // b
  67. assert.equal( ary.join(','), 'a,b,a,b,b', '.off in listener does not interfer' );
  68. });
  69. it( 'should handle once()', function() {
  70. var emitter = new EvEmitter();
  71. var ary = [];
  72. emitter.on( 'pop', function() {
  73. ary.push('a');
  74. });
  75. emitter.once( 'pop', function() {
  76. ary.push('b');
  77. });
  78. emitter.on( 'pop', function() {
  79. ary.push('c');
  80. });
  81. emitter.emitEvent('pop');
  82. emitter.emitEvent('pop');
  83. assert.equal( ary.join(','), 'a,b,c,a,c', 'once listener triggered once' );
  84. // reset
  85. emitter.allOff();
  86. ary = [];
  87. // add two identical but not === listeners, only do one once
  88. emitter.on( 'pop', function() {
  89. ary.push('a');
  90. });
  91. emitter.once( 'pop', function() {
  92. ary.push('a');
  93. });
  94. emitter.emitEvent('pop');
  95. emitter.emitEvent('pop');
  96. assert.equal( ary.join(','), 'a,a,a', 'identical listeners do not interfere with once' );
  97. });
  98. it( 'does not infinite loop in once()', function() {
  99. var emitter = new EvEmitter();
  100. var ticks = 0;
  101. function onPop() {
  102. ticks++;
  103. if ( ticks < 4 ) {
  104. emitter.emitEvent('pop');
  105. }
  106. }
  107. emitter.once( 'pop', onPop );
  108. emitter.emitEvent('pop');
  109. assert.equal( ticks, 1, '1 tick with emitEvent in once' );
  110. });
  111. it( 'handles emitEvent with no listeners', function() {
  112. var emitter = new EvEmitter();
  113. assert.doesNotThrow( function() {
  114. emitter.emitEvent( 'pop', [ 1, 2, 3 ] );
  115. });
  116. function onPop() {}
  117. emitter.on( 'pop', onPop );
  118. emitter.off( 'pop', onPop );
  119. assert.doesNotThrow( function() {
  120. emitter.emitEvent( 'pop', [ 1, 2, 3 ] );
  121. });
  122. emitter.on( 'pop', onPop );
  123. emitter.emitEvent( 'pop', [ 1, 2, 3 ] );
  124. emitter.off( 'pop', onPop );
  125. assert.doesNotThrow( function() {
  126. emitter.emitEvent( 'pop', [ 1, 2, 3 ] );
  127. });
  128. });
  129. it( 'removes all listeners after allOff', function() {
  130. var emitter = new EvEmitter();
  131. var ary = [];
  132. emitter.on( 'pop', function() {
  133. ary.push('a');
  134. });
  135. emitter.on( 'pop', function() {
  136. ary.push('b');
  137. });
  138. emitter.once( 'pop', function() {
  139. ary.push('c');
  140. });
  141. emitter.emitEvent('pop');
  142. emitter.allOff();
  143. emitter.emitEvent('pop');
  144. assert.equal( ary.join(','), 'a,b,c', 'allOff removed listeners' );
  145. });