ssrVIf.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { compile } from '../src'
  2. describe('ssr: v-if', () => {
  3. test('basic', () => {
  4. expect(compile(`<div v-if="foo"></div>`).code).toMatchInlineSnapshot(`
  5. "
  6. return function ssrRender(_ctx, _push, _parent) {
  7. if (_ctx.foo) {
  8. _push(\`<div></div>\`)
  9. } else {
  10. _push(\`<!---->\`)
  11. }
  12. }"
  13. `)
  14. })
  15. test('with nested content', () => {
  16. expect(compile(`<div v-if="foo">hello<span>ok</span></div>`).code)
  17. .toMatchInlineSnapshot(`
  18. "
  19. return function ssrRender(_ctx, _push, _parent) {
  20. if (_ctx.foo) {
  21. _push(\`<div>hello<span>ok</span></div>\`)
  22. } else {
  23. _push(\`<!---->\`)
  24. }
  25. }"
  26. `)
  27. })
  28. test('v-if + v-else', () => {
  29. expect(compile(`<div v-if="foo"/><span v-else/>`).code)
  30. .toMatchInlineSnapshot(`
  31. "
  32. return function ssrRender(_ctx, _push, _parent) {
  33. if (_ctx.foo) {
  34. _push(\`<div></div>\`)
  35. } else {
  36. _push(\`<span></span>\`)
  37. }
  38. }"
  39. `)
  40. })
  41. test('v-if + v-else-if', () => {
  42. expect(compile(`<div v-if="foo"/><span v-else-if="bar"/>`).code)
  43. .toMatchInlineSnapshot(`
  44. "
  45. return function ssrRender(_ctx, _push, _parent) {
  46. if (_ctx.foo) {
  47. _push(\`<div></div>\`)
  48. } else if (_ctx.bar) {
  49. _push(\`<span></span>\`)
  50. } else {
  51. _push(\`<!---->\`)
  52. }
  53. }"
  54. `)
  55. })
  56. test('v-if + v-else-if + v-else', () => {
  57. expect(compile(`<div v-if="foo"/><span v-else-if="bar"/><p v-else/>`).code)
  58. .toMatchInlineSnapshot(`
  59. "
  60. return function ssrRender(_ctx, _push, _parent) {
  61. if (_ctx.foo) {
  62. _push(\`<div></div>\`)
  63. } else if (_ctx.bar) {
  64. _push(\`<span></span>\`)
  65. } else {
  66. _push(\`<p></p>\`)
  67. }
  68. }"
  69. `)
  70. })
  71. test('<template v-if> (text)', () => {
  72. expect(compile(`<template v-if="foo">hello</template>`).code)
  73. .toMatchInlineSnapshot(`
  74. "
  75. return function ssrRender(_ctx, _push, _parent) {
  76. if (_ctx.foo) {
  77. _push(\`hello\`)
  78. } else {
  79. _push(\`<!---->\`)
  80. }
  81. }"
  82. `)
  83. })
  84. test('<template v-if> (single element)', () => {
  85. // single element should not wrap with fragment
  86. expect(compile(`<template v-if="foo"><div>hi</div></template>`).code)
  87. .toMatchInlineSnapshot(`
  88. "
  89. return function ssrRender(_ctx, _push, _parent) {
  90. if (_ctx.foo) {
  91. _push(\`<div>hi</div>\`)
  92. } else {
  93. _push(\`<!---->\`)
  94. }
  95. }"
  96. `)
  97. })
  98. test('<template v-if> (multiple element)', () => {
  99. expect(
  100. compile(`<template v-if="foo"><div>hi</div><div>ho</div></template>`).code
  101. ).toMatchInlineSnapshot(`
  102. "
  103. return function ssrRender(_ctx, _push, _parent) {
  104. if (_ctx.foo) {
  105. _push(\`<div>hi</div><div>ho</div>\`)
  106. } else {
  107. _push(\`<!---->\`)
  108. }
  109. }"
  110. `)
  111. })
  112. test('<template v-if> (with v-for inside)', () => {
  113. expect(
  114. compile(`<template v-if="foo"><div v-for="i in list"/></template>`).code
  115. ).toMatchInlineSnapshot(`
  116. "const { ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
  117. return function ssrRender(_ctx, _push, _parent) {
  118. if (_ctx.foo) {
  119. _ssrRenderList(_ctx.list, (i) => {
  120. _push(\`<div></div>\`)
  121. })
  122. } else {
  123. _push(\`<!---->\`)
  124. }
  125. }"
  126. `)
  127. })
  128. test('<template v-if> + normal v-else', () => {
  129. expect(
  130. compile(
  131. `<template v-if="foo"><div>hi</div><div>ho</div></template><div v-else/>`
  132. ).code
  133. ).toMatchInlineSnapshot(`
  134. "
  135. return function ssrRender(_ctx, _push, _parent) {
  136. if (_ctx.foo) {
  137. _push(\`<div>hi</div><div>ho</div>\`)
  138. } else {
  139. _push(\`<div></div>\`)
  140. }
  141. }"
  142. `)
  143. })
  144. })