ssrVShow.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { compile } from '../src'
  2. function compileWithWrapper(src: string) {
  3. return compile(`<div>${src}</div>`)
  4. }
  5. describe('ssr: v-show', () => {
  6. test('basic as root', () => {
  7. expect(compile(`<div v-show="foo"/>`).code).toMatchInlineSnapshot(`
  8. "const { mergeProps: _mergeProps } = require("vue")
  9. const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  10. return function ssrRender(_ctx, _push, _parent, _attrs) {
  11. _push(\`<div\${_ssrRenderAttrs(_mergeProps(_attrs, {
  12. style: (_ctx.foo) ? null : { display: "none" }
  13. }))}></div>\`)
  14. }"
  15. `)
  16. })
  17. test('basic', () => {
  18. expect(compileWithWrapper(`<div v-show="foo"/>`).code)
  19. .toMatchInlineSnapshot(`
  20. "const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  21. return function ssrRender(_ctx, _push, _parent, _attrs) {
  22. _push(\`<div\${
  23. _ssrRenderAttrs(_attrs)
  24. }><div style="\${
  25. _ssrRenderStyle((_ctx.foo) ? null : { display: "none" })
  26. }"></div></div>\`)
  27. }"
  28. `)
  29. })
  30. test('with static style', () => {
  31. expect(compileWithWrapper(`<div style="color:red" v-show="foo"/>`).code)
  32. .toMatchInlineSnapshot(`
  33. "const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  34. return function ssrRender(_ctx, _push, _parent, _attrs) {
  35. _push(\`<div\${
  36. _ssrRenderAttrs(_attrs)
  37. }><div style="\${
  38. _ssrRenderStyle([
  39. {"color":"red"},
  40. (_ctx.foo) ? null : { display: "none" }
  41. ])
  42. }"></div></div>\`)
  43. }"
  44. `)
  45. })
  46. test('with dynamic style', () => {
  47. expect(
  48. compileWithWrapper(`<div :style="{ color: 'red' }" v-show="foo"/>`).code,
  49. ).toMatchInlineSnapshot(`
  50. "const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  51. return function ssrRender(_ctx, _push, _parent, _attrs) {
  52. _push(\`<div\${
  53. _ssrRenderAttrs(_attrs)
  54. }><div style="\${
  55. _ssrRenderStyle([
  56. { color: 'red' },
  57. (_ctx.foo) ? null : { display: "none" }
  58. ])
  59. }"></div></div>\`)
  60. }"
  61. `)
  62. })
  63. test('with static + dynamic style', () => {
  64. expect(
  65. compileWithWrapper(
  66. `<div style="color:red" :style="{ fontSize: 14 }" v-show="foo"/>`,
  67. ).code,
  68. ).toMatchInlineSnapshot(`
  69. "const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  70. return function ssrRender(_ctx, _push, _parent, _attrs) {
  71. _push(\`<div\${
  72. _ssrRenderAttrs(_attrs)
  73. }><div style="\${
  74. _ssrRenderStyle([
  75. {"color":"red"},
  76. { fontSize: 14 },
  77. (_ctx.foo) ? null : { display: "none" }
  78. ])
  79. }"></div></div>\`)
  80. }"
  81. `)
  82. })
  83. test('with style + display', () => {
  84. expect(compileWithWrapper(`<div v-show="foo" style="display:flex" />`).code)
  85. .toMatchInlineSnapshot(`
  86. "const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  87. return function ssrRender(_ctx, _push, _parent, _attrs) {
  88. _push(\`<div\${
  89. _ssrRenderAttrs(_attrs)
  90. }><div style="\${
  91. _ssrRenderStyle([
  92. {"display":"flex"},
  93. (_ctx.foo) ? null : { display: "none" }
  94. ])
  95. }"></div></div>\`)
  96. }"
  97. `)
  98. })
  99. test('with v-bind', () => {
  100. expect(
  101. compileWithWrapper(
  102. `<div v-bind="baz" style="color:red" :style="{ fontSize: 14 }" v-show="foo"/>`,
  103. ).code,
  104. ).toMatchInlineSnapshot(`
  105. "const { mergeProps: _mergeProps } = require("vue")
  106. const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
  107. return function ssrRender(_ctx, _push, _parent, _attrs) {
  108. _push(\`<div\${
  109. _ssrRenderAttrs(_attrs)
  110. }><div\${
  111. _ssrRenderAttrs(_mergeProps(_ctx.baz, {
  112. style: [
  113. {"color":"red"},
  114. { fontSize: 14 },
  115. (_ctx.foo) ? null : { display: "none" }
  116. ]
  117. }))
  118. }></div></div>\`)
  119. }"
  120. `)
  121. })
  122. })