index.js 4.7 KB

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