fire.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var test = require('tap').test,
  2. eventify = require('..');
  3. test('fire fires callback', function(t) {
  4. var subject = {};
  5. eventify(subject);
  6. t.plan(1);
  7. subject.on('something', function (){
  8. t.ok(true, 'fired callback');
  9. });
  10. subject.fire('something');
  11. t.end();
  12. });
  13. test('fire fires all callbacks', function(t) {
  14. t.plan(2);
  15. var subject = eventify({});
  16. var onSomething = function (){
  17. t.ok(true, 'fired callback');
  18. };
  19. subject.on('something', onSomething);
  20. subject.on('something', onSomething);
  21. subject.fire('something');
  22. t.end();
  23. });
  24. test('Chaining can be used on fire and "on"', function(t) {
  25. t.plan(2);
  26. var subject = eventify({});
  27. var onSomething = function (){
  28. t.ok(true, 'fired callback');
  29. };
  30. subject.on('beep', onSomething).on('bop', onSomething);
  31. subject.fire('beep').fire('bop');
  32. t.end();
  33. });
  34. test('fire passes all arguments', function(t) {
  35. t.plan(2);
  36. var subject = eventify({});
  37. var testX = 42,
  38. testY = 'hello';
  39. subject.on('something', function (x, y){
  40. t.equal(x, testX, "X argument should be expected");
  41. t.equal(y, testY, "Y argument should be expected");
  42. });
  43. subject.fire('something', testX, testY);
  44. t.end();
  45. });
  46. test('"on" and fire preserves the context', function(t) {
  47. var subject = eventify({});
  48. var context = {};
  49. subject.on('something', function (){
  50. t.equal(this, context, "On should be called with expected context");
  51. }, context);
  52. subject.fire('something');
  53. t.end();
  54. });
  55. test('"off" removes passed listener', function(t) {
  56. t.plan(1);
  57. var subject = eventify({});
  58. var context = {};
  59. var onFoo = function (){
  60. t.ok(false, "off() did not properly removed the handler");
  61. };
  62. var onBar = function (){
  63. t.ok(true, "off() removed bar handler");
  64. };
  65. subject.on('foo', onFoo);
  66. subject.on('bar', onBar);
  67. subject.off('foo', onFoo);
  68. subject.fire('foo');
  69. subject.fire('bar');
  70. t.end();
  71. });
  72. test('"off" removes only one from the same event name', function(t) {
  73. t.plan(1);
  74. var subject = eventify({});
  75. var context = {};
  76. var onFoo1 = function (){
  77. t.ok(false, "off() did not properly removed the handler");
  78. };
  79. var onFoo2 = function (){
  80. t.ok(true, "off() removed wrong handler");
  81. };
  82. subject.on('foo', onFoo1);
  83. subject.on('foo', onFoo2);
  84. subject.off('foo', onFoo1);
  85. subject.fire('foo');
  86. t.end();
  87. });
  88. test('"off" removes all for given event name', function(t) {
  89. t.plan(0);
  90. var subject = eventify({});
  91. var context = {};
  92. var onFoo = function (){
  93. t.ok(false, "off() did not properly removed the handler");
  94. };
  95. subject.on('foo', onFoo);
  96. subject.off('foo');
  97. subject.fire('foo');
  98. });
  99. test('"off" removes all events', function(t) {
  100. t.plan(0);
  101. var subject = eventify({});
  102. var onFoo = function (){
  103. t.ok(false, "off() did not properly removed the handler");
  104. };
  105. subject.on('foo', onFoo);
  106. subject.on('bar', onFoo);
  107. subject.off();
  108. subject.fire('foo');
  109. subject.fire('bar');
  110. });
  111. test('"off" does not harm when no such event', function(t) {
  112. t.plan(1);
  113. var subject = eventify({});
  114. var onFoo = function () {
  115. t.ok(true, "off() called just one");
  116. };
  117. subject.on('foo', onFoo);
  118. subject.off('bar', onFoo);
  119. subject.fire('foo');
  120. subject.fire('bar');
  121. });
  122. test('"off" can remove by function', function(t) {
  123. t.plan(1);
  124. var subject = eventify({});
  125. var onFooYes = function () {
  126. t.ok(true, "off() called just one");
  127. };
  128. var onFooNo = function () {
  129. t.ok(false, "off() should not be called");
  130. };
  131. subject.on('foo', onFooYes);
  132. subject.on('foo', onFooNo);
  133. subject.off('foo', onFooNo);
  134. subject.fire('foo');
  135. });
  136. test('eventify can chain', function(t) {
  137. var subject = {};
  138. var eventifiedSubject = eventify(subject);
  139. t.ok(subject === eventifiedSubject, "eventified result should be the same as subject");
  140. t.end();
  141. });