2020-02-04 00:46:14 +08:00
|
|
|
import { getCompiledString } from './utils'
|
2020-02-05 11:49:47 +08:00
|
|
|
import { compile } from '../src'
|
2020-02-04 00:46:14 +08:00
|
|
|
|
2020-02-04 04:51:41 +08:00
|
|
|
describe('ssr: element', () => {
|
2020-02-04 00:46:14 +08:00
|
|
|
test('basic elements', () => {
|
|
|
|
expect(getCompiledString(`<div></div>`)).toMatchInlineSnapshot(
|
|
|
|
`"\`<div></div>\`"`
|
|
|
|
)
|
|
|
|
expect(getCompiledString(`<div/>`)).toMatchInlineSnapshot(
|
|
|
|
`"\`<div></div>\`"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('nested elements', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div><span></span><span></span></div>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div><span></span><span></span></div>\`"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('void element', () => {
|
|
|
|
expect(getCompiledString(`<input>`)).toMatchInlineSnapshot(`"\`<input>\`"`)
|
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
describe('children override', () => {
|
|
|
|
test('v-html', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div v-html="foo"/>`)).toMatchInlineSnapshot(`
|
|
|
|
"\`<div>\${
|
|
|
|
_ctx.foo
|
|
|
|
}</div>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-text', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div v-text="foo"/>`)).toMatchInlineSnapshot(`
|
|
|
|
"\`<div>\${
|
|
|
|
_ssrInterpolate(_ctx.foo)
|
|
|
|
}</div>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('<textarea> with dynamic value', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<textarea :value="foo"/>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<textarea>\${
|
|
|
|
_ssrInterpolate(_ctx.foo)
|
|
|
|
}</textarea>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('<textarea> with static value', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<textarea value="fo>o"/>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<textarea>fo>o</textarea>\`"`)
|
|
|
|
})
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
test('<textarea> with dynamic v-bind', () => {
|
2020-02-05 11:49:47 +08:00
|
|
|
expect(compile(`<textarea v-bind="obj">fallback</textarea>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"const { mergeProps: _mergeProps } = require(\\"vue\\")
|
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
|
2020-02-05 11:49:47 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-02-05 11:49:47 +08:00
|
|
|
let _temp0
|
2020-02-06 06:01:00 +08:00
|
|
|
|
2020-02-05 11:49:47 +08:00
|
|
|
_push(\`<textarea\${
|
2020-06-27 02:23:50 +08:00
|
|
|
_ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, _attrs), \\"textarea\\")
|
2020-02-05 11:49:47 +08:00
|
|
|
}>\${
|
2020-02-07 01:09:09 +08:00
|
|
|
_ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"fallback\\")
|
2020-02-05 11:49:47 +08:00
|
|
|
}</textarea>\`)
|
|
|
|
}"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
})
|
2020-03-17 06:24:26 +08:00
|
|
|
|
2021-07-20 06:24:18 +08:00
|
|
|
test('multiple _ssrInterpolate at parent and child import dependency once', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<div>{{ hello }}<textarea v-bind="a"></textarea></div>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2021-02-26 07:25:39 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
|
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
|
|
|
let _temp0
|
|
|
|
|
|
|
|
_push(\`<div\${
|
|
|
|
_ssrRenderAttrs(_attrs)
|
|
|
|
}>\${
|
|
|
|
_ssrInterpolate(_ctx.hello)
|
|
|
|
}<textarea\${
|
|
|
|
_ssrRenderAttrs(_temp0 = _ctx.a, \\"textarea\\")
|
|
|
|
}>\${
|
|
|
|
_ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"\\")
|
|
|
|
}</textarea></div>\`)
|
|
|
|
}"
|
2021-07-20 06:24:18 +08:00
|
|
|
`)
|
|
|
|
})
|
2021-02-26 07:25:39 +08:00
|
|
|
|
2020-03-17 06:24:26 +08:00
|
|
|
test('should pass tag to custom elements w/ dynamic v-bind', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<my-foo v-bind="obj"></my-foo>`, {
|
|
|
|
isCustomElement: () => true
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"const { mergeProps: _mergeProps } = require(\\"vue\\")
|
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
|
2020-03-17 06:24:26 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
|
|
|
_push(\`<my-foo\${_ssrRenderAttrs(_mergeProps(_ctx.obj, _attrs), \\"my-foo\\")}></my-foo>\`)
|
2020-03-17 06:24:26 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
describe('attrs', () => {
|
|
|
|
test('static attrs', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div id="foo" class="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
|
|
|
|
})
|
|
|
|
|
2020-11-28 01:35:00 +08:00
|
|
|
test('ignore static key/ref', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div key="1" ref="el"></div>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('ignore v-bind key/ref', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div :key="1" :ref="el"></div>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
|
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-bind:class', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div id="foo" :class="bar"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div id=\\"foo\\" class=\\"\${
|
|
|
|
_ssrRenderClass(_ctx.bar)
|
|
|
|
}\\"></div>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
|
|
|
|
2020-02-05 07:37:23 +08:00
|
|
|
test('static class + v-bind:class', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div class="foo" :class="bar"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div class=\\"\${
|
|
|
|
_ssrRenderClass([_ctx.bar, \\"foo\\"])
|
2020-10-14 04:25:15 +08:00
|
|
|
}\\"></div>\`"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-bind:class + static class', () => {
|
|
|
|
expect(getCompiledString(`<div :class="bar" class="foo"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div class=\\"\${
|
|
|
|
_ssrRenderClass([_ctx.bar, \\"foo\\"])
|
2020-06-27 02:23:50 +08:00
|
|
|
}\\"></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-bind:style', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div id=\\"foo\\" style=\\"\${
|
|
|
|
_ssrRenderStyle(_ctx.bar)
|
|
|
|
}\\"></div>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
|
|
|
|
2020-02-05 07:37:23 +08:00
|
|
|
test('static style + v-bind:style', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div style="color:red;" :style="bar"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div style=\\"\${
|
|
|
|
_ssrRenderStyle([{\\"color\\":\\"red\\"}, _ctx.bar])
|
|
|
|
}\\"></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
})
|
|
|
|
|
2020-11-28 01:35:00 +08:00
|
|
|
test('v-bind:arg (boolean)', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<input type="checkbox" :checked="checked">`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<input type=\\"checkbox\\"\${
|
|
|
|
(_ctx.checked) ? \\" checked\\" : \\"\\"
|
|
|
|
}>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
|
|
|
|
2020-11-28 01:35:00 +08:00
|
|
|
test('v-bind:arg (non-boolean)', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div :id="id" class="bar"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttr(\\"id\\", _ctx.id)
|
|
|
|
} class=\\"bar\\"></div>\`"
|
|
|
|
`)
|
2020-02-05 05:47:12 +08:00
|
|
|
})
|
2020-02-05 07:37:23 +08:00
|
|
|
|
2020-11-28 01:35:00 +08:00
|
|
|
test('v-bind:[arg]', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
2020-10-20 05:15:53 +08:00
|
|
|
_ssrRenderAttrs({ [_ctx.key || \\"\\"]: _ctx.value })
|
2020-06-27 02:23:50 +08:00
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
expect(getCompiledString(`<div class="foo" v-bind:[key]="value"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs({
|
|
|
|
class: \\"foo\\",
|
2020-10-20 05:15:53 +08:00
|
|
|
[_ctx.key || \\"\\"]: _ctx.value
|
2020-06-27 02:23:50 +08:00
|
|
|
})
|
|
|
|
}></div>\`"
|
2020-02-05 07:37:23 +08:00
|
|
|
`)
|
|
|
|
|
|
|
|
expect(getCompiledString(`<div :id="id" v-bind:[key]="value"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs({
|
|
|
|
id: _ctx.id,
|
2020-10-20 05:15:53 +08:00
|
|
|
[_ctx.key || \\"\\"]: _ctx.value
|
2020-06-27 02:23:50 +08:00
|
|
|
})
|
|
|
|
}></div>\`"
|
2020-02-05 07:37:23 +08:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-bind="obj"', () => {
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div v-bind="obj"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_ctx.obj)
|
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div class="foo" v-bind="obj"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_mergeProps({ class: \\"foo\\" }, _ctx.obj))
|
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div :id="id" v-bind="obj"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_mergeProps({ id: _ctx.id }, _ctx.obj))
|
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
// dynamic key + v-bind="object"
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div :[key]="id" v-bind="obj"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
2020-10-20 05:15:53 +08:00
|
|
|
_ssrRenderAttrs(_mergeProps({ [_ctx.key || \\"\\"]: _ctx.id }, _ctx.obj))
|
2020-06-27 02:23:50 +08:00
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
// should merge class and :class
|
|
|
|
expect(getCompiledString(`<div class="a" :class="b" v-bind="obj"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_mergeProps({
|
|
|
|
class: [\\"a\\", _ctx.b]
|
|
|
|
}, _ctx.obj))
|
|
|
|
}></div>\`"
|
2020-02-05 07:37:23 +08:00
|
|
|
`)
|
|
|
|
|
|
|
|
// should merge style and :style
|
|
|
|
expect(
|
|
|
|
getCompiledString(
|
|
|
|
`<div style="color:red;" :style="b" v-bind="obj"></div>`
|
|
|
|
)
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-06-27 02:23:50 +08:00
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_mergeProps({
|
|
|
|
style: [{\\"color\\":\\"red\\"}, _ctx.b]
|
|
|
|
}, _ctx.obj))
|
|
|
|
}></div>\`"
|
2020-02-05 07:37:23 +08:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore v-on', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div id="foo" @click="bar"/>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div id="foo" v-on="bar"/>`)
|
|
|
|
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
|
2020-06-27 02:23:50 +08:00
|
|
|
expect(getCompiledString(`<div v-bind="foo" v-on="bar"/>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"\`<div\${
|
|
|
|
_ssrRenderAttrs(_ctx.foo)
|
|
|
|
}></div>\`"
|
|
|
|
`)
|
2020-02-05 07:37:23 +08:00
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
})
|
|
|
|
})
|