emitter_spec.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var Emitter = require('../../../src/emitter')
  2. describe('Emitter', function () {
  3. var e, spy
  4. beforeEach(function () {
  5. e = new Emitter()
  6. spy = jasmine.createSpy('emitter')
  7. })
  8. it('on', function () {
  9. e.on('test', spy)
  10. e.emit('test', 1, 2 ,3)
  11. expect(spy.calls.count()).toBe(1)
  12. expect(spy).toHaveBeenCalledWith(1, 2, 3)
  13. })
  14. it('once', function () {
  15. e.once('test', spy)
  16. e.emit('test', 1, 2 ,3)
  17. e.emit('test', 2, 3, 4)
  18. expect(spy.calls.count()).toBe(1)
  19. expect(spy).toHaveBeenCalledWith(1, 2, 3)
  20. })
  21. it('off', function () {
  22. e.on('test1', spy)
  23. e.on('test2', spy)
  24. e.off()
  25. e.emit('test1')
  26. e.emit('test2')
  27. expect(spy.calls.count()).toBe(0)
  28. })
  29. it('off event', function () {
  30. e.on('test1', spy)
  31. e.on('test2', spy)
  32. e.off('test1')
  33. e.off('test1') // test off something that's already off
  34. e.emit('test1', 1)
  35. e.emit('test2', 2)
  36. expect(spy.calls.count()).toBe(1)
  37. expect(spy).toHaveBeenCalledWith(2, undefined, undefined)
  38. })
  39. it('off event + fn', function () {
  40. var spy2 = jasmine.createSpy('emitter')
  41. e.on('test', spy)
  42. e.on('test', spy2)
  43. e.off('test', spy)
  44. e.emit('test', 1, 2, 3)
  45. expect(spy.calls.count()).toBe(0)
  46. expect(spy2.calls.count()).toBe(1)
  47. expect(spy2).toHaveBeenCalledWith(1, 2, 3)
  48. })
  49. it('apply emit', function () {
  50. e.on('test', spy)
  51. e.applyEmit('test', 1)
  52. e.applyEmit('test', 1, 2, 3, 4, 5)
  53. expect(spy).toHaveBeenCalledWith(1)
  54. expect(spy).toHaveBeenCalledWith(1, 2, 3, 4, 5)
  55. })
  56. })