ssrCompilerOptions.spec.ts 4.1 KB

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