attrs.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. compileAndStringify,
  3. prepareRuntime,
  4. resetRuntime,
  5. createInstance
  6. } from '../helpers/index'
  7. describe('generate attribute', () => {
  8. let runtime
  9. beforeAll(() => {
  10. runtime = prepareRuntime()
  11. })
  12. afterAll(() => {
  13. resetRuntime()
  14. runtime = null
  15. })
  16. it('should be generated', () => {
  17. const { render, staticRenderFns } = compileAndStringify(`
  18. <div>
  19. <text value="Hello World" style="font-size: 100"></text>
  20. </div>
  21. `)
  22. const instance = createInstance(runtime, `
  23. new Vue({
  24. render: ${render},
  25. staticRenderFns: ${staticRenderFns},
  26. el: 'body'
  27. })
  28. `)
  29. expect(instance.getRealRoot()).toEqual({
  30. type: 'div',
  31. children: [
  32. { type: 'text', style: { fontSize: '100' }, attr: { value: 'Hello World' }}
  33. ]
  34. })
  35. })
  36. it('should be updated', (done) => {
  37. const { render, staticRenderFns } = compileAndStringify(`
  38. <div @click="foo">
  39. <text :value="x"></text>
  40. </div>
  41. `)
  42. const instance = createInstance(runtime, `
  43. new Vue({
  44. data: {
  45. x: 'Hello World'
  46. },
  47. methods: {
  48. foo: function () {
  49. this.x = 'Hello Vue'
  50. }
  51. },
  52. render: ${render},
  53. staticRenderFns: ${staticRenderFns},
  54. el: "body"
  55. })
  56. `)
  57. expect(instance.getRealRoot()).toEqual({
  58. type: 'div',
  59. event: ['click'],
  60. children: [
  61. { type: 'text', attr: { value: 'Hello World' }}
  62. ]
  63. })
  64. instance.$fireEvent(instance.doc.body.ref, 'click', {})
  65. setTimeout(() => {
  66. expect(instance.getRealRoot()).toEqual({
  67. type: 'div',
  68. event: ['click'],
  69. children: [
  70. { type: 'text', attr: { value: 'Hello Vue' }}
  71. ]
  72. })
  73. done()
  74. })
  75. })
  76. it('should be cleared', (done) => {
  77. const { render, staticRenderFns } = compileAndStringify(`
  78. <div @click="foo">
  79. <text :value="x"></text>
  80. </div>
  81. `)
  82. const instance = createInstance(runtime, `
  83. new Vue({
  84. data: {
  85. x: 'Hello World'
  86. },
  87. methods: {
  88. foo: function () {
  89. this.x = ''
  90. }
  91. },
  92. render: ${render},
  93. staticRenderFns: ${staticRenderFns},
  94. el: "body"
  95. })
  96. `)
  97. expect(instance.getRealRoot()).toEqual({
  98. type: 'div',
  99. event: ['click'],
  100. children: [
  101. { type: 'text', attr: { value: 'Hello World' }}
  102. ]
  103. })
  104. instance.$fireEvent(instance.doc.body.ref, 'click', {})
  105. setTimeout(() => {
  106. expect(instance.getRealRoot()).toEqual({
  107. type: 'div',
  108. event: ['click'],
  109. children: [
  110. { type: 'text', attr: { value: '' }}
  111. ]
  112. })
  113. done()
  114. })
  115. })
  116. })