ssrElement.spec.ts 5.7 KB

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