on.spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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('remove listener', done => {
  140. const spy2 = jasmine.createSpy('remove listener')
  141. vm = new Vue({
  142. el,
  143. methods: { foo: spy, bar: spy2 },
  144. data: {
  145. ok: true
  146. },
  147. render (h) {
  148. return this.ok
  149. ? h('input', { on: { click: this.foo }})
  150. : h('input', { on: { input: this.bar }})
  151. }
  152. })
  153. triggerEvent(vm.$el, 'click')
  154. expect(spy.calls.count()).toBe(1)
  155. expect(spy2.calls.count()).toBe(0)
  156. vm.ok = false
  157. waitForUpdate(() => {
  158. triggerEvent(vm.$el, 'click')
  159. expect(spy.calls.count()).toBe(1) // should no longer trigger
  160. triggerEvent(vm.$el, 'input')
  161. expect(spy2.calls.count()).toBe(1)
  162. }).then(done)
  163. })
  164. it('remove listener on child component', done => {
  165. const spy2 = jasmine.createSpy('remove listener')
  166. vm = new Vue({
  167. el,
  168. methods: { foo: spy, bar: spy2 },
  169. data: {
  170. ok: true
  171. },
  172. components: {
  173. test: {
  174. template: '<div></div>'
  175. }
  176. },
  177. render (h) {
  178. return this.ok
  179. ? h('test', { on: { foo: this.foo }})
  180. : h('test', { on: { bar: this.bar }})
  181. }
  182. })
  183. vm.$children[0].$emit('foo')
  184. expect(spy.calls.count()).toBe(1)
  185. expect(spy2.calls.count()).toBe(0)
  186. vm.ok = false
  187. waitForUpdate(() => {
  188. vm.$children[0].$emit('foo')
  189. expect(spy.calls.count()).toBe(1) // should no longer trigger
  190. vm.$children[0].$emit('bar')
  191. expect(spy2.calls.count()).toBe(1)
  192. }).then(done)
  193. })
  194. })