on.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import Vue from 'vue'
  2. function trigger (target, event, process) {
  3. var e = document.createEvent('HTMLEvents')
  4. e.initEvent(event, true, true)
  5. if (process) process(e)
  6. target.dispatchEvent(e)
  7. return e
  8. }
  9. describe('Directive v-on', () => {
  10. let vm, spy, spy2
  11. beforeEach(() => {
  12. spy = jasmine.createSpy()
  13. spy2 = jasmine.createSpy()
  14. const el = document.createElement('div')
  15. el.id = 'app'
  16. document.body.appendChild(el)
  17. })
  18. afterEach(() => {
  19. document.body.removeChild(vm.$el)
  20. })
  21. it('should bind event to a method', () => {
  22. vm = new Vue({
  23. el: '#app',
  24. template: '<div v-on:click="foo"></div>',
  25. methods: { foo: spy }
  26. })
  27. const el = vm.$el
  28. trigger(el, 'click')
  29. expect(spy.calls.count()).toBe(1)
  30. const args = spy.calls.allArgs()
  31. const event = args[0] && args[0][0] || {}
  32. expect(event.type).toBe('click')
  33. })
  34. it('should bind event to a inline method', () => {
  35. vm = new Vue({
  36. el: '#app',
  37. template: '<div v-on:click="foo(1,2,3,$event)"></div>',
  38. methods: { foo: spy }
  39. })
  40. const el = vm.$el
  41. trigger(el, 'click')
  42. expect(spy.calls.count()).toBe(1)
  43. const args = spy.calls.allArgs()
  44. const firstArgs = args[0]
  45. expect(firstArgs.length).toBe(4)
  46. expect(firstArgs[0]).toBe(1)
  47. expect(firstArgs[1]).toBe(2)
  48. expect(firstArgs[2]).toBe(3)
  49. expect(firstArgs[3].type).toBe('click')
  50. })
  51. it('should support shorthand', () => {
  52. vm = new Vue({
  53. el: '#app',
  54. template: '<a href="#test" @click.prevent="foo"></a>',
  55. methods: { foo: spy }
  56. })
  57. const el = vm.$el
  58. trigger(el, 'click')
  59. expect(spy.calls.count()).toBe(1)
  60. })
  61. it('should support stop propagation', () => {
  62. vm = new Vue({
  63. el: '#app',
  64. template: `
  65. <div @click.stop="foo"></div>
  66. `,
  67. methods: { foo: spy }
  68. })
  69. const hash = window.location.hash
  70. const el = vm.$el
  71. trigger(el, 'click')
  72. expect(window.location.hash).toBe(hash)
  73. })
  74. it('should support prevent default', () => {
  75. vm = new Vue({
  76. el: '#app',
  77. template: `
  78. <div @click="bar">
  79. <div @click.stop="foo"></div>
  80. </div>
  81. `,
  82. methods: { foo: spy, bar: spy2 }
  83. })
  84. const el = vm.$el
  85. trigger(el.firstChild, 'click')
  86. expect(spy).toHaveBeenCalled()
  87. expect(spy2).not.toHaveBeenCalled()
  88. })
  89. it('should support capture', () => {
  90. const callOrder = []
  91. vm = new Vue({
  92. el: '#app',
  93. template: `
  94. <div @click.capture="foo">
  95. <div @click="bar"></div>
  96. </div>
  97. `,
  98. methods: {
  99. foo () { callOrder.push(1) },
  100. bar () { callOrder.push(2) }
  101. }
  102. })
  103. const el = vm.$el
  104. trigger(el.firstChild, 'click')
  105. expect(callOrder.toString()).toBe('1,2')
  106. })
  107. it('should bind to a child component', () => {
  108. Vue.component('bar', {
  109. template: '<span>Hello</span>'
  110. })
  111. vm = new Vue({
  112. el: '#app',
  113. template: '<bar @custom="foo"></bar>',
  114. methods: { foo: spy }
  115. })
  116. vm.$children[0].$emit('custom', 'foo', 'bar')
  117. expect(spy).toHaveBeenCalledWith('foo', 'bar')
  118. })
  119. })