test-hierarchical-addressing.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. var PubSub = global.PubSub || require('../src/pubsub'),
  3. TestHelper = global.TestHelper || require('../test/helper'),
  4. assert = require('referee').assert,
  5. sinon = require('sinon');
  6. describe( 'Hierarchical addressing', function () {
  7. beforeEach(function(){
  8. this.clock = sinon.useFakeTimers();
  9. });
  10. afterEach(function(){
  11. this.clock.restore();
  12. });
  13. it('publish method should not call any children in a namespace', function( done ) {
  14. var messages = ['library', 'library.music'],
  15. spy = sinon.spy(),
  16. data = TestHelper.getUniqueString();
  17. PubSub.subscribe( messages[0], spy ); //This should be called
  18. PubSub.subscribe( messages[1], spy );
  19. PubSub.publish( messages[0], data );
  20. assert.equals( spy.callCount, 0 );
  21. this.clock.tick(1);
  22. assert.equals( spy.callCount, 1 );
  23. done();
  24. });
  25. it('publish method should call a parent namespace', function( done ) {
  26. // Publishing library.music should trigger parent library
  27. var messages = ['library', 'library.music'],
  28. spy = sinon.spy(),
  29. data = TestHelper.getUniqueString();
  30. PubSub.subscribe( messages[0], spy ); //This should be called
  31. PubSub.subscribe( messages[1], spy ); //This should be called
  32. PubSub.publish( messages[1], data );
  33. assert.equals( spy.callCount, 0 );
  34. this.clock.tick(1);
  35. assert.equals( spy.callCount, 2 );
  36. done();
  37. });
  38. it('publish method should call only a parent namespace', function( done ) {
  39. //Publishing library.music should only trigger parents descendants
  40. //Even if it has a child
  41. var messages = ['library', 'library.music', 'library.music.jazz'],
  42. spy = sinon.spy(),
  43. data = TestHelper.getUniqueString();
  44. PubSub.subscribe( messages[0], spy ); //This should be called
  45. PubSub.subscribe( messages[1], spy ); //This should be called
  46. PubSub.subscribe( messages[2], spy );
  47. PubSub.publish( messages[1], data );
  48. assert.equals( spy.callCount, 0 );
  49. this.clock.tick(1);
  50. assert.equals( spy.callCount, 2 );
  51. done();
  52. });
  53. it('publish method should call all parent namespaces', function( done ) {
  54. //Publishing library.music.jazz should trigger all parents
  55. var messages = ['library', 'library.music', 'library.music.jazz'],
  56. spy = sinon.spy(),
  57. data = TestHelper.getUniqueString();
  58. PubSub.subscribe( messages[0], spy ); //This should be called
  59. PubSub.subscribe( messages[1], spy ); //This should be called
  60. PubSub.subscribe( messages[2], spy ); //This should be called
  61. PubSub.publish( messages[2], data );
  62. assert.equals( spy.callCount, 0 );
  63. this.clock.tick(1);
  64. assert.equals( spy.callCount, 3 );
  65. done();
  66. });
  67. it('publish method should call only parent descendants', function( done ) {
  68. //Publishing library.music.jazz should trigger only all parents descendants
  69. //Skipping library.playlist and library.playlist.*
  70. var messages = [
  71. 'library',
  72. 'library.music',
  73. 'library.music.jazz',
  74. 'library.playlist',
  75. 'library.playlist.mine'
  76. ],
  77. spy = sinon.spy(),
  78. data = TestHelper.getUniqueString();
  79. PubSub.subscribe( messages[0], spy ); //This should be called
  80. PubSub.subscribe( messages[1], spy ); //This should be called
  81. PubSub.subscribe( messages[2], spy ); //This should be called
  82. PubSub.subscribe( messages[3], spy );
  83. PubSub.subscribe( messages[4], spy );
  84. PubSub.publish( messages[2], data );
  85. assert.equals( spy.callCount, 0 );
  86. this.clock.tick(1);
  87. assert.equals( spy.callCount, 3 );
  88. done();
  89. });
  90. it('publish method should call all parent descendants deeply', function( done ) {
  91. //Publishing library.music.jazz.soft.swing should trigger all but
  92. //library.music.playlist.jazz
  93. var messages = [
  94. 'library',
  95. 'library.music',
  96. 'library.music.jazz',
  97. 'library.music.jazz.soft',
  98. 'library.music.jazz.soft.swing',
  99. 'library.music.playlist.jazz'
  100. ],
  101. spy = sinon.spy(),
  102. data = TestHelper.getUniqueString();
  103. PubSub.subscribe( messages[0], spy ); //This should be called
  104. PubSub.subscribe( messages[1], spy ); //This should be called
  105. PubSub.subscribe( messages[2], spy ); //This should be called
  106. PubSub.subscribe( messages[3], spy ); //This should be called
  107. PubSub.subscribe( messages[4], spy ); //This should be called
  108. PubSub.subscribe( messages[5], spy ); //This should be called
  109. PubSub.subscribe( messages[6], spy );
  110. PubSub.publish( messages[4], data );
  111. assert.equals( spy.callCount, 0 );
  112. this.clock.tick(1);
  113. assert.equals( spy.callCount, 5 );
  114. done();
  115. });
  116. it('publish method should still call all parents, even when middle child is unsubscribed', function( done ) {
  117. var messages = ['library', 'library.music', 'library.music.jazz'],
  118. spy = sinon.spy(),
  119. data = TestHelper.getUniqueString(),
  120. token;
  121. PubSub.subscribe( messages[0], spy ); //This should be called
  122. PubSub.subscribe( messages[2], spy ); //This should be called
  123. token = PubSub.subscribe( messages[1], spy );
  124. PubSub.unsubscribe( token ); //Take out middle child
  125. PubSub.publish( messages[2], data );
  126. assert.equals( spy.callCount, 0 );
  127. this.clock.tick(1);
  128. assert.equals( spy.callCount, 2 );
  129. done();
  130. });
  131. it('unsubscribe method should return tokens when succesfully removing namespaced message', function(){
  132. var func = function(){ return undefined; },
  133. messages = ['playlist.music', 'playlist.music.jazz'],
  134. token1 = PubSub.subscribe( messages[0], func),
  135. token2 = PubSub.subscribe( messages[1], func ),
  136. result1 = PubSub.unsubscribe( token1 ),
  137. result2 = PubSub.unsubscribe( token2 );
  138. assert.equals( result1, token1 );
  139. assert.equals( result2, token2 );
  140. });
  141. it('unsubscribe method should unsubscribe parent without affecting orphans', function( done ){
  142. var data = TestHelper.getUniqueString(),
  143. spy = sinon.spy(),
  144. messages = ['playlist', 'playlist.music', 'playlist.music.jazz'],
  145. token;
  146. token = PubSub.subscribe( messages[0], spy ); //Gets unsubscribed
  147. PubSub.subscribe( messages[1], spy ); //This should be called
  148. PubSub.subscribe( messages[2], spy ); //This should be called
  149. PubSub.unsubscribe( token );
  150. PubSub.publish( messages[2], data );
  151. assert.equals( spy.callCount, 0 );
  152. this.clock.tick(1);
  153. assert.equals( spy.callCount, 2 );
  154. done();
  155. });
  156. });