test-issue-54.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var PubSub = require('../src/pubsub'),
  3. TestHelper = require('../test/helper'),
  4. assert = require('referee').assert,
  5. sinon = require('sinon');
  6. /**
  7. * This is a test proving that bug 54 has been fixed.
  8. * See https://github.com/mroderick/PubSubJS/issues/54
  9. */
  10. describe( 'Issue 54, publish method', function () {
  11. it('should notify all subscribers, even when one is unsubscribed', function( done ){
  12. var topic = TestHelper.getUniqueString(),
  13. token1,
  14. token1Unsubscribed = false,
  15. subscriber1 = function(){
  16. PubSub.unsubscribe(token1);
  17. token1Unsubscribed = true;
  18. },
  19. spy1 = sinon.spy(subscriber1),
  20. spy2 = sinon.spy(),
  21. spy3 = sinon.spy(),
  22. clock = sinon.useFakeTimers();
  23. token1 = PubSub.subscribe( topic, spy1 );
  24. PubSub.subscribe( topic, spy2 );
  25. PubSub.subscribe( topic, spy3 );
  26. PubSub.publish( topic );
  27. clock.tick(1);
  28. assert( token1Unsubscribed === true );
  29. assert( spy1.calledOnce );
  30. assert( spy2.calledOnce );
  31. assert( spy3.calledOnce );
  32. done();
  33. clock.restore();
  34. });
  35. });