ssrVFor.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { compile } from '../src'
  2. describe('ssr: v-for', () => {
  3. test('basic', () => {
  4. expect(compile(`<div v-for="i in list" />`).code).toMatchInlineSnapshot(`
  5. "const { ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  6. return function ssrRender(_ctx, _push, _parent, _attrs) {
  7. _push(\`<!--[-->\`)
  8. _ssrRenderList(_ctx.list, (i) => {
  9. _push(\`<div></div>\`)
  10. })
  11. _push(\`<!--]-->\`)
  12. }"
  13. `)
  14. })
  15. test('nested content', () => {
  16. expect(compile(`<div v-for="i in list">foo<span>bar</span></div>`).code)
  17. .toMatchInlineSnapshot(`
  18. "const { ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  19. return function ssrRender(_ctx, _push, _parent, _attrs) {
  20. _push(\`<!--[-->\`)
  21. _ssrRenderList(_ctx.list, (i) => {
  22. _push(\`<div>foo<span>bar</span></div>\`)
  23. })
  24. _push(\`<!--]-->\`)
  25. }"
  26. `)
  27. })
  28. test('nested v-for', () => {
  29. expect(
  30. compile(
  31. `<div v-for="row, i in list">` +
  32. `<div v-for="j in row">{{ i }},{{ j }}</div>` +
  33. `</div>`,
  34. ).code,
  35. ).toMatchInlineSnapshot(`
  36. "const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  37. return function ssrRender(_ctx, _push, _parent, _attrs) {
  38. _push(\`<!--[-->\`)
  39. _ssrRenderList(_ctx.list, (row, i) => {
  40. _push(\`<div><!--[-->\`)
  41. _ssrRenderList(row, (j) => {
  42. _push(\`<div>\${
  43. _ssrInterpolate(i)
  44. },\${
  45. _ssrInterpolate(j)
  46. }</div>\`)
  47. })
  48. _push(\`<!--]--></div>\`)
  49. })
  50. _push(\`<!--]-->\`)
  51. }"
  52. `)
  53. })
  54. test('template v-for (text)', () => {
  55. expect(compile(`<template v-for="i in list">{{ i }}</template>`).code)
  56. .toMatchInlineSnapshot(`
  57. "const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  58. return function ssrRender(_ctx, _push, _parent, _attrs) {
  59. _push(\`<!--[-->\`)
  60. _ssrRenderList(_ctx.list, (i) => {
  61. _push(\`<!--[-->\${_ssrInterpolate(i)}<!--]-->\`)
  62. })
  63. _push(\`<!--]-->\`)
  64. }"
  65. `)
  66. })
  67. test('template v-for (single element)', () => {
  68. expect(
  69. compile(`<template v-for="i in list"><span>{{ i }}</span></template>`)
  70. .code,
  71. ).toMatchInlineSnapshot(`
  72. "const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  73. return function ssrRender(_ctx, _push, _parent, _attrs) {
  74. _push(\`<!--[-->\`)
  75. _ssrRenderList(_ctx.list, (i) => {
  76. _push(\`<span>\${_ssrInterpolate(i)}</span>\`)
  77. })
  78. _push(\`<!--]-->\`)
  79. }"
  80. `)
  81. })
  82. test('template v-for (multi element)', () => {
  83. expect(
  84. compile(
  85. `<template v-for="i in list"><span>{{ i }}</span><span>{{ i + 1 }}</span></template>`,
  86. ).code,
  87. ).toMatchInlineSnapshot(`
  88. "const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  89. return function ssrRender(_ctx, _push, _parent, _attrs) {
  90. _push(\`<!--[-->\`)
  91. _ssrRenderList(_ctx.list, (i) => {
  92. _push(\`<!--[--><span>\${
  93. _ssrInterpolate(i)
  94. }</span><span>\${
  95. _ssrInterpolate(i + 1)
  96. }</span><!--]-->\`)
  97. })
  98. _push(\`<!--]-->\`)
  99. }"
  100. `)
  101. })
  102. test('render loop args should not be prefixed', () => {
  103. const { code } = compile(
  104. `<div v-for="{ foo }, index in list">{{ foo + bar + index }}</div>`,
  105. )
  106. expect(code).toMatch(`_ctx.bar`)
  107. expect(code).not.toMatch(`_ctx.foo`)
  108. expect(code).not.toMatch(`_ctx.index`)
  109. expect(code).toMatchInlineSnapshot(`
  110. "const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
  111. return function ssrRender(_ctx, _push, _parent, _attrs) {
  112. _push(\`<!--[-->\`)
  113. _ssrRenderList(_ctx.list, ({ foo }, index) => {
  114. _push(\`<div>\${_ssrInterpolate(foo + _ctx.bar + index)}</div>\`)
  115. })
  116. _push(\`<!--]-->\`)
  117. }"
  118. `)
  119. })
  120. })