123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var Emitter = require('../index.js');
  2. var test = require('tape');
  3. test('subscribes to an event', function (t) {
  4. var emitter = new Emitter();
  5. emitter.on('test', function () {});
  6. t.equal(emitter.e.test.length, 1, 'subscribed to event');
  7. t.end();
  8. });
  9. test('subscribes to an event with context', function (t) {
  10. var emitter = new Emitter();
  11. var context = {
  12. contextValue: true
  13. };
  14. emitter.on('test', function () {
  15. t.ok(this.contextValue, 'is in context');
  16. t.end();
  17. }, context);
  18. emitter.emit('test');
  19. });
  20. test('subscibes only once to an event', function (t) {
  21. var emitter = new Emitter();
  22. emitter.once('test', function () {
  23. t.notOk(emitter.e.test, 'removed event from list');
  24. t.end();
  25. });
  26. emitter.emit('test');
  27. });
  28. test('keeps context when subscribed only once', function (t) {
  29. var emitter = new Emitter();
  30. var context = {
  31. contextValue: true
  32. };
  33. emitter.once('test', function () {
  34. t.ok(this.contextValue, 'is in context');
  35. t.notOk(emitter.e.test, 'not subscribed anymore');
  36. t.end();
  37. }, context);
  38. emitter.emit('test');
  39. });
  40. test('emits an event', function (t) {
  41. var emitter = new Emitter();
  42. emitter.on('test', function () {
  43. t.ok(true, 'triggered event');
  44. t.end();
  45. });
  46. emitter.emit('test');
  47. });
  48. test('passes all arguments to event listener', function (t) {
  49. var emitter = new Emitter();
  50. emitter.on('test', function (arg1, arg2) {
  51. t.equal(arg1, 'arg1', 'passed the first argument');
  52. t.equal(arg2, 'arg2', 'passed the second argument');
  53. t.end();
  54. });
  55. emitter.emit('test', 'arg1', 'arg2');
  56. });
  57. test('unsubscribes from all events with name', function (t) {
  58. var emitter = new Emitter();
  59. emitter.on('test', function () {
  60. t.ok(false, 'should not get called');
  61. });
  62. emitter.off('test');
  63. emitter.emit('test')
  64. process.nextTick(function () {
  65. t.end();
  66. });
  67. });
  68. test('unsubscribes single event with name and callback', function (t) {
  69. var emitter = new Emitter();
  70. var fn = function () {
  71. t.ok(false, 'should not get called');
  72. }
  73. emitter.on('test', fn);
  74. emitter.off('test', fn);
  75. emitter.emit('test')
  76. process.nextTick(function () {
  77. t.end();
  78. });
  79. });
  80. // Test added by https://github.com/lazd
  81. // From PR: https://github.com/scottcorgan/tiny-emitter/pull/6
  82. test('unsubscribes single event with name and callback when subscribed twice', function (t) {
  83. var emitter = new Emitter();
  84. var fn = function () {
  85. t.ok(false, 'should not get called');
  86. };
  87. emitter.on('test', fn);
  88. emitter.on('test', fn);
  89. emitter.off('test', fn);
  90. emitter.emit('test');
  91. process.nextTick(function () {
  92. t.notOk(emitter.e['test'], 'removes all events');
  93. t.end();
  94. });
  95. });
  96. test('unsubscribes single event with name and callback when subscribed twice out of order', function (t) {
  97. var emitter = new Emitter();
  98. var calls = 0;
  99. var fn = function () {
  100. t.ok(false, 'should not get called');
  101. };
  102. var fn2 = function () {
  103. calls++;
  104. };
  105. emitter.on('test', fn);
  106. emitter.on('test', fn2);
  107. emitter.on('test', fn);
  108. emitter.off('test', fn);
  109. emitter.emit('test');
  110. process.nextTick(function () {
  111. t.equal(calls, 1, 'callback was called');
  112. t.end();
  113. });
  114. });
  115. test('removes an event inside another event', function (t) {
  116. var emitter = new Emitter();
  117. emitter.on('test', function () {
  118. t.equal(emitter.e.test.length, 1, 'event is still in list');
  119. emitter.off('test');
  120. t.notOk(emitter.e.test, 0, 'event is gone from list');
  121. t.end();
  122. });
  123. emitter.emit('test');
  124. });
  125. test('event is emitted even if unsubscribed in the event callback', function (t) {
  126. var emitter = new Emitter();
  127. var calls = 0;
  128. var fn = function () {
  129. calls += 1;
  130. emitter.off('test', fn);
  131. };
  132. emitter.on('test', fn);
  133. emitter.on('test', function () {
  134. calls += 1;
  135. });
  136. emitter.on('test', function () {
  137. calls += 1;
  138. });
  139. process.nextTick(function () {
  140. t.equal(calls, 3, 'all callbacks were called');
  141. t.end();
  142. });
  143. emitter.emit('test');
  144. });
  145. test('calling off before any events added does nothing', function (t) {
  146. var emitter = new Emitter();
  147. emitter.off('test', function () {});
  148. t.end();
  149. });
  150. test('emitting event that has not been subscribed to yet', function (t) {
  151. var emitter = new Emitter();
  152. emitter.emit('some-event', 'some message');
  153. t.end();
  154. });
  155. test('unsubscribes single event with name and callback which was subscribed once', function (t) {
  156. var emitter = new Emitter();
  157. var fn = function () {
  158. t.fail('event not unsubscribed');
  159. }
  160. emitter.once('test', fn);
  161. emitter.off('test', fn);
  162. emitter.emit('test');
  163. t.end();
  164. });