compile.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { BindingTypes, CompilerOptions, RootNode } from '@vue/compiler-dom'
  2. // TODO remove it
  3. import { format } from 'prettier'
  4. import { compile as _compile } from '../src'
  5. async function compile(
  6. template: string | RootNode,
  7. options: CompilerOptions = {},
  8. ) {
  9. let { code } = _compile(template, options)
  10. code = await format(code, {
  11. parser: 'babel',
  12. printWidth: 999999,
  13. singleQuote: true,
  14. })
  15. return code
  16. }
  17. describe('compile', () => {
  18. test('static template', async () => {
  19. const code = await compile(
  20. `<div>
  21. <p>hello</p>
  22. <input />
  23. <span />
  24. </div>`,
  25. )
  26. expect(code).matchSnapshot()
  27. })
  28. test('dynamic root', async () => {
  29. const code = await compile(`{{ 1 }}{{ 2 }}`)
  30. expect(code).matchSnapshot()
  31. })
  32. test('dynamic root nodes and interpolation', async () => {
  33. const code = await compile(
  34. `<button @click="handleClick" :id="count">{{count}}foo{{count}}foo{{count}} </button>`,
  35. )
  36. expect(code).matchSnapshot()
  37. })
  38. test('static + dynamic root', async () => {
  39. const code = await compile(
  40. `{{ 1 }}{{ 2 }}3{{ 4 }}{{ 5 }}6{{ 7 }}{{ 8 }}9{{ 'A' }}{{ 'B' }}`,
  41. )
  42. expect(code).matchSnapshot()
  43. })
  44. test('fragment', async () => {
  45. const code = await compile(`<p/><span/><div/>`)
  46. expect(code).matchSnapshot()
  47. })
  48. test('bindings', async () => {
  49. const code = await compile(`<div>count is {{ count }}.</div>`, {
  50. bindingMetadata: {
  51. count: BindingTypes.SETUP_REF,
  52. },
  53. })
  54. expect(code).matchSnapshot()
  55. })
  56. describe('directives', () => {
  57. describe('v-bind', () => {
  58. test('simple expression', async () => {
  59. const code = await compile(`<div :id="id"></div>`, {
  60. bindingMetadata: {
  61. id: BindingTypes.SETUP_REF,
  62. },
  63. })
  64. expect(code).matchSnapshot()
  65. })
  66. })
  67. describe('v-on', () => {
  68. test('simple expression', async () => {
  69. const code = await compile(`<div @click="handleClick"></div>`, {
  70. bindingMetadata: {
  71. handleClick: BindingTypes.SETUP_CONST,
  72. },
  73. })
  74. expect(code).matchSnapshot()
  75. })
  76. })
  77. describe('v-html', () => {
  78. test('simple expression', async () => {
  79. const code = await compile(`<div v-html="code"></div>`, {
  80. bindingMetadata: {
  81. code: BindingTypes.SETUP_REF,
  82. },
  83. })
  84. expect(code).matchSnapshot()
  85. })
  86. test('no expression', async () => {
  87. const code = await compile(`<div v-html></div>`)
  88. expect(code).matchSnapshot()
  89. })
  90. })
  91. describe('v-text', () => {
  92. test('simple expression', async () => {
  93. const code = await compile(`<div v-text="str"></div>`, {
  94. bindingMetadata: {
  95. str: BindingTypes.SETUP_REF,
  96. },
  97. })
  98. expect(code).matchSnapshot()
  99. })
  100. test('no expression', async () => {
  101. const code = await compile(`<div v-text></div>`)
  102. expect(code).matchSnapshot()
  103. })
  104. })
  105. describe('v-once', () => {
  106. test('basic', async () => {
  107. const code = await compile(
  108. `<div v-once>
  109. {{ msg }}
  110. <span :class="clz" />
  111. </div>`,
  112. {
  113. bindingMetadata: {
  114. msg: BindingTypes.SETUP_REF,
  115. clz: BindingTypes.SETUP_REF,
  116. },
  117. },
  118. )
  119. expect(code).matchSnapshot()
  120. })
  121. test.fails('as root node', async () => {
  122. const code = await compile(`<div :id="foo" v-once />`)
  123. expect(code).toMatchSnapshot()
  124. expect(code).not.contains('effect')
  125. })
  126. })
  127. })
  128. })