emitter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var Emitter = require('..');
  2. function Custom() {
  3. Emitter.call(this)
  4. }
  5. Custom.prototype.__proto__ = Emitter.prototype;
  6. describe('Custom', function(){
  7. describe('with Emitter.call(this)', function(){
  8. it('should work', function(done){
  9. var emitter = new Custom;
  10. emitter.on('foo', done);
  11. emitter.emit('foo');
  12. })
  13. })
  14. })
  15. describe('Emitter', function(){
  16. describe('.on(event, fn)', function(){
  17. it('should add listeners', function(){
  18. var emitter = new Emitter;
  19. var calls = [];
  20. emitter.on('foo', function(val){
  21. calls.push('one', val);
  22. });
  23. emitter.on('foo', function(val){
  24. calls.push('two', val);
  25. });
  26. emitter.emit('foo', 1);
  27. emitter.emit('bar', 1);
  28. emitter.emit('foo', 2);
  29. calls.should.eql([ 'one', 1, 'two', 1, 'one', 2, 'two', 2 ]);
  30. })
  31. })
  32. describe('.once(event, fn)', function(){
  33. it('should add a single-shot listener', function(){
  34. var emitter = new Emitter;
  35. var calls = [];
  36. emitter.once('foo', function(val){
  37. calls.push('one', val);
  38. });
  39. emitter.emit('foo', 1);
  40. emitter.emit('foo', 2);
  41. emitter.emit('foo', 3);
  42. emitter.emit('bar', 1);
  43. calls.should.eql([ 'one', 1 ]);
  44. })
  45. })
  46. describe('.off(event, fn)', function(){
  47. it('should remove a listener', function(){
  48. var emitter = new Emitter;
  49. var calls = [];
  50. function one() { calls.push('one'); }
  51. function two() { calls.push('two'); }
  52. emitter.on('foo', one);
  53. emitter.on('foo', two);
  54. emitter.off('foo', two);
  55. emitter.emit('foo');
  56. calls.should.eql([ 'one' ]);
  57. })
  58. it('should work with .once()', function(){
  59. var emitter = new Emitter;
  60. var calls = [];
  61. function one() { calls.push('one'); }
  62. emitter.once('foo', one);
  63. emitter.once('fee', one);
  64. emitter.off('foo', one);
  65. emitter.emit('foo');
  66. calls.should.eql([]);
  67. })
  68. it('should work when called from an event', function(){
  69. var emitter = new Emitter
  70. , called
  71. function b () {
  72. called = true;
  73. }
  74. emitter.on('tobi', function () {
  75. emitter.off('tobi', b);
  76. });
  77. emitter.on('tobi', b);
  78. emitter.emit('tobi');
  79. called.should.be.true;
  80. called = false;
  81. emitter.emit('tobi');
  82. called.should.be.false;
  83. });
  84. })
  85. describe('.off(event)', function(){
  86. it('should remove all listeners for an event', function(){
  87. var emitter = new Emitter;
  88. var calls = [];
  89. function one() { calls.push('one'); }
  90. function two() { calls.push('two'); }
  91. emitter.on('foo', one);
  92. emitter.on('foo', two);
  93. emitter.off('foo');
  94. emitter.emit('foo');
  95. emitter.emit('foo');
  96. calls.should.eql([]);
  97. })
  98. })
  99. describe('.off()', function(){
  100. it('should remove all listeners', function(){
  101. var emitter = new Emitter;
  102. var calls = [];
  103. function one() { calls.push('one'); }
  104. function two() { calls.push('two'); }
  105. emitter.on('foo', one);
  106. emitter.on('bar', two);
  107. emitter.emit('foo');
  108. emitter.emit('bar');
  109. emitter.off();
  110. emitter.emit('foo');
  111. emitter.emit('bar');
  112. calls.should.eql(['one', 'two']);
  113. })
  114. })
  115. describe('.listeners(event)', function(){
  116. describe('when handlers are present', function(){
  117. it('should return an array of callbacks', function(){
  118. var emitter = new Emitter;
  119. function foo(){}
  120. emitter.on('foo', foo);
  121. emitter.listeners('foo').should.eql([foo]);
  122. })
  123. })
  124. describe('when no handlers are present', function(){
  125. it('should return an empty array', function(){
  126. var emitter = new Emitter;
  127. emitter.listeners('foo').should.eql([]);
  128. })
  129. })
  130. })
  131. describe('.hasListeners(event)', function(){
  132. describe('when handlers are present', function(){
  133. it('should return true', function(){
  134. var emitter = new Emitter;
  135. emitter.on('foo', function(){});
  136. emitter.hasListeners('foo').should.be.true;
  137. })
  138. })
  139. describe('when no handlers are present', function(){
  140. it('should return false', function(){
  141. var emitter = new Emitter;
  142. emitter.hasListeners('foo').should.be.false;
  143. })
  144. })
  145. })
  146. })
  147. describe('Emitter(obj)', function(){
  148. it('should mixin', function(done){
  149. var proto = {};
  150. Emitter(proto);
  151. proto.on('something', done);
  152. proto.emit('something');
  153. })
  154. })