events_spec.js 8.2 KB

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