ssrSlot.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @jest-environment node
  3. */
  4. import { createApp } from 'vue'
  5. import { renderToString } from '../src/renderToString'
  6. const components = {
  7. one: {
  8. template: `<div><slot/></div>`
  9. }
  10. }
  11. describe('ssr: slot', () => {
  12. test('text slot', async () => {
  13. expect(
  14. await renderToString(
  15. createApp({
  16. components,
  17. template: `<one>hello</one>`
  18. })
  19. )
  20. ).toBe(`<div><!--[-->hello<!--]--></div>`)
  21. })
  22. test('element slot', async () => {
  23. expect(
  24. await renderToString(
  25. createApp({
  26. components,
  27. template: `<one><div>hi</div></one>`
  28. })
  29. )
  30. ).toBe(`<div><!--[--><div>hi</div><!--]--></div>`)
  31. })
  32. test('empty slot', async () => {
  33. expect(
  34. await renderToString(
  35. createApp({
  36. components: {
  37. one: {
  38. template: `<div><slot/></div>`
  39. }
  40. },
  41. template: `<one><template v-if="false"/></one>`
  42. })
  43. )
  44. ).toBe(`<div><!--[--><!--]--></div>`)
  45. })
  46. test('empty slot (manual comments)', async () => {
  47. expect(
  48. await renderToString(
  49. createApp({
  50. components: {
  51. one: {
  52. template: `<div><slot/></div>`
  53. }
  54. },
  55. template: `<one><!--hello--></one>`
  56. })
  57. )
  58. ).toBe(`<div><!--[--><!--]--></div>`)
  59. })
  60. test('empty slot (multi-line comments)', async () => {
  61. expect(
  62. await renderToString(
  63. createApp({
  64. components: {
  65. one: {
  66. template: `<div><slot/></div>`
  67. }
  68. },
  69. template: `<one><!--he\nllo--></one>`
  70. })
  71. )
  72. ).toBe(`<div><!--[--><!--]--></div>`)
  73. })
  74. test('multiple elements', async () => {
  75. expect(
  76. await renderToString(
  77. createApp({
  78. components,
  79. template: `<one><div>one</div><div>two</div></one>`
  80. })
  81. )
  82. ).toBe(`<div><!--[--><div>one</div><div>two</div><!--]--></div>`)
  83. })
  84. test('fragment slot (template v-if)', async () => {
  85. expect(
  86. await renderToString(
  87. createApp({
  88. components,
  89. template: `<one><template v-if="true">hello</template></one>`
  90. })
  91. )
  92. ).toBe(`<div><!--[--><!--[-->hello<!--]--><!--]--></div>`)
  93. })
  94. test('fragment slot (template v-if + multiple elements)', async () => {
  95. expect(
  96. await renderToString(
  97. createApp({
  98. components,
  99. template: `<one><template v-if="true"><div>one</div><div>two</div></template></one>`
  100. })
  101. )
  102. ).toBe(
  103. `<div><!--[--><!--[--><div>one</div><div>two</div><!--]--><!--]--></div>`
  104. )
  105. })
  106. })