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', () => {
|
|
|
|
expect(getCompiledString(`<div v-html="foo"/>`)).toMatchInlineSnapshot(
|
|
|
|
`"\`<div>\${_ctx.foo}</div>\`"`
|
|
|
|
)
|
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-text', () => {
|
|
|
|
expect(getCompiledString(`<div v-text="foo"/>`)).toMatchInlineSnapshot(
|
2020-02-07 01:09:09 +08:00
|
|
|
`"\`<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', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<textarea :value="foo"/>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-07 01:09:09 +08:00
|
|
|
`"\`<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-02-08 08:04:55 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
|
2020-02-05 11:49:47 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
|
|
|
let _temp0
|
2020-02-06 06:01:00 +08:00
|
|
|
|
2020-02-05 11:49:47 +08:00
|
|
|
_push(\`<textarea\${
|
2020-03-17 06:24:26 +08:00
|
|
|
_ssrRenderAttrs(_temp0 = _ctx.obj, \\"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
|
|
|
|
|
|
|
test('should pass tag to custom elements w/ dynamic v-bind', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<my-foo v-bind="obj"></my-foo>`, {
|
|
|
|
isCustomElement: () => true
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
|
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
|
|
|
_push(\`<my-foo\${_ssrRenderAttrs(_ctx.obj, \\"my-foo\\")}></my-foo>\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
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>\`"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-bind:class', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div id="foo" :class="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-15 09:48:06 +08:00
|
|
|
`"\`<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', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div class="foo" :class="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-15 09:48:06 +08:00
|
|
|
`"\`<div class=\\"\${_ssrRenderClass([_ctx.bar, \\"foo\\"])}\\"></div>\`"`
|
2020-02-05 07:37:23 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-bind:style', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div id="foo" :style="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-15 09:48:06 +08:00
|
|
|
`"\`<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', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div style="color:red;" :style="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-21 20:10:13 +08:00
|
|
|
`"\`<div style=\\"\${_ssrRenderStyle([{\\"color\\":\\"red\\"}, _ctx.bar])}\\"></div>\`"`
|
2020-02-05 07:37:23 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
test('v-bind:key (boolean)', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<input type="checkbox" :checked="checked">`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-05 11:49:47 +08:00
|
|
|
`"\`<input type=\\"checkbox\\"\${(_ctx.checked) ? \\" checked\\" : \\"\\"}>\`"`
|
2020-02-05 05:47:12 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-bind:key (non-boolean)', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div :id="id" class="bar"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-07 01:09:09 +08:00
|
|
|
`"\`<div\${_ssrRenderAttr(\\"id\\", _ctx.id)} class=\\"bar\\"></div>\`"`
|
2020-02-05 05:47:12 +08:00
|
|
|
)
|
|
|
|
})
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
test('v-bind:[key]', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div v-bind:[key]="value"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-07 01:09:09 +08:00
|
|
|
`"\`<div\${_ssrRenderAttrs({ [_ctx.key]: _ctx.value })}></div>\`"`
|
2020-02-05 07:37:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(getCompiledString(`<div class="foo" v-bind:[key]="value"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-07 01:09:09 +08:00
|
|
|
"\`<div\${_ssrRenderAttrs({
|
2020-02-05 07:37:23 +08:00
|
|
|
class: \\"foo\\",
|
|
|
|
[_ctx.key]: _ctx.value
|
|
|
|
})}></div>\`"
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(getCompiledString(`<div :id="id" v-bind:[key]="value"></div>`))
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-07 01:09:09 +08:00
|
|
|
"\`<div\${_ssrRenderAttrs({
|
2020-02-05 07:37:23 +08:00
|
|
|
id: _ctx.id,
|
|
|
|
[_ctx.key]: _ctx.value
|
|
|
|
})}></div>\`"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-bind="obj"', () => {
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div v-bind="obj"></div>`)
|
2020-02-07 01:09:09 +08:00
|
|
|
).toMatchInlineSnapshot(`"\`<div\${_ssrRenderAttrs(_ctx.obj)}></div>\`"`)
|
2020-02-05 07:37:23 +08:00
|
|
|
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div class="foo" v-bind="obj"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-08 08:04:55 +08:00
|
|
|
`"\`<div\${_ssrRenderAttrs(_mergeProps({ class: \\"foo\\" }, _ctx.obj))}></div>\`"`
|
2020-02-05 07:37:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div :id="id" v-bind="obj"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-08 08:04:55 +08:00
|
|
|
`"\`<div\${_ssrRenderAttrs(_mergeProps({ id: _ctx.id }, _ctx.obj))}></div>\`"`
|
2020-02-05 07:37:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// dynamic key + v-bind="object"
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div :[key]="id" v-bind="obj"></div>`)
|
|
|
|
).toMatchInlineSnapshot(
|
2020-02-08 08:04:55 +08:00
|
|
|
`"\`<div\${_ssrRenderAttrs(_mergeProps({ [_ctx.key]: _ctx.id }, _ctx.obj))}></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-02-08 08:04:55 +08:00
|
|
|
"\`<div\${_ssrRenderAttrs(_mergeProps({
|
2020-02-05 07:37:23 +08:00
|
|
|
class: [\\"a\\", _ctx.b]
|
|
|
|
}, _ctx.obj))}></div>\`"
|
|
|
|
`)
|
|
|
|
|
|
|
|
// should merge style and :style
|
|
|
|
expect(
|
|
|
|
getCompiledString(
|
|
|
|
`<div style="color:red;" :style="b" v-bind="obj"></div>`
|
|
|
|
)
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"\`<div\${_ssrRenderAttrs(_mergeProps({
|
2020-02-21 20:10:13 +08:00
|
|
|
style: [{\\"color\\":\\"red\\"}, _ctx.b]
|
2020-02-05 07:37:23 +08:00
|
|
|
}, _ctx.obj))}></div>\`"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
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>\`"`)
|
|
|
|
expect(
|
|
|
|
getCompiledString(`<div v-bind="foo" v-on="bar"/>`)
|
2020-02-07 01:09:09 +08:00
|
|
|
).toMatchInlineSnapshot(`"\`<div\${_ssrRenderAttrs(_ctx.foo)}></div>\`"`)
|
2020-02-05 07:37:23 +08:00
|
|
|
})
|
2020-02-04 00:46:14 +08:00
|
|
|
})
|
|
|
|
})
|