emitter_spec.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, 4)
  11. expect(spy.calls.count()).toBe(1)
  12. expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
  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, undefined)
  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, 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, undefined)
  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. it('apply emit cancel', function () {
  57. expect(e._cancelled).toBe(false)
  58. e.on('test', function () {
  59. return false
  60. })
  61. e.applyEmit('test')
  62. expect(e._cancelled).toBe(true)
  63. e.applyEmit('other')
  64. expect(e._cancelled).toBe(false)
  65. })
  66. })