events_spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // check $off bookkeeping
  78. child.$off('test', spy)
  79. expect(vm._eventsCount['test']).toBe(0)
  80. function noop () {}
  81. child.$on('test', noop)
  82. child2.$on('test', noop)
  83. expect(vm._eventsCount['test']).toBe(2)
  84. child.$off('test')
  85. expect(vm._eventsCount['test']).toBe(1)
  86. child.$on('test', noop)
  87. child2.$on('test', noop)
  88. expect(vm._eventsCount['test']).toBe(3)
  89. child.$off()
  90. child2.$off()
  91. expect(vm._eventsCount['test']).toBe(0)
  92. })
  93. it('$broadcast cancel', function () {
  94. var child = vm.$addChild()
  95. var child2 = child.$addChild()
  96. child.$on('test', function () {
  97. return false
  98. })
  99. child2.$on('test', spy)
  100. vm.$broadcast('test')
  101. expect(spy).not.toHaveBeenCalled()
  102. })
  103. it('$dispatch', function () {
  104. var child = vm.$addChild()
  105. var child2 = child.$addChild()
  106. child.$on('test', spy)
  107. vm.$on('test', spy)
  108. child2.$dispatch('test')
  109. expect(spy.calls.count()).toBe(2)
  110. })
  111. it('$dispatch cancel', function () {
  112. var child = vm.$addChild()
  113. var child2 = child.$addChild()
  114. child.$on('test', function () {
  115. return false
  116. })
  117. vm.$on('test', spy)
  118. child2.$dispatch('test')
  119. expect(spy).not.toHaveBeenCalled()
  120. })
  121. })