on.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 spy, spy2
  11. beforeEach(() => {
  12. spy = jasmine.createSpy()
  13. spy2 = jasmine.createSpy()
  14. })
  15. it('should bind event to a method', () => {
  16. const vm = new Vue({
  17. el: '#app',
  18. template: '<div v-on:click="foo"></div>',
  19. methods: { foo: spy }
  20. })
  21. const el = vm.$el
  22. trigger(el, 'click')
  23. expect(spy.calls.count()).toBe(1)
  24. const args = spy.calls.allArgs()
  25. const event = args[0] && args[0][0] || {}
  26. expect(event.type).toBe('click')
  27. })
  28. it('should bind event to a inline method', () => {
  29. const vm = new Vue({
  30. el: '#app',
  31. template: '<div v-on:click="foo(1,2,3,$event)"></div>',
  32. methods: { foo: spy }
  33. })
  34. const el = vm.$el
  35. trigger(el, 'click')
  36. expect(spy.calls.count()).toBe(1)
  37. const args = spy.calls.allArgs()
  38. const firstArgs = args[0]
  39. expect(firstArgs.length).toBe(4)
  40. expect(firstArgs[0]).toBe(1)
  41. expect(firstArgs[1]).toBe(2)
  42. expect(firstArgs[2]).toBe(3)
  43. expect(firstArgs[3].type).toBe('click')
  44. })
  45. it('should support shorthand', () => {
  46. const vm = new Vue({
  47. el: '#app',
  48. template: '<a href="#test" @click.prevent="foo"></a>',
  49. methods: { foo: spy }
  50. })
  51. const el = vm.$el
  52. trigger(el, 'click')
  53. expect(spy.calls.count()).toBe(1)
  54. })
  55. it('should support stop propagation', () => {
  56. const vm = new Vue({
  57. el: '#app',
  58. template: `
  59. <div @click.stop="foo"></div>
  60. `,
  61. methods: { foo: spy }
  62. })
  63. const hash = window.location.hash
  64. const el = vm.$el
  65. trigger(el, 'click')
  66. expect(window.location.hash).toBe(hash)
  67. })
  68. it('should support prevent default', () => {
  69. const vm = new Vue({
  70. el: '#app',
  71. template: `
  72. <div @click="bar">
  73. <div @click.stop="foo"></div>
  74. </div>
  75. `,
  76. methods: { foo: spy, bar: spy2 }
  77. })
  78. const el = vm.$el
  79. trigger(el.firstChild, 'click')
  80. expect(spy).toHaveBeenCalled()
  81. expect(spy2).not.toHaveBeenCalled()
  82. })
  83. it('should support capture', () => {
  84. const vm = new Vue({
  85. el: '#app',
  86. template: `
  87. <div @click.capture.stop="foo">
  88. <div @click="bar"></div>
  89. </div>
  90. `,
  91. methods: { foo: spy, bar: spy2 }
  92. })
  93. const el = vm.$el
  94. trigger(el.firstChild, 'click')
  95. expect(spy).toHaveBeenCalled()
  96. expect(spy2).not.toHaveBeenCalled()
  97. })
  98. xit('should bind to a child component', () => {
  99. Vue.component('bar', {
  100. template: '<span>Hello</span>'
  101. })
  102. const vm = new Vue({
  103. el: '#app',
  104. template: '<bar @click="foo"></bar>',
  105. methods: { foo: spy }
  106. })
  107. const el = vm.$el
  108. trigger(el, 'click')
  109. expect(spy.calls.count()).toBe(1)
  110. })
  111. })