import { compile } from '../src'
describe('ssr: v-if', () => {
test('basic', () => {
expect(compile(`
`).code).toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`\`)
} else {
_push(\`\`)
}
}"
`)
})
test('with nested content', () => {
expect(compile(`hellook
`).code)
.toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`hellook
\`)
} else {
_push(\`\`)
}
}"
`)
})
test('v-if + v-else', () => {
expect(compile(``).code)
.toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`\`)
} else {
_push(\`\`)
}
}"
`)
})
test('v-if + v-else-if', () => {
expect(compile(``).code)
.toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`\`)
} else if (_ctx.bar) {
_push(\`\`)
} else {
_push(\`\`)
}
}"
`)
})
test('v-if + v-else-if + v-else', () => {
expect(compile(``).code)
.toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`\`)
} else if (_ctx.bar) {
_push(\`\`)
} else {
_push(\`\`)
}
}"
`)
})
test(' (text)', () => {
expect(compile(`hello`).code)
.toMatchInlineSnapshot(`
"
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`hello\`)
} else {
_push(\`\`)
}
}"
`)
})
test(' (single element)', () => {
// single element should not wrap with fragment
expect(compile(`hi
`).code)
.toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`hi
\`)
} else {
_push(\`\`)
}
}"
`)
})
test(' (multiple element)', () => {
expect(
compile(`hi
ho
`).code
).toMatchInlineSnapshot(`
"
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`hi
ho
\`)
} else {
_push(\`\`)
}
}"
`)
})
test(' (with v-for inside)', () => {
expect(
compile(``).code
).toMatchInlineSnapshot(`
"const { ssrRenderList: _ssrRenderList } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`\`)
_ssrRenderList(_ctx.list, (i) => {
_push(\`\`)
})
_push(\`\`)
} else {
_push(\`\`)
}
}"
`)
})
test(' + normal v-else', () => {
expect(
compile(
`hi
ho
`
).code
).toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(\`hi
ho
\`)
} else {
_push(\`\`)
}
}"
`)
})
})