vue3-yuanma/packages/compiler-ssr/__tests__/ssrText.spec.ts

73 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-02-04 06:47:06 +08:00
import { compile } from '../src'
import { getCompiledString } from './utils'
2020-02-04 04:51:41 +08:00
describe('ssr: text', () => {
test('static text', () => {
expect(getCompiledString(`foo`)).toMatchInlineSnapshot(`"\`foo\`"`)
})
test('static text with template string special chars', () => {
expect(getCompiledString(`\`\${foo}\``)).toMatchInlineSnapshot(
`"\`\\\\\`\\\\\${foo}\\\\\`\`"`
)
})
test('static text with char escape', () => {
// the desired generated code should be `\\\$foo`
// snapshot -> inline snapshot goes through two escapes
// so that makes a total of 3 * 2 * 2 = 12 back slashes
expect(getCompiledString(`\\$foo`)).toMatchInlineSnapshot(
`"\`\\\\\\\\\\\\$foo\`"`
)
})
test('comments', () => {
expect(getCompiledString(`<!--bar-->`)).toMatchInlineSnapshot(
`"\`<!--bar-->\`"`
)
})
test('static text escape', () => {
expect(getCompiledString(`&lt;foo&gt;`)).toMatchInlineSnapshot(
`"\`&lt;foo&gt;\`"`
)
})
test('nested elements with static text', () => {
expect(
getCompiledString(`<div><span>hello</span><span>bye</span></div>`)
).toMatchInlineSnapshot(
`"\`<div><span>hello</span><span>bye</span></div>\`"`
)
})
test('interpolation', () => {
2020-02-04 06:47:06 +08:00
expect(compile(`foo {{ bar }} baz`).code).toMatchInlineSnapshot(`
"const { ssrInterpolate: _ssrInterpolate } = require(\\"vue/server-renderer\\")
2020-02-04 06:47:06 +08:00
return function ssrRender(_ctx, _push, _parent, _attrs) {
2020-02-07 01:09:09 +08:00
_push(\`foo \${_ssrInterpolate(_ctx.bar)} baz\`)
2020-02-04 06:47:06 +08:00
}"
`)
})
test('nested elements with interpolation', () => {
expect(
2020-02-04 06:47:06 +08:00
compile(`<div><span>{{ foo }} bar</span><span>baz {{ qux }}</span></div>`)
.code
).toMatchInlineSnapshot(`
"const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"vue/server-renderer\\")
2020-02-04 06:47:06 +08:00
return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${
_ssrRenderAttrs(_attrs)
}><span>\${
2020-02-07 01:09:09 +08:00
_ssrInterpolate(_ctx.foo)
} bar</span><span>baz \${
2020-02-07 01:09:09 +08:00
_ssrInterpolate(_ctx.qux)
}</span></div>\`)
2020-02-04 06:47:06 +08:00
}"
`)
})
})