events_spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var Vue = require('../../../../src/vue')
  2. describe('Instance Events', function () {
  3. describe('events', function () {
  4. var spy
  5. beforeEach(function () {
  6. spy = jasmine.createSpy()
  7. })
  8. it('normal events', function () {
  9. var vm = new Vue({
  10. events: {
  11. test: spy,
  12. test2: [spy, spy]
  13. }
  14. })
  15. vm.$emit('test', 123)
  16. expect(spy).toHaveBeenCalledWith(123)
  17. vm.$emit('test2')
  18. expect(spy.calls.count()).toBe(3)
  19. })
  20. it('hook events', function () {
  21. var vm = new Vue({
  22. events: {
  23. 'hook:created': spy
  24. }
  25. })
  26. expect(spy).toHaveBeenCalled()
  27. })
  28. it('method name strings', function () {
  29. var vm = new Vue({
  30. events: {
  31. 'test': 'doSomething'
  32. },
  33. methods: {
  34. doSomething: spy
  35. }
  36. })
  37. vm.$emit('test', 123)
  38. expect(spy).toHaveBeenCalledWith(123)
  39. })
  40. })
  41. describe('hooks', function () {
  42. var spy
  43. beforeEach(function () {
  44. spy = jasmine.createSpy()
  45. })
  46. it('created', function () {
  47. var ctx
  48. var vm = new Vue({
  49. created: function () {
  50. // can't assert this === vm here
  51. // because the constructor hasn't returned yet
  52. ctx = this
  53. // should have observed data
  54. expect(this._data.__ob__).toBeTruthy()
  55. spy()
  56. }
  57. })
  58. expect(ctx).toBe(vm)
  59. expect(spy).toHaveBeenCalled()
  60. })
  61. it('beforeDestroy', function () {
  62. var vm = new Vue({
  63. beforeDestroy: function () {
  64. expect(this).toBe(vm)
  65. expect(this._isDestroyed).toBe(false)
  66. spy()
  67. }
  68. })
  69. vm.$destroy()
  70. expect(spy).toHaveBeenCalled()
  71. })
  72. it('destroyed', function () {
  73. var vm = new Vue({
  74. destroyed: function () {
  75. expect(this).toBe(vm)
  76. expect(this._isDestroyed).toBe(true)
  77. expect(this._data).toBeNull()
  78. spy()
  79. }
  80. })
  81. vm.$destroy()
  82. expect(spy).toHaveBeenCalled()
  83. })
  84. if (Vue.util.inBrowser) {
  85. it('beforeCompile', function () {
  86. var vm = new Vue({
  87. template: '{{a}}',
  88. data: { a: 1 },
  89. beforeCompile: function () {
  90. expect(this).toBe(vm)
  91. expect(this.$el).toBe(el)
  92. expect(this.$el.textContent).toBe('{{a}}')
  93. spy()
  94. }
  95. })
  96. var el = document.createElement('div')
  97. vm.$mount(el)
  98. expect(spy).toHaveBeenCalled()
  99. })
  100. it('compiled', function () {
  101. var vm = new Vue({
  102. template: '{{a}}',
  103. data: { a: 1 },
  104. compiled: function () {
  105. expect(this.$el).toBe(el)
  106. expect(this.$el.textContent).toBe('1')
  107. spy()
  108. }
  109. })
  110. var el = document.createElement('div')
  111. vm.$mount(el)
  112. expect(spy).toHaveBeenCalled()
  113. })
  114. it('ready', function () {
  115. var vm = new Vue({
  116. ready: spy
  117. })
  118. expect(spy).not.toHaveBeenCalled()
  119. var el = document.createElement('div')
  120. vm.$mount(el)
  121. expect(spy).not.toHaveBeenCalled()
  122. vm.$appendTo(document.body)
  123. expect(spy).toHaveBeenCalled()
  124. vm.$remove()
  125. // try mounting on something already in dom
  126. el = document.createElement('div')
  127. document.body.appendChild(el)
  128. var spy2 = jasmine.createSpy()
  129. vm = new Vue({
  130. el: el,
  131. ready: spy2
  132. })
  133. expect(spy2).toHaveBeenCalled()
  134. vm.$remove()
  135. })
  136. describe('attached/detached', function () {
  137. it('in DOM', function () {
  138. // body...
  139. })
  140. it('create then attach', function () {
  141. // body...
  142. })
  143. it('should not fire on detached child', function () {
  144. // body...
  145. })
  146. })
  147. }
  148. })
  149. })