events_spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-events', function () {
  5. var el
  6. beforeEach(function () {
  7. spyOn(_, 'warn')
  8. el = document.createElement('div')
  9. })
  10. it('should register events', function () {
  11. var spy = jasmine.createSpy('v-events')
  12. new Vue({
  13. el: el,
  14. template: '<div v-component="test" v-events="test:test"></div>',
  15. methods: {
  16. test: spy
  17. },
  18. components: {
  19. test: {
  20. compiled: function () {
  21. this.$emit('test')
  22. }
  23. }
  24. }
  25. })
  26. expect(spy).toHaveBeenCalled()
  27. })
  28. it('should warn when used on non-root node', function () {
  29. new Vue({
  30. el: el,
  31. template: '<div v-events="test:test"></div>'
  32. })
  33. expect(hasWarned(_,
  34. 'should only be used on a child component ' +
  35. 'from the parent template')).toBe(true)
  36. })
  37. it('should warn when used on child component root', function () {
  38. var spy = jasmine.createSpy('v-events')
  39. new Vue({
  40. el: el,
  41. template: '<div v-component="test"></div>',
  42. methods: {
  43. test: spy
  44. },
  45. components: {
  46. test: {
  47. replace: true,
  48. template: '<div v-events="test:test"></div>',
  49. compiled: function () {
  50. this.$emit('test')
  51. }
  52. }
  53. }
  54. })
  55. expect(hasWarned(_,
  56. 'should only be used on a child component ' +
  57. 'from the parent template')).toBe(true)
  58. expect(spy).not.toHaveBeenCalled()
  59. })
  60. it('should warn on non-function values', function () {
  61. var vm = new Vue({
  62. el: el,
  63. data: { test: 123 },
  64. template: '<div v-component="test" v-events="test:test"></div>',
  65. components: {
  66. test: {}
  67. }
  68. })
  69. expect(hasWarned(_, 'expects a function value')).toBe(true)
  70. })
  71. it('should accept inline statement', function (done) {
  72. var vm = new Vue({
  73. el: el,
  74. data: {a:1},
  75. template: '<div v-component="test" v-events="test:a++"></div>',
  76. components: {
  77. test: {
  78. compiled: function () {
  79. this.$emit('test')
  80. }
  81. }
  82. }
  83. })
  84. _.nextTick(function () {
  85. expect(vm.a).toBe(2)
  86. done()
  87. })
  88. })
  89. it('should be able to switch handlers if not a method', function (done) {
  90. var a = 0
  91. var b = 0
  92. var vm = new Vue({
  93. el: el,
  94. data: {
  95. handle: function () {
  96. a++
  97. }
  98. },
  99. template: '<div v-component="test" v-events="test:handle"></div>',
  100. components: {
  101. test: {
  102. compiled: function () {
  103. this.$emit('test')
  104. }
  105. }
  106. }
  107. })
  108. _.nextTick(function () {
  109. expect(a).toBe(1)
  110. expect(b).toBe(0)
  111. vm.handle = function () {
  112. b++
  113. }
  114. _.nextTick(function () {
  115. vm._children[0].$emit('test')
  116. expect(a).toBe(1)
  117. expect(b).toBe(1)
  118. done()
  119. })
  120. })
  121. })
  122. })
  123. }