on.spec.js 2.9 KB

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