events_spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. describe('Instance Events', function () {
  4. var spy, spy2
  5. beforeEach(function () {
  6. spy = jasmine.createSpy()
  7. spy2 = jasmine.createSpy()
  8. spyOn(_, 'warn')
  9. })
  10. describe('option events', function () {
  11. it('normal events', function () {
  12. var vm = new Vue({
  13. events: {
  14. test: spy,
  15. test2: [spy, spy]
  16. }
  17. })
  18. vm.$emit('test', 123)
  19. expect(spy).toHaveBeenCalledWith(123)
  20. vm.$emit('test2')
  21. expect(spy.calls.count()).toBe(3)
  22. })
  23. it('hook events', function () {
  24. new Vue({
  25. events: {
  26. 'hook:created': spy
  27. }
  28. })
  29. expect(spy).toHaveBeenCalled()
  30. })
  31. it('method name strings', function () {
  32. var vm = new Vue({
  33. events: {
  34. test: 'doSomething',
  35. test2: 'doSomethingElse'
  36. },
  37. methods: {
  38. doSomething: spy
  39. }
  40. })
  41. vm.$emit('test', 123)
  42. expect(spy).toHaveBeenCalledWith(123)
  43. vm.$emit('test2')
  44. expect(hasWarned(_, 'Unknown method')).toBe(true)
  45. })
  46. })
  47. describe('option watchers', function () {
  48. it('normal', function (done) {
  49. var spyA = jasmine.createSpy()
  50. var spyB = jasmine.createSpy()
  51. var count = 0
  52. var a = {
  53. b: { c: 1 }
  54. }
  55. var vm = new Vue({
  56. watch: {
  57. 'a.b.c': spyA,
  58. 'b + c': spyB,
  59. a: {
  60. deep: true,
  61. immediate: true,
  62. handler: 'test'
  63. }
  64. },
  65. data: {
  66. a: a,
  67. b: 1,
  68. c: 2
  69. },
  70. methods: {
  71. test: function (val) {
  72. count++
  73. expect(val).toBe(a)
  74. }
  75. }
  76. })
  77. vm.a.b.c = 2
  78. vm.b = 3
  79. vm.c = 4
  80. expect(count).toBe(1)
  81. _.nextTick(function () {
  82. expect(spyA).toHaveBeenCalledWith(2, 1)
  83. expect(spyB).toHaveBeenCalledWith(7, 3)
  84. expect(count).toBe(2)
  85. done()
  86. })
  87. })
  88. it('method name strings', function (done) {
  89. var spy = jasmine.createSpy()
  90. var vm = new Vue({
  91. watch: {
  92. 'a': 'test'
  93. },
  94. data: {
  95. a: 1
  96. },
  97. methods: {
  98. test: spy
  99. }
  100. })
  101. vm.a = 2
  102. _.nextTick(function () {
  103. expect(spy).toHaveBeenCalledWith(2, 1)
  104. done()
  105. })
  106. })
  107. })
  108. describe('hooks', function () {
  109. it('created', function () {
  110. var ctx
  111. var vm = new Vue({
  112. created: function () {
  113. // can't assert this === vm here
  114. // because the constructor hasn't returned yet
  115. ctx = this
  116. // should have observed data
  117. expect(this._data.__ob__).toBeTruthy()
  118. spy()
  119. }
  120. })
  121. expect(ctx).toBe(vm)
  122. expect(spy).toHaveBeenCalled()
  123. })
  124. it('beforeDestroy', function () {
  125. var vm = new Vue({
  126. beforeDestroy: function () {
  127. expect(this).toBe(vm)
  128. expect(this._isDestroyed).toBe(false)
  129. spy()
  130. }
  131. })
  132. vm.$destroy()
  133. expect(spy).toHaveBeenCalled()
  134. })
  135. it('destroyed', function () {
  136. var vm = new Vue({
  137. destroyed: function () {
  138. expect(this).toBe(vm)
  139. expect(this._isDestroyed).toBe(true)
  140. spy()
  141. }
  142. })
  143. vm.$destroy()
  144. expect(spy).toHaveBeenCalled()
  145. })
  146. it('beforeCompile', function () {
  147. var vm = new Vue({
  148. template: '{{a}}',
  149. data: { a: 1 },
  150. beforeCompile: function () {
  151. expect(this).toBe(vm)
  152. expect(this.$el).toBe(el)
  153. expect(this.$el.textContent).toBe('{{a}}')
  154. spy()
  155. }
  156. })
  157. var el = document.createElement('div')
  158. vm.$mount(el)
  159. expect(spy).toHaveBeenCalled()
  160. })
  161. it('compiled', function () {
  162. var vm = new Vue({
  163. template: '{{a}}',
  164. data: { a: 1 },
  165. compiled: function () {
  166. expect(this.$el).toBe(el)
  167. expect(this.$el.textContent).toBe('1')
  168. spy()
  169. }
  170. })
  171. var el = document.createElement('div')
  172. vm.$mount(el)
  173. expect(spy).toHaveBeenCalled()
  174. })
  175. it('ready', function () {
  176. var vm = new Vue({
  177. ready: spy
  178. })
  179. expect(spy).not.toHaveBeenCalled()
  180. var el = document.createElement('div')
  181. vm.$mount(el)
  182. expect(spy).not.toHaveBeenCalled()
  183. vm.$appendTo(document.body)
  184. expect(spy).toHaveBeenCalled()
  185. vm.$remove()
  186. // try mounting on something already in dom
  187. el = document.createElement('div')
  188. document.body.appendChild(el)
  189. vm = new Vue({
  190. el: el,
  191. ready: spy2
  192. })
  193. expect(spy2).toHaveBeenCalled()
  194. vm.$remove()
  195. })
  196. it('compile v-on on child component', function () {
  197. var vm = new Vue({
  198. el: document.createElement('div'),
  199. template: '<comp v-on:hook:created="onCreated" @ready="onReady" @ok="a++"></comp>',
  200. data: {
  201. a: 0
  202. },
  203. methods: {
  204. onCreated: spy,
  205. onReady: spy
  206. },
  207. components: {
  208. comp: {
  209. compiled: function () {
  210. this.$emit('ready', 123)
  211. this.$emit('ok')
  212. }
  213. }
  214. }
  215. })
  216. expect(spy.calls.count()).toBe(2)
  217. expect(spy).toHaveBeenCalledWith(123)
  218. expect(vm.a).toBe(1)
  219. })
  220. describe('attached/detached', function () {
  221. it('in DOM', function () {
  222. var el = document.createElement('div')
  223. var childEl = document.createElement('div')
  224. el.appendChild(childEl)
  225. document.body.appendChild(el)
  226. var parentVm = new Vue({
  227. el: el,
  228. attached: spy,
  229. detached: spy2
  230. })
  231. var childVm = new Vue({
  232. parent: parentVm,
  233. el: childEl,
  234. attached: spy,
  235. detached: spy2
  236. })
  237. expect(spy.calls.count()).toBe(2)
  238. parentVm.$remove()
  239. expect(spy2.calls.count()).toBe(2)
  240. // child should be already detached
  241. // so the hook should not fire again
  242. childVm.$remove()
  243. expect(spy2.calls.count()).toBe(2)
  244. })
  245. it('create then attach', function () {
  246. var el = document.createElement('div')
  247. var childEl = document.createElement('div')
  248. el.appendChild(childEl)
  249. var parentVm = new Vue({
  250. el: el,
  251. attached: spy,
  252. detached: spy2
  253. })
  254. var childVm = new Vue({
  255. parent: parentVm,
  256. el: childEl,
  257. attached: spy,
  258. detached: spy2
  259. })
  260. parentVm.$appendTo(document.body)
  261. expect(spy.calls.count()).toBe(2)
  262. // detach child first
  263. childVm.$remove()
  264. expect(spy2.calls.count()).toBe(1)
  265. // should only fire parent detach
  266. parentVm.$remove()
  267. expect(spy2.calls.count()).toBe(2)
  268. })
  269. it('should not fire on detached child', function () {
  270. var el = document.createElement('div')
  271. var childEl = document.createElement('div')
  272. var parentVm = new Vue({
  273. el: el,
  274. attached: spy
  275. })
  276. var childVm = new Vue({
  277. parent: parentVm,
  278. el: childEl,
  279. attached: spy
  280. })
  281. parentVm.$appendTo(document.body)
  282. expect(spy.calls.count()).toBe(1)
  283. childVm.$appendTo(el)
  284. expect(spy.calls.count()).toBe(2)
  285. })
  286. })
  287. })
  288. })