import { compile } from '../src'
describe('ssr: v-for', () => {
test('basic', () => {
expect(compile(`
`).code).toMatchInlineSnapshot(`
"const { ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (i) => {
_push(\`\`)
})
}"
`)
})
test('nested content', () => {
expect(compile(`foobar
`).code)
.toMatchInlineSnapshot(`
"const { ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (i) => {
_push(\`foobar
\`)
})
}"
`)
})
test('nested v-for', () => {
expect(
compile(
`` +
`
{{ i }},{{ j }}
` +
`
`
).code
).toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (row, i) => {
_push(\`\`)
_ssrRenderList(row, (j) => {
_push(\`
\${
_ssrInterpolate(i)
},\${
_ssrInterpolate(j)
}
\`)
})
_push(\`
\`)
})
}"
`)
})
test('template v-for (text)', () => {
expect(compile(`{{ i }}`).code)
.toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (i) => {
_push(\`\${_ssrInterpolate(i)}\`)
})
}"
`)
})
test('template v-for (single element)', () => {
expect(
compile(`{{ i }}`)
.code
).toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (i) => {
_push(\`\${_ssrInterpolate(i)}\`)
})
}"
`)
})
test('template v-for (multi element)', () => {
expect(
compile(
`{{ i }}{{ i + 1 }}`
).code
).toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, (i) => {
_push(\`\${
_ssrInterpolate(i)
}\${
_ssrInterpolate(i + 1)
}\`)
})
}"
`)
})
test('render loop args should not be prefixed', () => {
const { code } = compile(
`{{ foo + bar + index }}
`
)
expect(code).toMatch(`_ctx.bar`)
expect(code).not.toMatch(`_ctx.foo`)
expect(code).not.toMatch(`_ctx.index`)
expect(code).toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_ssrRenderList(_ctx.list, ({ foo }, index) => {
_push(\`\${_ssrInterpolate(foo + _ctx.bar + index)}
\`)
})
}"
`)
})
})