ssrElement.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { getCompiledString } from './utils'
  2. import { compile } from '../src'
  3. describe('ssr: element', () => {
  4. test('basic elements', () => {
  5. expect(getCompiledString(`<div></div>`)).toMatchInlineSnapshot(
  6. `"\`<div></div>\`"`
  7. )
  8. expect(getCompiledString(`<div/>`)).toMatchInlineSnapshot(
  9. `"\`<div></div>\`"`
  10. )
  11. })
  12. test('nested elements', () => {
  13. expect(
  14. getCompiledString(`<div><span></span><span></span></div>`)
  15. ).toMatchInlineSnapshot(`"\`<div><span></span><span></span></div>\`"`)
  16. })
  17. test('void element', () => {
  18. expect(getCompiledString(`<input>`)).toMatchInlineSnapshot(`"\`<input>\`"`)
  19. })
  20. describe('children override', () => {
  21. test('v-html', () => {
  22. expect(getCompiledString(`<div v-html="foo"/>`)).toMatchInlineSnapshot(
  23. `"\`<div>\${_ctx.foo}</div>\`"`
  24. )
  25. })
  26. test('v-text', () => {
  27. expect(getCompiledString(`<div v-text="foo"/>`)).toMatchInlineSnapshot(
  28. `"\`<div>\${_ssrInterpolate(_ctx.foo)}</div>\`"`
  29. )
  30. })
  31. test('<textarea> with dynamic value', () => {
  32. expect(
  33. getCompiledString(`<textarea :value="foo"/>`)
  34. ).toMatchInlineSnapshot(
  35. `"\`<textarea>\${_ssrInterpolate(_ctx.foo)}</textarea>\`"`
  36. )
  37. })
  38. test('<textarea> with static value', () => {
  39. expect(
  40. getCompiledString(`<textarea value="fo&gt;o"/>`)
  41. ).toMatchInlineSnapshot(`"\`<textarea>fo&gt;o</textarea>\`"`)
  42. })
  43. test('<textarea> with dynamic v-bind', () => {
  44. expect(compile(`<textarea v-bind="obj">fallback</textarea>`).code)
  45. .toMatchInlineSnapshot(`
  46. "const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
  47. return function ssrRender(_ctx, _push, _parent) {
  48. let _temp0
  49. _push(\`<textarea\${
  50. _ssrRenderAttrs(_temp0 = _ctx.obj)
  51. }>\${
  52. _ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"fallback\\")
  53. }</textarea>\`)
  54. }"
  55. `)
  56. })
  57. })
  58. describe('attrs', () => {
  59. test('static attrs', () => {
  60. expect(
  61. getCompiledString(`<div id="foo" class="bar"></div>`)
  62. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
  63. })
  64. test('v-bind:class', () => {
  65. expect(
  66. getCompiledString(`<div id="foo" :class="bar"></div>`)
  67. ).toMatchInlineSnapshot(
  68. `"\`<div id=\\"foo\\"\${_ssrRenderClass(_ctx.bar)}></div>\`"`
  69. )
  70. })
  71. test('static class + v-bind:class', () => {
  72. expect(
  73. getCompiledString(`<div class="foo" :class="bar"></div>`)
  74. ).toMatchInlineSnapshot(
  75. `"\`<div\${_ssrRenderClass([_ctx.bar, \\"foo\\"])}></div>\`"`
  76. )
  77. })
  78. test('v-bind:style', () => {
  79. expect(
  80. getCompiledString(`<div id="foo" :style="bar"></div>`)
  81. ).toMatchInlineSnapshot(
  82. `"\`<div id=\\"foo\\"\${_ssrRenderStyle(_ctx.bar)}></div>\`"`
  83. )
  84. })
  85. test('static style + v-bind:style', () => {
  86. expect(
  87. getCompiledString(`<div style="color:red;" :style="bar"></div>`)
  88. ).toMatchInlineSnapshot(
  89. `"\`<div\${_ssrRenderStyle([_hoisted_1, _ctx.bar])}></div>\`"`
  90. )
  91. })
  92. test('v-bind:key (boolean)', () => {
  93. expect(
  94. getCompiledString(`<input type="checkbox" :checked="checked">`)
  95. ).toMatchInlineSnapshot(
  96. `"\`<input type=\\"checkbox\\"\${(_ctx.checked) ? \\" checked\\" : \\"\\"}>\`"`
  97. )
  98. })
  99. test('v-bind:key (non-boolean)', () => {
  100. expect(
  101. getCompiledString(`<div :id="id" class="bar"></div>`)
  102. ).toMatchInlineSnapshot(
  103. `"\`<div\${_ssrRenderAttr(\\"id\\", _ctx.id)} class=\\"bar\\"></div>\`"`
  104. )
  105. })
  106. test('v-bind:[key]', () => {
  107. expect(
  108. getCompiledString(`<div v-bind:[key]="value"></div>`)
  109. ).toMatchInlineSnapshot(
  110. `"\`<div\${_ssrRenderAttrs({ [_ctx.key]: _ctx.value })}></div>\`"`
  111. )
  112. expect(getCompiledString(`<div class="foo" v-bind:[key]="value"></div>`))
  113. .toMatchInlineSnapshot(`
  114. "\`<div\${_ssrRenderAttrs({
  115. class: \\"foo\\",
  116. [_ctx.key]: _ctx.value
  117. })}></div>\`"
  118. `)
  119. expect(getCompiledString(`<div :id="id" v-bind:[key]="value"></div>`))
  120. .toMatchInlineSnapshot(`
  121. "\`<div\${_ssrRenderAttrs({
  122. id: _ctx.id,
  123. [_ctx.key]: _ctx.value
  124. })}></div>\`"
  125. `)
  126. })
  127. test('v-bind="obj"', () => {
  128. expect(
  129. getCompiledString(`<div v-bind="obj"></div>`)
  130. ).toMatchInlineSnapshot(`"\`<div\${_ssrRenderAttrs(_ctx.obj)}></div>\`"`)
  131. expect(
  132. getCompiledString(`<div class="foo" v-bind="obj"></div>`)
  133. ).toMatchInlineSnapshot(
  134. `"\`<div\${_ssrRenderAttrs(_mergeProps({ class: \\"foo\\" }, _ctx.obj))}></div>\`"`
  135. )
  136. expect(
  137. getCompiledString(`<div :id="id" v-bind="obj"></div>`)
  138. ).toMatchInlineSnapshot(
  139. `"\`<div\${_ssrRenderAttrs(_mergeProps({ id: _ctx.id }, _ctx.obj))}></div>\`"`
  140. )
  141. // dynamic key + v-bind="object"
  142. expect(
  143. getCompiledString(`<div :[key]="id" v-bind="obj"></div>`)
  144. ).toMatchInlineSnapshot(
  145. `"\`<div\${_ssrRenderAttrs(_mergeProps({ [_ctx.key]: _ctx.id }, _ctx.obj))}></div>\`"`
  146. )
  147. // should merge class and :class
  148. expect(getCompiledString(`<div class="a" :class="b" v-bind="obj"></div>`))
  149. .toMatchInlineSnapshot(`
  150. "\`<div\${_ssrRenderAttrs(_mergeProps({
  151. class: [\\"a\\", _ctx.b]
  152. }, _ctx.obj))}></div>\`"
  153. `)
  154. // should merge style and :style
  155. expect(
  156. getCompiledString(
  157. `<div style="color:red;" :style="b" v-bind="obj"></div>`
  158. )
  159. ).toMatchInlineSnapshot(`
  160. "\`<div\${_ssrRenderAttrs(_mergeProps({
  161. style: [_hoisted_1, _ctx.b]
  162. }, _ctx.obj))}></div>\`"
  163. `)
  164. })
  165. test('should ignore v-on', () => {
  166. expect(
  167. getCompiledString(`<div id="foo" @click="bar"/>`)
  168. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  169. expect(
  170. getCompiledString(`<div id="foo" v-on="bar"/>`)
  171. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  172. expect(
  173. getCompiledString(`<div v-bind="foo" v-on="bar"/>`)
  174. ).toMatchInlineSnapshot(`"\`<div\${_ssrRenderAttrs(_ctx.foo)}></div>\`"`)
  175. })
  176. })
  177. })