2
0

events_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var Vue = require('../../../../src/vue')
  2. describe('Events API', function () {
  3. var vm, spy
  4. beforeEach(function () {
  5. vm = new Vue()
  6. spy = jasmine.createSpy('emitter')
  7. })
  8. it('$on', function () {
  9. vm.$on('test', function () {
  10. // expect correct context
  11. expect(this).toBe(vm)
  12. spy.apply(this, arguments)
  13. })
  14. vm.$emit('test', 1, 2 ,3, 4)
  15. expect(spy.calls.count()).toBe(1)
  16. expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
  17. })
  18. it('$once', function () {
  19. vm.$once('test', spy)
  20. vm.$emit('test', 1, 2 ,3)
  21. vm.$emit('test', 2, 3, 4)
  22. expect(spy.calls.count()).toBe(1)
  23. expect(spy).toHaveBeenCalledWith(1, 2, 3)
  24. })
  25. it('$off', function () {
  26. vm.$on('test1', spy)
  27. vm.$on('test2', spy)
  28. vm.$off()
  29. vm.$emit('test1')
  30. vm.$emit('test2')
  31. expect(spy).not.toHaveBeenCalled()
  32. })
  33. it('$off event', function () {
  34. vm.$on('test1', spy)
  35. vm.$on('test2', spy)
  36. vm.$off('test1')
  37. vm.$off('test1') // test off something that's already off
  38. vm.$emit('test1', 1)
  39. vm.$emit('test2', 2)
  40. expect(spy.calls.count()).toBe(1)
  41. expect(spy).toHaveBeenCalledWith(2)
  42. })
  43. it('$off event + fn', function () {
  44. var spy2 = jasmine.createSpy('emitter')
  45. vm.$on('test', spy)
  46. vm.$on('test', spy2)
  47. vm.$off('test', spy)
  48. vm.$emit('test', 1, 2, 3)
  49. expect(spy).not.toHaveBeenCalled()
  50. expect(spy2.calls.count()).toBe(1)
  51. expect(spy2).toHaveBeenCalledWith(1, 2, 3)
  52. })
  53. it('$broadcast', function () {
  54. var child1 = vm.$addChild()
  55. var child2 = vm.$addChild()
  56. var child3 = child1.$addChild()
  57. child1.$on('test', spy)
  58. child2.$on('test', spy)
  59. child3.$on('test', spy)
  60. vm.$broadcast('test')
  61. expect(spy.calls.count()).toBe(3)
  62. })
  63. it('$broadcast optimization', function () {
  64. var child = vm.$addChild()
  65. var child2 = child.$addChild()
  66. // hooks should not incurr the bookkeeping cost
  67. child.$on('hook:created', function () {})
  68. expect(vm._eventsCount['hook:created']).toBeUndefined()
  69. child.$on('test', spy)
  70. expect(vm._eventsCount['test']).toBe(1)
  71. // child2's $emit & $broadcast
  72. // shouldn't get called if no child listens to the event
  73. child2.$emit = spy
  74. child2.$broadcast = spy
  75. vm.$broadcast('test')
  76. expect(spy.calls.count()).toBe(1)
  77. })
  78. it('$broadcast cancel', function () {
  79. var child = vm.$addChild()
  80. var child2 = child.$addChild()
  81. child.$on('test', function () {
  82. return false
  83. })
  84. child2.$on('test', spy)
  85. vm.$broadcast('test')
  86. expect(spy).not.toHaveBeenCalled()
  87. })
  88. it('$dispatch', function () {
  89. var child = vm.$addChild()
  90. var child2 = child.$addChild()
  91. child.$on('test', spy)
  92. vm.$on('test', spy)
  93. child2.$dispatch('test')
  94. expect(spy.calls.count()).toBe(2)
  95. })
  96. it('$dispatch cancel', function () {
  97. var child = vm.$addChild()
  98. var child2 = child.$addChild()
  99. child.$on('test', function () {
  100. return false
  101. })
  102. vm.$on('test', spy)
  103. child2.$dispatch('test')
  104. expect(spy).not.toHaveBeenCalled()
  105. })
  106. })