2020-02-04 04:51:41 +08:00
|
|
|
import { compile } from '../src'
|
|
|
|
|
|
|
|
describe('ssr: v-if', () => {
|
|
|
|
test('basic', () => {
|
|
|
|
expect(compile(`<div v-if="foo"></div>`).code).toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with nested content', () => {
|
|
|
|
expect(compile(`<div v-if="foo">hello<span>ok</span></div>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}>hello<span>ok</span></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-if + v-else', () => {
|
|
|
|
expect(compile(`<div v-if="foo"/><span v-else/>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<span\${_ssrRenderAttrs(_attrs)}></span>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-if + v-else-if', () => {
|
|
|
|
expect(compile(`<div v-if="foo"/><span v-else-if="bar"/>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else if (_ctx.bar) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<span\${_ssrRenderAttrs(_attrs)}></span>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-if + v-else-if + v-else', () => {
|
|
|
|
expect(compile(`<div v-if="foo"/><span v-else-if="bar"/><p v-else/>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else if (_ctx.bar) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<span\${_ssrRenderAttrs(_attrs)}></span>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<p\${_ssrRenderAttrs(_attrs)}></p>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('<template v-if> (text)', () => {
|
|
|
|
expect(compile(`<template v-if="foo">hello</template>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[-->hello<!--]-->\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('<template v-if> (single element)', () => {
|
|
|
|
// single element should not wrap with fragment
|
|
|
|
expect(compile(`<template v-if="foo"><div>hi</div></template>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}>hi</div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('<template v-if> (multiple element)', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<template v-if="foo"><div>hi</div><div>ho</div></template>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
|
|
|
"
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[--><div>hi</div><div>ho</div><!--]-->\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('<template v-if> (with v-for inside)', () => {
|
2020-02-04 09:47:41 +08:00
|
|
|
expect(
|
|
|
|
compile(`<template v-if="foo"><div v-for="i in list"/></template>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +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-02-04 09:47:41 +08:00
|
|
|
if (_ctx.foo) {
|
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
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
2020-02-04 04:51:41 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('<template v-if> + normal v-else', () => {
|
|
|
|
expect(
|
|
|
|
compile(
|
|
|
|
`<template v-if="foo"><div>hi</div><div>ho</div></template><div v-else/>`
|
|
|
|
).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2021-09-23 11:28:13 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
2020-06-27 02:23:50 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-04 04:51:41 +08:00
|
|
|
if (_ctx.foo) {
|
2020-03-13 23:55:04 +08:00
|
|
|
_push(\`<!--[--><div>hi</div><div>ho</div><!--]-->\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
} else {
|
2020-06-27 02:23:50 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
|
2020-02-04 04:51:41 +08:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|