test-bug-9.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var PubSub = require('../src/pubsub'),
  3. assert = require('referee').assert,
  4. sinon = require('sinon');
  5. /**
  6. * This is a test proving that bug 9 has been fixed.
  7. * See https://github.com/mroderick/PubSubJS/issues/9
  8. */
  9. describe( 'Bug 9, publish method', function() {
  10. it('should notify all subscribers in a hierarchy', function( done ){
  11. var subscriber1 = sinon.spy(),
  12. subscriber2 = sinon.spy(),
  13. subscriber3 = sinon.spy(),
  14. clock = sinon.useFakeTimers();
  15. PubSub.subscribe( 'a.b.c', subscriber1 );
  16. PubSub.subscribe( 'a.b', subscriber2 );
  17. PubSub.subscribe( 'a', subscriber3 );
  18. PubSub.publish( 'a.b.c.d' );
  19. clock.tick(1);
  20. assert( subscriber1.calledOnce );
  21. assert( subscriber2.calledOnce );
  22. assert( subscriber3.calledOnce );
  23. done();
  24. clock.restore();
  25. });
  26. it('should notify individual subscribers, even when there are no subscribers further up', function( done ){
  27. var rootTopic = 'a.b.c',
  28. subscriber = sinon.spy(),
  29. clock = sinon.useFakeTimers();
  30. PubSub.subscribe(rootTopic, subscriber);
  31. PubSub.publish(rootTopic + '.d');
  32. clock.tick(1);
  33. assert( subscriber.calledOnce );
  34. done();
  35. clock.restore();
  36. });
  37. });