ssrVShow.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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({
  12. style: (_ctx.foo) ? null : { display: \\"none\\" }
  13. }, _attrs))}></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 v-bind', () => {
  84. expect(
  85. compileWithWrapper(
  86. `<div v-bind="baz" style="color:red" :style="{ fontSize: 14 }" v-show="foo"/>`
  87. ).code
  88. ).toMatchInlineSnapshot(`
  89. "const { mergeProps: _mergeProps } = require(\\"vue\\")
  90. const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
  91. return function ssrRender(_ctx, _push, _parent, _attrs) {
  92. _push(\`<div\${
  93. _ssrRenderAttrs(_attrs)
  94. }><div\${
  95. _ssrRenderAttrs(_mergeProps(_ctx.baz, {
  96. style: [
  97. {\\"color\\":\\"red\\"},
  98. { fontSize: 14 },
  99. (_ctx.foo) ? null : { display: \\"none\\" }
  100. ]
  101. }))
  102. }></div></div>\`)
  103. }"
  104. `)
  105. })
  106. })