ssrFallthroughAttrs.spec.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { compile } from '../src'
  2. describe('ssr: attrs fallthrough', () => {
  3. test('basic', () => {
  4. expect(compile(`<div/>`).code).toMatchInlineSnapshot(`
  5. "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  6. return function ssrRender(_ctx, _push, _parent, _attrs) {
  7. _push(\`<div\${_ssrRenderAttrs(_attrs)}></div>\`)
  8. }"
  9. `)
  10. })
  11. test('with comments', () => {
  12. expect(compile(`<!--!--><div/>`).code).toMatchInlineSnapshot(`
  13. "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  14. return function ssrRender(_ctx, _push, _parent, _attrs) {
  15. _push(\`<!--[--><!--!--><div\${_ssrRenderAttrs(_attrs)}></div><!--]-->\`)
  16. }"
  17. `)
  18. })
  19. // #5140
  20. test('should not inject to non-single-root if branches', () => {
  21. expect(compile(`<div v-if="true"/><div/>`).code).toMatchInlineSnapshot(`
  22. "
  23. return function ssrRender(_ctx, _push, _parent, _attrs) {
  24. _push(\`<!--[-->\`)
  25. if (true) {
  26. _push(\`<div></div>\`)
  27. } else {
  28. _push(\`<!---->\`)
  29. }
  30. _push(\`<div></div><!--]-->\`)
  31. }"
  32. `)
  33. })
  34. test('fallthrough component content (root with comments)', () => {
  35. expect(compile(`<!--root--><transition><div/></transition>`).code)
  36. .toMatchInlineSnapshot(`
  37. "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  38. return function ssrRender(_ctx, _push, _parent, _attrs) {
  39. _push(\`<!--[--><!--root--><div\${_ssrRenderAttrs(_attrs)}></div><!--]-->\`)
  40. }"
  41. `)
  42. })
  43. //#8072
  44. test(`fallthrough component content (with whitespace: 'preserve')`, () => {
  45. expect(
  46. compile(
  47. `
  48. <a v-if="to">Foo</a>
  49. <a v-else>Bar</a>
  50. `,
  51. {
  52. whitespace: 'preserve',
  53. },
  54. ).code,
  55. ).toMatchInlineSnapshot(`
  56. "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  57. return function ssrRender(_ctx, _push, _parent, _attrs) {
  58. if (_ctx.to) {
  59. _push(\`<a\${_ssrRenderAttrs(_attrs)}>Foo</a>\`)
  60. } else {
  61. _push(\`<a\${_ssrRenderAttrs(_attrs)}>Bar</a>\`)
  62. }
  63. }"
  64. `)
  65. })
  66. test('should not inject to fallthrough component content if not root', () => {
  67. expect(compile(`<div/><transition><div/></transition>`).code)
  68. .toMatchInlineSnapshot(`
  69. "
  70. return function ssrRender(_ctx, _push, _parent, _attrs) {
  71. _push(\`<!--[--><div></div><div></div><!--]-->\`)
  72. }"
  73. `)
  74. })
  75. })