ssrElement.spec.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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>\${
  24. _ctx.foo
  25. }</div>\`"
  26. `)
  27. })
  28. test('v-text', () => {
  29. expect(getCompiledString(`<div v-text="foo"/>`)).toMatchInlineSnapshot(`
  30. "\`<div>\${
  31. _ssrInterpolate(_ctx.foo)
  32. }</div>\`"
  33. `)
  34. })
  35. test('<textarea> with dynamic value', () => {
  36. expect(getCompiledString(`<textarea :value="foo"/>`))
  37. .toMatchInlineSnapshot(`
  38. "\`<textarea>\${
  39. _ssrInterpolate(_ctx.foo)
  40. }</textarea>\`"
  41. `)
  42. })
  43. test('<textarea> with static value', () => {
  44. expect(
  45. getCompiledString(`<textarea value="fo&gt;o"/>`)
  46. ).toMatchInlineSnapshot(`"\`<textarea>fo&gt;o</textarea>\`"`)
  47. })
  48. test('<textarea> with dynamic v-bind', () => {
  49. expect(compile(`<textarea v-bind="obj">fallback</textarea>`).code)
  50. .toMatchInlineSnapshot(`
  51. "const { mergeProps: _mergeProps } = require(\\"vue\\")
  52. const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
  53. return function ssrRender(_ctx, _push, _parent, _attrs) {
  54. let _temp0
  55. _push(\`<textarea\${
  56. _ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, _attrs), \\"textarea\\")
  57. }>\${
  58. _ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"fallback\\")
  59. }</textarea>\`)
  60. }"
  61. `)
  62. })
  63. test('should pass tag to custom elements w/ dynamic v-bind', () => {
  64. expect(
  65. compile(`<my-foo v-bind="obj"></my-foo>`, {
  66. isCustomElement: () => true
  67. }).code
  68. ).toMatchInlineSnapshot(`
  69. "const { mergeProps: _mergeProps } = require(\\"vue\\")
  70. const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
  71. return function ssrRender(_ctx, _push, _parent, _attrs) {
  72. _push(\`<my-foo\${_ssrRenderAttrs(_mergeProps(_ctx.obj, _attrs), \\"my-foo\\")}></my-foo>\`)
  73. }"
  74. `)
  75. })
  76. })
  77. describe('attrs', () => {
  78. test('static attrs', () => {
  79. expect(
  80. getCompiledString(`<div id="foo" class="bar"></div>`)
  81. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
  82. })
  83. test('v-bind:class', () => {
  84. expect(getCompiledString(`<div id="foo" :class="bar"></div>`))
  85. .toMatchInlineSnapshot(`
  86. "\`<div id=\\"foo\\" class=\\"\${
  87. _ssrRenderClass(_ctx.bar)
  88. }\\"></div>\`"
  89. `)
  90. })
  91. test('static class + v-bind:class', () => {
  92. expect(getCompiledString(`<div class="foo" :class="bar"></div>`))
  93. .toMatchInlineSnapshot(`
  94. "\`<div class=\\"\${
  95. _ssrRenderClass([_ctx.bar, \\"foo\\"])
  96. }\\"></div>\`"
  97. `)
  98. })
  99. test('v-bind:class + static class', () => {
  100. expect(getCompiledString(`<div :class="bar" class="foo"></div>`))
  101. .toMatchInlineSnapshot(`
  102. "\`<div class=\\"\${
  103. _ssrRenderClass([_ctx.bar, \\"foo\\"])
  104. }\\"></div>\`"
  105. `)
  106. })
  107. test('v-bind:style', () => {
  108. expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
  109. .toMatchInlineSnapshot(`
  110. "\`<div id=\\"foo\\" style=\\"\${
  111. _ssrRenderStyle(_ctx.bar)
  112. }\\"></div>\`"
  113. `)
  114. })
  115. test('static style + v-bind:style', () => {
  116. expect(getCompiledString(`<div style="color:red;" :style="bar"></div>`))
  117. .toMatchInlineSnapshot(`
  118. "\`<div style=\\"\${
  119. _ssrRenderStyle([{\\"color\\":\\"red\\"}, _ctx.bar])
  120. }\\"></div>\`"
  121. `)
  122. })
  123. test('v-bind:key (boolean)', () => {
  124. expect(getCompiledString(`<input type="checkbox" :checked="checked">`))
  125. .toMatchInlineSnapshot(`
  126. "\`<input type=\\"checkbox\\"\${
  127. (_ctx.checked) ? \\" checked\\" : \\"\\"
  128. }>\`"
  129. `)
  130. })
  131. test('v-bind:key (non-boolean)', () => {
  132. expect(getCompiledString(`<div :id="id" class="bar"></div>`))
  133. .toMatchInlineSnapshot(`
  134. "\`<div\${
  135. _ssrRenderAttr(\\"id\\", _ctx.id)
  136. } class=\\"bar\\"></div>\`"
  137. `)
  138. })
  139. test('v-bind:[key]', () => {
  140. expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
  141. .toMatchInlineSnapshot(`
  142. "\`<div\${
  143. _ssrRenderAttrs({ [_ctx.key || \\"\\"]: _ctx.value })
  144. }></div>\`"
  145. `)
  146. expect(getCompiledString(`<div class="foo" v-bind:[key]="value"></div>`))
  147. .toMatchInlineSnapshot(`
  148. "\`<div\${
  149. _ssrRenderAttrs({
  150. class: \\"foo\\",
  151. [_ctx.key || \\"\\"]: _ctx.value
  152. })
  153. }></div>\`"
  154. `)
  155. expect(getCompiledString(`<div :id="id" v-bind:[key]="value"></div>`))
  156. .toMatchInlineSnapshot(`
  157. "\`<div\${
  158. _ssrRenderAttrs({
  159. id: _ctx.id,
  160. [_ctx.key || \\"\\"]: _ctx.value
  161. })
  162. }></div>\`"
  163. `)
  164. })
  165. test('v-bind="obj"', () => {
  166. expect(getCompiledString(`<div v-bind="obj"></div>`))
  167. .toMatchInlineSnapshot(`
  168. "\`<div\${
  169. _ssrRenderAttrs(_ctx.obj)
  170. }></div>\`"
  171. `)
  172. expect(getCompiledString(`<div class="foo" v-bind="obj"></div>`))
  173. .toMatchInlineSnapshot(`
  174. "\`<div\${
  175. _ssrRenderAttrs(_mergeProps({ class: \\"foo\\" }, _ctx.obj))
  176. }></div>\`"
  177. `)
  178. expect(getCompiledString(`<div :id="id" v-bind="obj"></div>`))
  179. .toMatchInlineSnapshot(`
  180. "\`<div\${
  181. _ssrRenderAttrs(_mergeProps({ id: _ctx.id }, _ctx.obj))
  182. }></div>\`"
  183. `)
  184. // dynamic key + v-bind="object"
  185. expect(getCompiledString(`<div :[key]="id" v-bind="obj"></div>`))
  186. .toMatchInlineSnapshot(`
  187. "\`<div\${
  188. _ssrRenderAttrs(_mergeProps({ [_ctx.key || \\"\\"]: _ctx.id }, _ctx.obj))
  189. }></div>\`"
  190. `)
  191. // should merge class and :class
  192. expect(getCompiledString(`<div class="a" :class="b" v-bind="obj"></div>`))
  193. .toMatchInlineSnapshot(`
  194. "\`<div\${
  195. _ssrRenderAttrs(_mergeProps({
  196. class: [\\"a\\", _ctx.b]
  197. }, _ctx.obj))
  198. }></div>\`"
  199. `)
  200. // should merge style and :style
  201. expect(
  202. getCompiledString(
  203. `<div style="color:red;" :style="b" v-bind="obj"></div>`
  204. )
  205. ).toMatchInlineSnapshot(`
  206. "\`<div\${
  207. _ssrRenderAttrs(_mergeProps({
  208. style: [{\\"color\\":\\"red\\"}, _ctx.b]
  209. }, _ctx.obj))
  210. }></div>\`"
  211. `)
  212. })
  213. test('should ignore v-on', () => {
  214. expect(
  215. getCompiledString(`<div id="foo" @click="bar"/>`)
  216. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  217. expect(
  218. getCompiledString(`<div id="foo" v-on="bar"/>`)
  219. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  220. expect(getCompiledString(`<div v-bind="foo" v-on="bar"/>`))
  221. .toMatchInlineSnapshot(`
  222. "\`<div\${
  223. _ssrRenderAttrs(_ctx.foo)
  224. }></div>\`"
  225. `)
  226. })
  227. })
  228. })