emitter_spec.js 1.5 KB

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