2020-02-04 09:47:41 +08:00
|
|
|
import { compile } from '../src'
|
|
|
|
|
|
|
|
describe('ssr: v-for', () => {
|
|
|
|
test('basic', () => {
|
|
|
|
expect(compile(`<div v-for="i in list" />`).code).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
2020-02-04 09:47:41 +08:00
|
|
|
_push(\`<div></div>\`)
|
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('nested content', () => {
|
|
|
|
expect(compile(`<div v-for="i in list">foo<span>bar</span></div>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
2020-02-04 09:47:41 +08:00
|
|
|
_push(\`<div>foo<span>bar</span></div>\`)
|
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('nested v-for', () => {
|
|
|
|
expect(
|
|
|
|
compile(
|
|
|
|
`<div v-for="row, i in list">` +
|
|
|
|
`<div v-for="j in row">{{ i }},{{ j }}</div>` +
|
|
|
|
`</div>`
|
|
|
|
).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (row, i) => {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<div><!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(row, (j) => {
|
2020-02-05 05:47:12 +08:00
|
|
|
_push(\`<div>\${
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrInterpolate(i)
|
2020-02-05 05:47:12 +08:00
|
|
|
},\${
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrInterpolate(j)
|
2020-02-05 05:47:12 +08:00
|
|
|
}</div>\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]--></div>\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template v-for (text)', () => {
|
|
|
|
expect(compile(`<template v-for="i in list">{{ i }}</template>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\${_ssrInterpolate(i)}<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template v-for (single element)', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<template v-for="i in list"><span>{{ i }}</span></template>`)
|
|
|
|
.code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
|
|
|
_push(\`<span>\${_ssrInterpolate(i)}</span>\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template v-for (multi element)', () => {
|
|
|
|
expect(
|
|
|
|
compile(
|
|
|
|
`<template v-for="i in list"><span>{{ i }}</span><span>{{ i + 1 }}</span></template>`
|
|
|
|
).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[--><span>\${
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrInterpolate(i)
|
2020-02-05 05:47:12 +08:00
|
|
|
}</span><span>\${
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrInterpolate(i + 1)
|
2020-03-13 23:55:04 +08:00
|
|
|
}</span><!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('render loop args should not be prefixed', () => {
|
|
|
|
const { code } = compile(
|
|
|
|
`<div v-for="{ foo }, index in list">{{ foo + bar + index }}</div>`
|
|
|
|
)
|
|
|
|
expect(code).toMatch(`_ctx.bar`)
|
|
|
|
expect(code).not.toMatch(`_ctx.foo`)
|
|
|
|
expect(code).not.toMatch(`_ctx.index`)
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-04 09:47:41 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->\`)
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrRenderList(_ctx.list, ({ foo }, index) => {
|
|
|
|
_push(\`<div>\${_ssrInterpolate(foo + _ctx.bar + index)}</div>\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
})
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--]-->\`)
|
2020-02-04 09:47:41 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|