ssrCompilerOptions.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { createApp } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. describe('ssr: compiler options', () => {
  4. test('config.isCustomElement (deprecated)', async () => {
  5. const app = createApp({
  6. template: `<div><x-button/></div>`,
  7. })
  8. app.config.isCustomElement = tag => tag.startsWith('x-')
  9. expect(await renderToString(app)).toBe(`<div><x-button></x-button></div>`)
  10. })
  11. test('config.compilerOptions.isCustomElement', async () => {
  12. const app = createApp({
  13. template: `<div><x-panel/></div>`,
  14. })
  15. app.config.compilerOptions.isCustomElement = tag => tag.startsWith('x-')
  16. expect(await renderToString(app)).toBe(`<div><x-panel></x-panel></div>`)
  17. })
  18. test('component.compilerOptions.isCustomElement', async () => {
  19. const app = createApp({
  20. template: `<div><x-card/><y-child/></div>`,
  21. compilerOptions: {
  22. isCustomElement: (tag: string) => tag.startsWith('x-'),
  23. },
  24. components: {
  25. YChild: {
  26. template: `<div><y-button/></div>`,
  27. },
  28. },
  29. })
  30. app.config.compilerOptions.isCustomElement = tag => tag.startsWith('y-')
  31. expect(await renderToString(app)).toBe(
  32. `<div><x-card></x-card><div><y-button></y-button></div></div>`,
  33. )
  34. })
  35. test('component.delimiters (deprecated)', async () => {
  36. const app = createApp({
  37. template: `<div>[[ 1 + 1 ]]</div>`,
  38. delimiters: ['[[', ']]'],
  39. })
  40. expect(await renderToString(app)).toBe(`<div>2</div>`)
  41. })
  42. test('config.compilerOptions.delimiters', async () => {
  43. const app = createApp({
  44. template: `<div>[( 1 + 1 )]</div>`,
  45. })
  46. app.config.compilerOptions.delimiters = ['[(', ')]']
  47. expect(await renderToString(app)).toBe(`<div>2</div>`)
  48. })
  49. test('component.compilerOptions.delimiters', async () => {
  50. const app = createApp({
  51. template: `<div>[[ 1 + 1 ]]<ChildComponent/></div>`,
  52. compilerOptions: {
  53. delimiters: ['[[', ']]'],
  54. },
  55. components: {
  56. ChildComponent: {
  57. template: `<div>(( 2 + 2 ))</div>`,
  58. },
  59. },
  60. })
  61. app.config.compilerOptions.delimiters = ['((', '))']
  62. expect(await renderToString(app)).toBe(`<div>2<div>4</div></div>`)
  63. })
  64. test('compilerOptions.whitespace', async () => {
  65. const app = createApp({
  66. template: `<div><span>Hello world</span><ChildComponent/></div>`,
  67. compilerOptions: {
  68. whitespace: 'condense',
  69. },
  70. components: {
  71. ChildComponent: {
  72. template: `<span>Hello world</span>`,
  73. },
  74. },
  75. })
  76. app.config.compilerOptions.whitespace = 'preserve'
  77. expect(await renderToString(app)).toBe(
  78. `<div><span>Hello world</span><span>Hello world</span></div>`,
  79. )
  80. })
  81. test('caching with compilerOptions', async () => {
  82. const template = `<div>{{1 + 1}} [[1 + 1]]</div>`
  83. const app = createApp({
  84. template: `<div><ChildOne/><ChildTwo/><ChildThree/></div>`,
  85. components: {
  86. ChildOne: {
  87. template,
  88. },
  89. ChildTwo: {
  90. template,
  91. compilerOptions: {
  92. whitespace: 'preserve',
  93. },
  94. },
  95. ChildThree: {
  96. template,
  97. compilerOptions: {
  98. delimiters: ['[[', ']]'],
  99. },
  100. },
  101. },
  102. })
  103. expect(await renderToString(app)).toBe(
  104. `<div><div>2 [[1 + 1]]</div><div>2 [[1 + 1]]</div><div>{{1 + 1}} 2</div></div>`,
  105. )
  106. })
  107. test('caching with isCustomElement', async () => {
  108. const template = `<div><MyChild/></div>`
  109. const app = createApp({
  110. template,
  111. // No compilerOptions on the root
  112. components: {
  113. MyChild: {
  114. template,
  115. compilerOptions: {
  116. isCustomElement: tag => tag.startsWith('x-'),
  117. },
  118. components: {
  119. MyChild: {
  120. template,
  121. compilerOptions: {
  122. isCustomElement: tag => tag.startsWith('My'),
  123. },
  124. },
  125. },
  126. },
  127. },
  128. })
  129. expect(await renderToString(app)).toBe(
  130. `<div><div><div><MyChild></MyChild></div></div></div>`,
  131. )
  132. })
  133. })