test-unsubscribe.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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( 'unsubscribe method', function() {
  8. it('should return token when succesful', function(){
  9. var func = function(){ return undefined; },
  10. message = TestHelper.getUniqueString(),
  11. token = PubSub.subscribe( message, func),
  12. result = PubSub.unsubscribe( token );
  13. assert.equals( result, token );
  14. });
  15. it('should return false when unsuccesful', function(){
  16. var unknownToken = 'my unknown token',
  17. result = PubSub.unsubscribe( unknownToken ),
  18. func = function(){ return undefined; },
  19. message = TestHelper.getUniqueString(),
  20. token = PubSub.subscribe( message, func );
  21. // first, let's try a completely unknown token
  22. assert.equals( result, false );
  23. // now let's try unsubscribing the same method twice
  24. PubSub.unsubscribe( token );
  25. assert.equals( PubSub.unsubscribe( token ), false );
  26. });
  27. it('with function argument should return true when succesful', function(){
  28. var func = function(){ return undefined; },
  29. message = TestHelper.getUniqueString(),
  30. result;
  31. PubSub.subscribe( message, func);
  32. result = PubSub.unsubscribe( func );
  33. assert.equals( result, true );
  34. });
  35. it('with function argument should return false when unsuccesful', function(){
  36. var func = function(){ return undefined; },
  37. message = TestHelper.getUniqueString(),
  38. unknownToken = 'my unknown token',
  39. result = PubSub.unsubscribe( unknownToken );
  40. // first, let's try a completely unknown token
  41. assert.equals( result, false );
  42. // now let's try unsubscribing the same method twice
  43. PubSub.subscribe( message, func );
  44. PubSub.subscribe( message, func );
  45. PubSub.subscribe( message, func );
  46. // unsubscribe once, this should remove all subscriptions for message
  47. PubSub.unsubscribe( func );
  48. // unsubscribe again
  49. assert.equals( PubSub.unsubscribe( func ), false );
  50. });
  51. it('with topic argument, must clear all exactly matched subscriptions', function(){
  52. var topic = TestHelper.getUniqueString(),
  53. spy1 = sinon.spy(),
  54. spy2 = sinon.spy();
  55. PubSub.subscribe(topic, spy1);
  56. PubSub.subscribe(topic, spy2);
  57. PubSub.unsubscribe(topic);
  58. PubSub.publishSync(topic, TestHelper.getUniqueString());
  59. refute(spy1.called);
  60. refute(spy2.called);
  61. });
  62. it('with topic argument, must only clear matched subscriptions', function(){
  63. var topic1 = TestHelper.getUniqueString(),
  64. topic2 = TestHelper.getUniqueString(),
  65. spy1 = sinon.spy(),
  66. spy2 = sinon.spy();
  67. PubSub.subscribe(topic1, spy1);
  68. PubSub.subscribe(topic2, spy2);
  69. PubSub.unsubscribe(topic1);
  70. PubSub.publishSync(topic1, TestHelper.getUniqueString());
  71. PubSub.publishSync(topic2, TestHelper.getUniqueString());
  72. refute(spy1.called);
  73. assert(spy2.called);
  74. });
  75. it('with topic argument, must clear all matched hierarchical subscriptions', function(){
  76. var topic = TestHelper.getUniqueString(),
  77. topicA = topic + '.a',
  78. topicB = topic + '.a.b',
  79. topicC = topic + '.a.b.c',
  80. spyA = sinon.spy(),
  81. spyB = sinon.spy(),
  82. spyC = sinon.spy();
  83. PubSub.subscribe(topicA, spyA);
  84. PubSub.subscribe(topicB, spyB);
  85. PubSub.subscribe(topicC, spyC);
  86. PubSub.unsubscribe(topicB);
  87. PubSub.publishSync(topicC, TestHelper.getUniqueString());
  88. assert(spyA.called);
  89. refute(spyB.called);
  90. refute(spyC.called);
  91. });
  92. it('with parent topic argument, must clear all child subscriptions', function() {
  93. var topic = TestHelper.getUniqueString(),
  94. topicA = topic + '.a',
  95. topicB = topic + '.a.b',
  96. topicC = topic + '.a.b.c',
  97. spyB = sinon.spy(),
  98. spyC = sinon.spy();
  99. // subscribe only to children:
  100. PubSub.subscribe(topicB, spyB);
  101. PubSub.subscribe(topicC, spyC);
  102. // but unsubscribe from a parent:
  103. PubSub.unsubscribe(topicA);
  104. PubSub.publishSync(topicB, TestHelper.getUniqueString());
  105. PubSub.publishSync(topicC, TestHelper.getUniqueString());
  106. refute(spyB.called);
  107. refute(spyC.called);
  108. });
  109. it('must not throw exception when unsubscribing as part of publishing', function(){
  110. refute.exception(function(){
  111. var topic = TestHelper.getUniqueString(),
  112. sub1 = function(){
  113. PubSub.unsubscribe(sub1);
  114. },
  115. sub2 = function(){ return undefined; };
  116. PubSub.subscribe( topic, sub1 );
  117. PubSub.subscribe( topic, sub2 );
  118. PubSub.publishSync( topic, 'hello world!' );
  119. });
  120. });
  121. });