events.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. compileAndStringify,
  3. prepareRuntime,
  4. resetRuntime,
  5. createInstance
  6. } from '../helpers/index'
  7. describe('generate events', () => {
  8. let runtime
  9. beforeAll(() => {
  10. runtime = prepareRuntime()
  11. })
  12. afterAll(() => {
  13. resetRuntime()
  14. runtime = null
  15. })
  16. it('should be bound and fired for native component', (done) => {
  17. const { render, staticRenderFns } = compileAndStringify(`
  18. <div>
  19. <text @click="foo">Hello {{x}}</text>
  20. </div>
  21. `)
  22. const instance = createInstance(runtime, `
  23. new Vue({
  24. data: {
  25. x: 'World'
  26. },
  27. render: ${render},
  28. staticRenderFns: ${staticRenderFns},
  29. methods: {
  30. foo: function () {
  31. this.x = 'Weex'
  32. }
  33. },
  34. el: 'body'
  35. })
  36. `)
  37. expect(instance.getRealRoot()).toEqual({
  38. type: 'div',
  39. children: [
  40. {
  41. type: 'text',
  42. event: ['click'],
  43. attr: { value: 'Hello World' }
  44. }
  45. ]
  46. })
  47. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  48. setTimeout(() => {
  49. expect(instance.getRealRoot()).toEqual({
  50. type: 'div',
  51. children: [
  52. {
  53. type: 'text',
  54. event: ['click'],
  55. attr: { value: 'Hello Weex' }
  56. }
  57. ]
  58. })
  59. done()
  60. })
  61. })
  62. it('should be bound and fired by custom component', (done) => {
  63. const subTemplate = compileAndStringify(`<text>Hello {{x}}</text>`)
  64. const { render, staticRenderFns } = compileAndStringify(`
  65. <div>
  66. <text>Hello {{x}}</text>
  67. <sub @click="foo" @click.native="bar"></sub>
  68. </div>
  69. `)
  70. const instance = createInstance(runtime, `
  71. new Vue({
  72. data: {
  73. x: 'World'
  74. },
  75. render: ${render},
  76. staticRenderFns: ${staticRenderFns},
  77. components: {
  78. sub: {
  79. data: function () {
  80. return {
  81. x: 'Sub'
  82. }
  83. },
  84. render: ${subTemplate.render},
  85. staticRenderFns: ${subTemplate.staticRenderFns},
  86. created: function () {
  87. this.$emit('click')
  88. }
  89. }
  90. },
  91. methods: {
  92. foo: function () {
  93. this.x = 'Foo'
  94. },
  95. bar: function () {
  96. this.x = 'Bar'
  97. }
  98. },
  99. el: 'body'
  100. })
  101. `)
  102. setTimeout(() => {
  103. expect(instance.getRealRoot()).toEqual({
  104. type: 'div',
  105. children: [
  106. {
  107. type: 'text',
  108. attr: { value: 'Hello Foo' }
  109. },
  110. {
  111. type: 'text',
  112. event: ['click'],
  113. attr: { value: 'Hello Sub' }
  114. }
  115. ]
  116. })
  117. instance.$fireEvent(instance.doc.body.children[1].ref, 'click', {})
  118. setTimeout(() => {
  119. expect(instance.getRealRoot()).toEqual({
  120. type: 'div',
  121. children: [
  122. {
  123. type: 'text',
  124. attr: { value: 'Hello Bar' }
  125. },
  126. {
  127. type: 'text',
  128. event: ['click'],
  129. attr: { value: 'Hello Sub' }
  130. }
  131. ]
  132. })
  133. done()
  134. })
  135. })
  136. })
  137. })