ssrText.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { compile } from '../src'
  2. import { getCompiledString } from './utils'
  3. describe('ssr: text', () => {
  4. test('static text', () => {
  5. expect(getCompiledString(`foo`)).toMatchInlineSnapshot(`"\`foo\`"`)
  6. })
  7. test('static text with template string special chars', () => {
  8. expect(getCompiledString(`\`\${foo}\``)).toMatchInlineSnapshot(
  9. `"\`\\\`\\\${foo}\\\`\`"`,
  10. )
  11. })
  12. test('static text with char escape', () => {
  13. // the desired generated code should be `\\\$foo`
  14. // snapshot -> inline snapshot goes through two escapes
  15. // so that makes a total of 3 * 2 * 2 = 12 back slashes
  16. expect(getCompiledString(`\\$foo`)).toMatchInlineSnapshot(
  17. `"\`\\\\\\$foo\`"`,
  18. )
  19. })
  20. test('comments', () => {
  21. expect(getCompiledString(`<!--bar-->`)).toMatchInlineSnapshot(
  22. `"\`<!--bar-->\`"`,
  23. )
  24. })
  25. test('static text escape', () => {
  26. expect(getCompiledString(`&lt;foo&gt;`)).toMatchInlineSnapshot(
  27. `"\`&lt;foo&gt;\`"`,
  28. )
  29. })
  30. test('nested elements with static text', () => {
  31. expect(
  32. getCompiledString(`<div><span>hello</span><span>bye</span></div>`),
  33. ).toMatchInlineSnapshot(
  34. `"\`<div><span>hello</span><span>bye</span></div>\`"`,
  35. )
  36. })
  37. test('interpolation', () => {
  38. expect(compile(`foo {{ bar }} baz`).code).toMatchInlineSnapshot(`
  39. "const { ssrInterpolate: _ssrInterpolate } = require("vue/server-renderer")
  40. return function ssrRender(_ctx, _push, _parent, _attrs) {
  41. _push(\`foo \${_ssrInterpolate(_ctx.bar)} baz\`)
  42. }"
  43. `)
  44. })
  45. test('nested elements with interpolation', () => {
  46. expect(
  47. compile(`<div><span>{{ foo }} bar</span><span>baz {{ qux }}</span></div>`)
  48. .code,
  49. ).toMatchInlineSnapshot(`
  50. "const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require("vue/server-renderer")
  51. return function ssrRender(_ctx, _push, _parent, _attrs) {
  52. _push(\`<div\${
  53. _ssrRenderAttrs(_attrs)
  54. }><span>\${
  55. _ssrInterpolate(_ctx.foo)
  56. } bar</span><span>baz \${
  57. _ssrInterpolate(_ctx.qux)
  58. }</span></div>\`)
  59. }"
  60. `)
  61. })
  62. })