on.spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 bind to a child component', () => {
  116. Vue.component('bar', {
  117. template: '<span>Hello</span>'
  118. })
  119. vm = new Vue({
  120. el,
  121. template: '<bar @custom="foo"></bar>',
  122. methods: { foo: spy }
  123. })
  124. vm.$children[0].$emit('custom', 'foo', 'bar')
  125. expect(spy).toHaveBeenCalledWith('foo', 'bar')
  126. })
  127. it('remove listener', done => {
  128. const spy2 = jasmine.createSpy('remove listener')
  129. vm = new Vue({
  130. el,
  131. methods: { foo: spy, bar: spy2 },
  132. data: {
  133. ok: true
  134. },
  135. render (h) {
  136. return this.ok
  137. ? h('input', { on: { click: this.foo }})
  138. : h('input', { on: { input: this.bar }})
  139. }
  140. })
  141. triggerEvent(vm.$el, 'click')
  142. expect(spy.calls.count()).toBe(1)
  143. expect(spy2.calls.count()).toBe(0)
  144. vm.ok = false
  145. waitForUpdate(() => {
  146. triggerEvent(vm.$el, 'click')
  147. expect(spy.calls.count()).toBe(1) // should no longer trigger
  148. triggerEvent(vm.$el, 'input')
  149. expect(spy2.calls.count()).toBe(1)
  150. }).then(done)
  151. })
  152. it('remove listener on child component', done => {
  153. const spy2 = jasmine.createSpy('remove listener')
  154. vm = new Vue({
  155. el,
  156. methods: { foo: spy, bar: spy2 },
  157. data: {
  158. ok: true
  159. },
  160. components: {
  161. test: {
  162. template: '<div></div>'
  163. }
  164. },
  165. render (h) {
  166. return this.ok
  167. ? h('test', { on: { foo: this.foo }})
  168. : h('test', { on: { bar: this.bar }})
  169. }
  170. })
  171. vm.$children[0].$emit('foo')
  172. expect(spy.calls.count()).toBe(1)
  173. expect(spy2.calls.count()).toBe(0)
  174. vm.ok = false
  175. waitForUpdate(() => {
  176. vm.$children[0].$emit('foo')
  177. expect(spy.calls.count()).toBe(1) // should no longer trigger
  178. vm.$children[0].$emit('bar')
  179. expect(spy2.calls.count()).toBe(1)
  180. }).then(done)
  181. })
  182. })