on.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import Vue from 'vue'
  2. describe('Directive v-on', () => {
  3. let vm, spy, spy2, el
  4. beforeEach(() => {
  5. spy = jasmine.createSpy()
  6. spy2 = jasmine.createSpy()
  7. el = document.createElement('div')
  8. document.body.appendChild(el)
  9. })
  10. afterEach(() => {
  11. document.body.removeChild(vm.$el)
  12. })
  13. it('should bind event to a method', () => {
  14. vm = new Vue({
  15. el,
  16. template: '<div v-on:click="foo"></div>',
  17. methods: { foo: spy }
  18. })
  19. triggerEvent(vm.$el, 'click')
  20. expect(spy.calls.count()).toBe(1)
  21. const args = spy.calls.allArgs()
  22. const event = args[0] && args[0][0] || {}
  23. expect(event.type).toBe('click')
  24. })
  25. it('should bind event to a inline method', () => {
  26. vm = new Vue({
  27. el,
  28. template: '<div v-on:click="foo(1,2,3,$event)"></div>',
  29. methods: { foo: spy }
  30. })
  31. triggerEvent(vm.$el, 'click')
  32. expect(spy.calls.count()).toBe(1)
  33. const args = spy.calls.allArgs()
  34. const firstArgs = args[0]
  35. expect(firstArgs.length).toBe(4)
  36. expect(firstArgs[0]).toBe(1)
  37. expect(firstArgs[1]).toBe(2)
  38. expect(firstArgs[2]).toBe(3)
  39. expect(firstArgs[3].type).toBe('click')
  40. })
  41. it('should support shorthand', () => {
  42. vm = new Vue({
  43. el,
  44. template: '<a href="#test" @click.prevent="foo"></a>',
  45. methods: { foo: spy }
  46. })
  47. triggerEvent(vm.$el, 'click')
  48. expect(spy.calls.count()).toBe(1)
  49. })
  50. it('should support stop propagation', () => {
  51. vm = new Vue({
  52. el,
  53. template: `
  54. <div @click.stop="foo"></div>
  55. `,
  56. methods: { foo: spy }
  57. })
  58. const hash = window.location.hash
  59. triggerEvent(vm.$el, 'click')
  60. expect(window.location.hash).toBe(hash)
  61. })
  62. it('should support prevent default', () => {
  63. vm = new Vue({
  64. el,
  65. template: `
  66. <div @click="bar">
  67. <div @click.stop="foo"></div>
  68. </div>
  69. `,
  70. methods: { foo: spy, bar: spy2 }
  71. })
  72. triggerEvent(vm.$el.firstChild, 'click')
  73. expect(spy).toHaveBeenCalled()
  74. expect(spy2).not.toHaveBeenCalled()
  75. })
  76. it('should support capture', () => {
  77. const callOrder = []
  78. vm = new Vue({
  79. el,
  80. template: `
  81. <div @click.capture="foo">
  82. <div @click="bar"></div>
  83. </div>
  84. `,
  85. methods: {
  86. foo () { callOrder.push(1) },
  87. bar () { callOrder.push(2) }
  88. }
  89. })
  90. triggerEvent(vm.$el.firstChild, 'click')
  91. expect(callOrder.toString()).toBe('1,2')
  92. })
  93. it('should support keyCode', () => {
  94. vm = new Vue({
  95. el,
  96. template: `<input @keyup.enter="foo">`,
  97. methods: { foo: spy }
  98. })
  99. triggerEvent(vm.$el, 'keyup', e => {
  100. e.keyCode = 13
  101. })
  102. expect(spy).toHaveBeenCalled()
  103. })
  104. it('should support number keyCode', () => {
  105. vm = new Vue({
  106. el,
  107. template: `<input @keyup.13="foo">`,
  108. methods: { foo: spy }
  109. })
  110. triggerEvent(vm.$el, 'keyup', e => {
  111. e.keyCode = 13
  112. })
  113. expect(spy).toHaveBeenCalled()
  114. })
  115. it('should support custom keyCode', () => {
  116. Vue.config.keyCodes.test = 1
  117. vm = new Vue({
  118. el,
  119. template: `<input @keyup.test="foo">`,
  120. methods: { foo: spy }
  121. })
  122. triggerEvent(vm.$el, 'keyup', e => {
  123. e.keyCode = 1
  124. })
  125. expect(spy).toHaveBeenCalled()
  126. })
  127. it('should bind to a child component', () => {
  128. Vue.component('bar', {
  129. template: '<span>Hello</span>'
  130. })
  131. vm = new Vue({
  132. el,
  133. template: '<bar @custom="foo"></bar>',
  134. methods: { foo: spy }
  135. })
  136. vm.$children[0].$emit('custom', 'foo', 'bar')
  137. expect(spy).toHaveBeenCalledWith('foo', 'bar')
  138. })
  139. it('should be able to bind native events for a child component', () => {
  140. Vue.component('bar', {
  141. template: '<span>Hello</span>'
  142. })
  143. vm = new Vue({
  144. el,
  145. template: '<bar @click.native="foo"></bar>',
  146. methods: { foo: spy }
  147. })
  148. vm.$children[0].$emit('click')
  149. expect(spy).not.toHaveBeenCalled()
  150. triggerEvent(vm.$children[0].$el, 'click')
  151. expect(spy).toHaveBeenCalled()
  152. })
  153. it('remove listener', done => {
  154. const spy2 = jasmine.createSpy('remove listener')
  155. vm = new Vue({
  156. el,
  157. methods: { foo: spy, bar: spy2 },
  158. data: {
  159. ok: true
  160. },
  161. render (h) {
  162. return this.ok
  163. ? h('input', { on: { click: this.foo }})
  164. : h('input', { on: { input: this.bar }})
  165. }
  166. })
  167. triggerEvent(vm.$el, 'click')
  168. expect(spy.calls.count()).toBe(1)
  169. expect(spy2.calls.count()).toBe(0)
  170. vm.ok = false
  171. waitForUpdate(() => {
  172. triggerEvent(vm.$el, 'click')
  173. expect(spy.calls.count()).toBe(1) // should no longer trigger
  174. triggerEvent(vm.$el, 'input')
  175. expect(spy2.calls.count()).toBe(1)
  176. }).then(done)
  177. })
  178. it('remove listener on child component', done => {
  179. const spy2 = jasmine.createSpy('remove listener')
  180. vm = new Vue({
  181. el,
  182. methods: { foo: spy, bar: spy2 },
  183. data: {
  184. ok: true
  185. },
  186. components: {
  187. test: {
  188. template: '<div></div>'
  189. }
  190. },
  191. render (h) {
  192. return this.ok
  193. ? h('test', { on: { foo: this.foo }})
  194. : h('test', { on: { bar: this.bar }})
  195. }
  196. })
  197. vm.$children[0].$emit('foo')
  198. expect(spy.calls.count()).toBe(1)
  199. expect(spy2.calls.count()).toBe(0)
  200. vm.ok = false
  201. waitForUpdate(() => {
  202. vm.$children[0].$emit('foo')
  203. expect(spy.calls.count()).toBe(1) // should no longer trigger
  204. vm.$children[0].$emit('bar')
  205. expect(spy2.calls.count()).toBe(1)
  206. }).then(done)
  207. })
  208. it('warn missing handlers', () => {
  209. vm = new Vue({
  210. el,
  211. data: { none: null },
  212. template: `<div @click="none"></div>`
  213. })
  214. expect(`Handler for event "click" is undefined`).toHaveBeenWarned()
  215. expect(() => {
  216. triggerEvent(vm.$el, 'click')
  217. }).not.toThrow()
  218. })
  219. })