test-publish.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. var PubSub = require('../src/pubsub'),
  3. TestHelper = require('../test/helper'),
  4. assert = require('referee').assert,
  5. refute = require('referee').refute,
  6. sinon = require('sinon');
  7. describe( 'publish method', function () {
  8. it('should return false if there are no subscribers', function(){
  9. var message = TestHelper.getUniqueString();
  10. assert.equals( PubSub.publish( message ), false );
  11. });
  12. it('should return true if there are subscribers to a message', function(){
  13. var message = TestHelper.getUniqueString(),
  14. func = function(){ return undefined; };
  15. PubSub.subscribe( message, func );
  16. assert( PubSub.publish( message ) );
  17. });
  18. it('should return false, when there are no longer any subscribers to a message', function(){
  19. var message = TestHelper.getUniqueString(),
  20. func = function(){ return undefined; },
  21. token = PubSub.subscribe(message, func);
  22. PubSub.unsubscribe(token);
  23. assert.equals( PubSub.publish(message), false );
  24. });
  25. it('should call all subscribers for a message exactly once', function(){
  26. var message = TestHelper.getUniqueString(),
  27. spy1 = sinon.spy(),
  28. spy2 = sinon.spy();
  29. PubSub.subscribe( message, spy1 );
  30. PubSub.subscribe( message, spy2 );
  31. PubSub.publishSync( message, 'my payload' ); // force sync here, easier to test
  32. assert( spy1.calledOnce );
  33. assert( spy2.calledOnce );
  34. });
  35. it('should call all ONLY subscribers of the published message', function(){
  36. var message1 = TestHelper.getUniqueString(),
  37. message2 = TestHelper.getUniqueString(),
  38. spy1 = sinon.spy(),
  39. spy2 = sinon.spy();
  40. PubSub.subscribe( message1, spy1 );
  41. PubSub.subscribe( message2, spy2 );
  42. PubSub.publishSync( message1, 'some payload' );
  43. // ensure the first subscriber IS called
  44. assert( spy1.called );
  45. // ensure the second subscriber IS NOT called
  46. assert.equals( spy2.callCount, 0 );
  47. });
  48. it('should call subscribers with message as first argument', function(){
  49. var message = TestHelper.getUniqueString(),
  50. spy = sinon.spy();
  51. PubSub.subscribe( message, spy );
  52. PubSub.publishSync( message, 'some payload' );
  53. assert( spy.calledWith( message ) );
  54. });
  55. it('should call subscribers with data as second argument', function(){
  56. var message = TestHelper.getUniqueString(),
  57. spy = sinon.spy(),
  58. data = TestHelper.getUniqueString();
  59. PubSub.subscribe( message, spy );
  60. PubSub.publishSync( message, data );
  61. assert( spy.calledWith( message, data ) );
  62. });
  63. it('should publish method asyncronously', function( done ){
  64. var message = TestHelper.getUniqueString(),
  65. spy = sinon.spy(),
  66. data = TestHelper.getUniqueString(),
  67. clock = sinon.useFakeTimers();
  68. PubSub.subscribe( message, spy );
  69. PubSub.publish( message, data );
  70. assert.equals( spy.callCount, 0 );
  71. clock.tick(1);
  72. assert.equals( spy.callCount, 1 );
  73. done();
  74. clock.restore();
  75. });
  76. it('publishSync method should allow syncronous publication', function(){
  77. var message = TestHelper.getUniqueString(),
  78. spy = sinon.spy(),
  79. data = TestHelper.getUniqueString();
  80. PubSub.subscribe( message, spy );
  81. PubSub.publishSync( message, data );
  82. assert.equals( spy.callCount, 1 );
  83. });
  84. it('should call all subscribers, even if there are exceptions', function( done ){
  85. var message = TestHelper.getUniqueString(),
  86. func1 = function(){
  87. throw('some error');
  88. },
  89. spy1 = sinon.spy(),
  90. spy2 = sinon.spy(),
  91. clock = sinon.useFakeTimers();
  92. PubSub.subscribe( message, func1 );
  93. PubSub.subscribe( message, spy1 );
  94. PubSub.subscribe( message, spy2 );
  95. assert.exception( function(){
  96. PubSub.publishSync( message, 'some data' );
  97. clock.tick(1);
  98. });
  99. assert( spy1.called );
  100. assert( spy2.called );
  101. done();
  102. clock.restore();
  103. });
  104. it('should fail immediately on exceptions when immediateExceptions is true', function(){
  105. var message = TestHelper.getUniqueString(),
  106. func1 = function(){
  107. throw('some error');
  108. },
  109. spy1 = sinon.spy(),
  110. spy2 = sinon.spy();
  111. PubSub.subscribe( message, func1 );
  112. PubSub.subscribe( message, spy1 );
  113. PubSub.immediateExceptions = true;
  114. assert.exception( function(){
  115. PubSub.publishSync( message, 'some data' );
  116. });
  117. refute( spy1.called );
  118. refute( spy2.called );
  119. // make sure we restore PubSub to it's original state
  120. delete PubSub.immediateExceptions;
  121. });
  122. it('should fail immediately on exceptions in namespaces when immediateExceptions is true', function(){
  123. var func1 = function(){
  124. throw('some error');
  125. },
  126. spy1 = sinon.spy();
  127. PubSub.subscribe( 'buy', func1 );
  128. PubSub.subscribe( 'buy', spy1 );
  129. PubSub.immediateExceptions = true;
  130. assert.exception( function(){
  131. PubSub.publishSync( 'buy.tomatoes', 'some data' );
  132. });
  133. refute( spy1.called );
  134. // make sure we restore PubSub to it's original state
  135. delete PubSub.immediateExceptions;
  136. });
  137. it('should call all subscribers, even when there are unsubscriptions within', function(done){
  138. var topic = TestHelper.getUniqueString(),
  139. spy1 = sinon.spy(),
  140. func1 = function func1(){
  141. PubSub.unsubscribe(func1);
  142. spy1();
  143. },
  144. spy2 = sinon.spy(),
  145. func2 = function func2(){
  146. PubSub.unsubscribe(func2);
  147. spy2();
  148. },
  149. clock = sinon.useFakeTimers();
  150. PubSub.subscribe(topic, func1);
  151. PubSub.subscribe(topic, func2);
  152. PubSub.publish(topic, 'some data');
  153. clock.tick(1);
  154. assert(spy1.called, 'expected spy1 to be called');
  155. assert(spy2.called, 'expected spy2 to be called');
  156. clock.restore();
  157. done();
  158. });
  159. });