on_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. function trigger (target, event, process) {
  4. var e = document.createEvent('HTMLEvents')
  5. e.initEvent(event, true, true)
  6. if (process) process(e)
  7. target.dispatchEvent(e)
  8. return e
  9. }
  10. if (_.inBrowser) {
  11. describe('on-', function () {
  12. var el
  13. beforeEach(function () {
  14. el = document.createElement('div')
  15. spyOn(_, 'warn')
  16. })
  17. it('methods', function () {
  18. var spy = jasmine.createSpy()
  19. var vm = new Vue({
  20. el: el,
  21. template: '<a v-on="click:test"></a>',
  22. data: {a: 1},
  23. methods: {
  24. test: spy
  25. }
  26. })
  27. var a = el.firstChild
  28. trigger(a, 'click')
  29. expect(spy.calls.count()).toBe(1)
  30. vm.$destroy()
  31. trigger(a, 'click')
  32. expect(spy.calls.count()).toBe(1)
  33. })
  34. it('inline expression', function (done) {
  35. new Vue({
  36. el: el,
  37. template: '<a v-on="click:a++">{{a}}</a>',
  38. data: {a: 1}
  39. })
  40. var a = el.firstChild
  41. trigger(a, 'click')
  42. _.nextTick(function () {
  43. expect(a.textContent).toBe('2')
  44. done()
  45. })
  46. })
  47. it('with key filter', function (done) {
  48. new Vue({
  49. el: el,
  50. template: '<a v-on="keyup:test | key \'enter\'">{{a}}</a>',
  51. data: {a: 1},
  52. methods: {
  53. test: function () {
  54. this.a++
  55. }
  56. }
  57. })
  58. var a = el.firstChild
  59. trigger(a, 'keyup', function (e) {
  60. e.keyCode = 13
  61. })
  62. _.nextTick(function () {
  63. expect(a.textContent).toBe('2')
  64. done()
  65. })
  66. })
  67. it('with new syntax key filter', function (done) {
  68. new Vue({
  69. el: el,
  70. template: '<a on-keyup:enter="test">{{a}}</a>',
  71. data: {a: 1},
  72. methods: {
  73. test: function () {
  74. this.a++
  75. }
  76. }
  77. })
  78. var a = el.firstChild
  79. trigger(a, 'keyup', function (e) {
  80. e.keyCode = 13
  81. })
  82. _.nextTick(function () {
  83. expect(a.textContent).toBe('2')
  84. done()
  85. })
  86. })
  87. it('warn non-function values', function () {
  88. new Vue({
  89. el: el,
  90. data: { test: 123 },
  91. template: '<a v-on="keyup:test"></a>'
  92. })
  93. expect(hasWarned(_, 'expects a function value')).toBe(true)
  94. })
  95. it('iframe', function () {
  96. // iframes only gets contentWindow when inserted
  97. // into the document
  98. document.body.appendChild(el)
  99. var spy = jasmine.createSpy()
  100. var vm = new Vue({
  101. el: el,
  102. template: '<iframe v-on="click:test"></iframe>',
  103. methods: {
  104. test: spy
  105. }
  106. })
  107. var iframeDoc = el.firstChild.contentDocument
  108. trigger(iframeDoc, 'click')
  109. expect(spy.calls.count()).toBe(1)
  110. vm.$destroy()
  111. trigger(iframeDoc, 'click')
  112. expect(spy.calls.count()).toBe(1)
  113. document.body.removeChild(el)
  114. })
  115. it('passing $event', function () {
  116. var test = jasmine.createSpy()
  117. new Vue({
  118. el: el,
  119. template: '<a v-on="click:test($event)"></a>',
  120. methods: {
  121. test: test
  122. }
  123. })
  124. var e = trigger(el.firstChild, 'click')
  125. expect(test).toHaveBeenCalledWith(e)
  126. })
  127. it('passing $event on a nested instance', function () {
  128. var test = jasmine.createSpy()
  129. var parent = new Vue({
  130. methods: {
  131. test: test
  132. }
  133. })
  134. parent.$addChild({
  135. el: el,
  136. template: '<a v-on="click:$parent.test($event)"></a>'
  137. })
  138. var e = trigger(el.firstChild, 'click')
  139. expect(test).toHaveBeenCalledWith(e)
  140. })
  141. })
  142. }