ssrElement.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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:style', () => {
  100. expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
  101. .toMatchInlineSnapshot(`
  102. "\`<div id=\\"foo\\" style=\\"\${
  103. _ssrRenderStyle(_ctx.bar)
  104. }\\"></div>\`"
  105. `)
  106. })
  107. test('static style + v-bind:style', () => {
  108. expect(getCompiledString(`<div style="color:red;" :style="bar"></div>`))
  109. .toMatchInlineSnapshot(`
  110. "\`<div style=\\"\${
  111. _ssrRenderStyle([{\\"color\\":\\"red\\"}, _ctx.bar])
  112. }\\"></div>\`"
  113. `)
  114. })
  115. test('v-bind:key (boolean)', () => {
  116. expect(getCompiledString(`<input type="checkbox" :checked="checked">`))
  117. .toMatchInlineSnapshot(`
  118. "\`<input type=\\"checkbox\\"\${
  119. (_ctx.checked) ? \\" checked\\" : \\"\\"
  120. }>\`"
  121. `)
  122. })
  123. test('v-bind:key (non-boolean)', () => {
  124. expect(getCompiledString(`<div :id="id" class="bar"></div>`))
  125. .toMatchInlineSnapshot(`
  126. "\`<div\${
  127. _ssrRenderAttr(\\"id\\", _ctx.id)
  128. } class=\\"bar\\"></div>\`"
  129. `)
  130. })
  131. test('v-bind:[key]', () => {
  132. expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
  133. .toMatchInlineSnapshot(`
  134. "\`<div\${
  135. _ssrRenderAttrs({ [_ctx.key]: _ctx.value })
  136. }></div>\`"
  137. `)
  138. expect(getCompiledString(`<div class="foo" v-bind:[key]="value"></div>`))
  139. .toMatchInlineSnapshot(`
  140. "\`<div\${
  141. _ssrRenderAttrs({
  142. class: \\"foo\\",
  143. [_ctx.key]: _ctx.value
  144. })
  145. }></div>\`"
  146. `)
  147. expect(getCompiledString(`<div :id="id" v-bind:[key]="value"></div>`))
  148. .toMatchInlineSnapshot(`
  149. "\`<div\${
  150. _ssrRenderAttrs({
  151. id: _ctx.id,
  152. [_ctx.key]: _ctx.value
  153. })
  154. }></div>\`"
  155. `)
  156. })
  157. test('v-bind="obj"', () => {
  158. expect(getCompiledString(`<div v-bind="obj"></div>`))
  159. .toMatchInlineSnapshot(`
  160. "\`<div\${
  161. _ssrRenderAttrs(_ctx.obj)
  162. }></div>\`"
  163. `)
  164. expect(getCompiledString(`<div class="foo" v-bind="obj"></div>`))
  165. .toMatchInlineSnapshot(`
  166. "\`<div\${
  167. _ssrRenderAttrs(_mergeProps({ class: \\"foo\\" }, _ctx.obj))
  168. }></div>\`"
  169. `)
  170. expect(getCompiledString(`<div :id="id" v-bind="obj"></div>`))
  171. .toMatchInlineSnapshot(`
  172. "\`<div\${
  173. _ssrRenderAttrs(_mergeProps({ id: _ctx.id }, _ctx.obj))
  174. }></div>\`"
  175. `)
  176. // dynamic key + v-bind="object"
  177. expect(getCompiledString(`<div :[key]="id" v-bind="obj"></div>`))
  178. .toMatchInlineSnapshot(`
  179. "\`<div\${
  180. _ssrRenderAttrs(_mergeProps({ [_ctx.key]: _ctx.id }, _ctx.obj))
  181. }></div>\`"
  182. `)
  183. // should merge class and :class
  184. expect(getCompiledString(`<div class="a" :class="b" v-bind="obj"></div>`))
  185. .toMatchInlineSnapshot(`
  186. "\`<div\${
  187. _ssrRenderAttrs(_mergeProps({
  188. class: [\\"a\\", _ctx.b]
  189. }, _ctx.obj))
  190. }></div>\`"
  191. `)
  192. // should merge style and :style
  193. expect(
  194. getCompiledString(
  195. `<div style="color:red;" :style="b" v-bind="obj"></div>`
  196. )
  197. ).toMatchInlineSnapshot(`
  198. "\`<div\${
  199. _ssrRenderAttrs(_mergeProps({
  200. style: [{\\"color\\":\\"red\\"}, _ctx.b]
  201. }, _ctx.obj))
  202. }></div>\`"
  203. `)
  204. })
  205. test('should ignore v-on', () => {
  206. expect(
  207. getCompiledString(`<div id="foo" @click="bar"/>`)
  208. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  209. expect(
  210. getCompiledString(`<div id="foo" v-on="bar"/>`)
  211. ).toMatchInlineSnapshot(`"\`<div id=\\"foo\\"></div>\`"`)
  212. expect(getCompiledString(`<div v-bind="foo" v-on="bar"/>`))
  213. .toMatchInlineSnapshot(`
  214. "\`<div\${
  215. _ssrRenderAttrs(_ctx.foo)
  216. }></div>\`"
  217. `)
  218. })
  219. })
  220. })