on_spec.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  9. if (_.inBrowser) {
  10. describe('v-on', function () {
  11. var el
  12. beforeEach(function () {
  13. el = document.createElement('div')
  14. spyOn(_, 'warn')
  15. })
  16. it('methods', function () {
  17. var spy = jasmine.createSpy()
  18. var vm = new Vue({
  19. el: el,
  20. template: '<a v-on="click:test"></a>',
  21. data: {a:1},
  22. methods: {
  23. test: spy
  24. }
  25. })
  26. var a = el.firstChild
  27. trigger(a, 'click')
  28. expect(spy.calls.count()).toBe(1)
  29. vm.$destroy()
  30. trigger(a, 'click')
  31. expect(spy.calls.count()).toBe(1)
  32. })
  33. it('inline expression', function (done) {
  34. var vm = new Vue({
  35. el: el,
  36. template: '<a v-on="click:a++">{{a}}</a>',
  37. data: {a:1}
  38. })
  39. var a = el.firstChild
  40. trigger(a, 'click')
  41. _.nextTick(function () {
  42. expect(a.textContent).toBe('2')
  43. done()
  44. })
  45. })
  46. it('with key filter', function (done) {
  47. var vm = new Vue({
  48. el: el,
  49. template: '<a v-on="keyup:test | key enter">{{a}}</a>',
  50. data: {a:1},
  51. methods: {
  52. test: function () {
  53. this.a++
  54. }
  55. }
  56. })
  57. var a = el.firstChild
  58. trigger(a, 'keyup', function (e) {
  59. e.keyCode = 13
  60. })
  61. _.nextTick(function () {
  62. expect(a.textContent).toBe('2')
  63. done()
  64. })
  65. })
  66. it('warn non-function values', function () {
  67. var vm = new Vue({
  68. el: el,
  69. data: { test: 123 },
  70. template: '<a v-on="keyup:test"></a>'
  71. })
  72. expect(_.warn).toHaveBeenCalled()
  73. })
  74. it('iframe', function () {
  75. // iframes only gets contentWindow when inserted
  76. // into the document
  77. document.body.appendChild(el)
  78. var spy = jasmine.createSpy()
  79. var vm = new Vue({
  80. el: el,
  81. template: '<iframe v-on="click:test"></iframe>',
  82. methods: {
  83. test: spy
  84. }
  85. })
  86. var iframeDoc = el.firstChild.contentDocument
  87. trigger(iframeDoc, 'click')
  88. expect(spy.calls.count()).toBe(1)
  89. vm.$destroy()
  90. trigger(iframeDoc, 'click')
  91. expect(spy.calls.count()).toBe(1)
  92. document.body.removeChild(el)
  93. })
  94. })
  95. }