compile.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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('comile', () => {
  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('static + dynamic root', async () => {
  33. const code = await compile(
  34. `{{ 1 }}{{ 2 }}3{{ 4 }}{{ 5 }}6{{ 7 }}{{ 8 }}9{{ 'A' }}{{ 'B' }}`,
  35. )
  36. expect(code).matchSnapshot()
  37. })
  38. test('fragment', async () => {
  39. const code = await compile(`<p/><span/><div/>`)
  40. expect(code).matchSnapshot()
  41. })
  42. test('bindings', async () => {
  43. const code = await compile(`<div>count is {{ count }}.</div>`, {
  44. bindingMetadata: {
  45. count: BindingTypes.SETUP_REF,
  46. },
  47. })
  48. expect(code).matchSnapshot()
  49. })
  50. describe('directives', () => {
  51. describe('v-bind', () => {
  52. test('simple expression', async () => {
  53. const code = await compile(`<div :id="id"></div>`, {
  54. bindingMetadata: {
  55. id: BindingTypes.SETUP_REF,
  56. },
  57. })
  58. expect(code).matchSnapshot()
  59. })
  60. })
  61. describe('v-on', () => {
  62. test('simple expression', async () => {
  63. const code = await compile(`<div @click="handleClick"></div>`, {
  64. bindingMetadata: {
  65. handleClick: BindingTypes.SETUP_CONST,
  66. },
  67. })
  68. expect(code).matchSnapshot()
  69. })
  70. })
  71. describe('v-html', () => {
  72. test('simple expression', async () => {
  73. const code = await compile(`<div v-html="code"></div>`, {
  74. bindingMetadata: {
  75. code: BindingTypes.SETUP_REF,
  76. },
  77. })
  78. expect(code).matchSnapshot()
  79. })
  80. test('no expression', async () => {
  81. const code = await compile(`<div v-html></div>`)
  82. expect(code).matchSnapshot()
  83. })
  84. })
  85. describe('v-text', () => {
  86. test('simple expression', async () => {
  87. const code = await compile(`<div v-text="str"></div>`, {
  88. bindingMetadata: {
  89. str: BindingTypes.SETUP_REF,
  90. },
  91. })
  92. expect(code).matchSnapshot()
  93. })
  94. test('no expression', async () => {
  95. const code = await compile(`<div v-text></div>`)
  96. expect(code).matchSnapshot()
  97. })
  98. })
  99. describe('v-once', () => {
  100. test('basic', async () => {
  101. const code = await compile(
  102. `<div v-once>
  103. {{ msg }}
  104. <span :class="clz" />
  105. </div>`,
  106. {
  107. bindingMetadata: {
  108. msg: BindingTypes.SETUP_REF,
  109. clz: BindingTypes.SETUP_REF,
  110. },
  111. },
  112. )
  113. expect(code).matchSnapshot()
  114. })
  115. test.fails('as root node', async () => {
  116. const code = await compile(`<div :id="foo" v-once />`)
  117. expect(code).toMatchSnapshot()
  118. expect(code).not.contains('effect')
  119. })
  120. })
  121. })
  122. })