on_spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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('v-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('shorthand', function () {
  35. var spy = jasmine.createSpy()
  36. var vm = new Vue({
  37. el: el,
  38. template: '<a @click="test"></a>',
  39. data: {a: 1},
  40. methods: {
  41. test: spy
  42. }
  43. })
  44. var a = el.firstChild
  45. trigger(a, 'click')
  46. expect(spy.calls.count()).toBe(1)
  47. vm.$destroy()
  48. trigger(a, 'click')
  49. expect(spy.calls.count()).toBe(1)
  50. })
  51. it('inline expression', function (done) {
  52. new Vue({
  53. el: el,
  54. template: '<a v-on:click="a++">{{a}}</a>',
  55. data: {a: 1}
  56. })
  57. var a = el.firstChild
  58. trigger(a, 'click')
  59. _.nextTick(function () {
  60. expect(a.textContent).toBe('2')
  61. done()
  62. })
  63. })
  64. it('with key filter', function (done) {
  65. new Vue({
  66. el: el,
  67. template: '<a v-on:keyup.enter="test">{{a}}</a>',
  68. data: {a: 1},
  69. methods: {
  70. test: function () {
  71. this.a++
  72. }
  73. }
  74. })
  75. var a = el.firstChild
  76. trigger(a, 'keyup', function (e) {
  77. e.keyCode = 13
  78. })
  79. _.nextTick(function () {
  80. expect(a.textContent).toBe('2')
  81. done()
  82. })
  83. })
  84. it('with key filter (keycode)', function (done) {
  85. new Vue({
  86. el: el,
  87. template: '<a v-on:keyup.13="test">{{a}}</a>',
  88. data: {a: 1},
  89. methods: {
  90. test: function () {
  91. this.a++
  92. }
  93. }
  94. })
  95. var a = el.firstChild
  96. trigger(a, 'keyup', function (e) {
  97. e.keyCode = 13
  98. })
  99. _.nextTick(function () {
  100. expect(a.textContent).toBe('2')
  101. done()
  102. })
  103. })
  104. it('warn nv-on:function values', function () {
  105. new Vue({
  106. el: el,
  107. data: { test: 123 },
  108. template: '<a v-on:keyup="test"></a>'
  109. })
  110. expect(hasWarned(_, 'expects a function value')).toBe(true)
  111. })
  112. it('iframe', function () {
  113. // iframes only gets contentWindow when inserted
  114. // into the document
  115. document.body.appendChild(el)
  116. var spy = jasmine.createSpy()
  117. var vm = new Vue({
  118. el: el,
  119. template: '<iframe v-on:click="test"></iframe>',
  120. methods: {
  121. test: spy
  122. }
  123. })
  124. var iframeDoc = el.firstChild.contentDocument
  125. trigger(iframeDoc, 'click')
  126. expect(spy.calls.count()).toBe(1)
  127. vm.$destroy()
  128. trigger(iframeDoc, 'click')
  129. expect(spy.calls.count()).toBe(1)
  130. document.body.removeChild(el)
  131. })
  132. it('passing $event', function () {
  133. var test = jasmine.createSpy()
  134. new Vue({
  135. el: el,
  136. template: '<a v-on:click="test($event)"></a>',
  137. methods: {
  138. test: test
  139. }
  140. })
  141. var e = trigger(el.firstChild, 'click')
  142. expect(test).toHaveBeenCalledWith(e)
  143. })
  144. it('passing $event on a nested instance', function () {
  145. var test = jasmine.createSpy()
  146. var parent = new Vue({
  147. methods: {
  148. test: test
  149. }
  150. })
  151. parent.$addChild({
  152. el: el,
  153. template: '<a v-on:click="$parent.test($event)"></a>'
  154. })
  155. var e = trigger(el.firstChild, 'click')
  156. expect(test).toHaveBeenCalledWith(e)
  157. })
  158. })
  159. }