2
0

events_spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 vm = new Vue({
  197. el: document.createElement('div'),
  198. template: '<comp v-on:hook:created="onCreated" @ready="onReady" @ok="a++"></comp>',
  199. data: {
  200. a: 0
  201. },
  202. methods: {
  203. onCreated: spy,
  204. onReady: spy
  205. },
  206. components: {
  207. comp: {
  208. compiled: function () {
  209. this.$emit('ready', 123)
  210. this.$emit('ok')
  211. }
  212. }
  213. }
  214. })
  215. expect(spy.calls.count()).toBe(2)
  216. expect(spy).toHaveBeenCalledWith(123)
  217. expect(vm.a).toBe(1)
  218. })
  219. it('warn missing handler for child component v-on', function () {
  220. new Vue({
  221. el: document.createElement('div'),
  222. template: '<comp @test="onThat"></comp>',
  223. components: {
  224. comp: {}
  225. }
  226. })
  227. expect('v-on:test="onThat" expects a function value').toHaveBeenWarned()
  228. })
  229. it('passing $arguments', function () {
  230. new Vue({
  231. el: document.createElement('div'),
  232. template: '<comp @ready="onReady($arguments[1])"></comp>',
  233. methods: {
  234. onReady: spy
  235. },
  236. components: {
  237. comp: {
  238. compiled: function () {
  239. this.$emit('ready', 123, 1234)
  240. }
  241. }
  242. }
  243. })
  244. expect(spy).toHaveBeenCalledWith(1234)
  245. })
  246. describe('attached/detached', function () {
  247. it('in DOM', function () {
  248. var el = document.createElement('div')
  249. var childEl = document.createElement('div')
  250. el.appendChild(childEl)
  251. document.body.appendChild(el)
  252. var parentVm = new Vue({
  253. el: el,
  254. attached: spy,
  255. detached: spy2
  256. })
  257. var childVm = new Vue({
  258. parent: parentVm,
  259. el: childEl,
  260. attached: spy,
  261. detached: spy2
  262. })
  263. expect(spy.calls.count()).toBe(2)
  264. parentVm.$remove()
  265. expect(spy2.calls.count()).toBe(2)
  266. // child should be already detached
  267. // so the hook should not fire again
  268. childVm.$remove()
  269. expect(spy2.calls.count()).toBe(2)
  270. })
  271. it('create then attach', function () {
  272. var el = document.createElement('div')
  273. var childEl = document.createElement('div')
  274. el.appendChild(childEl)
  275. var parentVm = new Vue({
  276. el: el,
  277. attached: spy,
  278. detached: spy2
  279. })
  280. var childVm = new Vue({
  281. parent: parentVm,
  282. el: childEl,
  283. attached: spy,
  284. detached: spy2
  285. })
  286. parentVm.$appendTo(document.body)
  287. expect(spy.calls.count()).toBe(2)
  288. // detach child first
  289. childVm.$remove()
  290. expect(spy2.calls.count()).toBe(1)
  291. // should only fire parent detach
  292. parentVm.$remove()
  293. expect(spy2.calls.count()).toBe(2)
  294. })
  295. it('should not fire on detached child', function () {
  296. var el = document.createElement('div')
  297. var childEl = document.createElement('div')
  298. var parentVm = new Vue({
  299. el: el,
  300. attached: spy
  301. })
  302. var childVm = new Vue({
  303. parent: parentVm,
  304. el: childEl,
  305. attached: spy
  306. })
  307. parentVm.$appendTo(document.body)
  308. expect(spy.calls.count()).toBe(1)
  309. childVm.$appendTo(el)
  310. expect(spy.calls.count()).toBe(2)
  311. })
  312. })
  313. })
  314. })