ssrScopeId.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { createApp, h, mergeProps, withCtx } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. import { ssrRenderComponent, ssrRenderAttrs, ssrRenderSlot } from '../src'
  4. describe('ssr: scopedId runtime behavior', () => {
  5. test('id on component root', async () => {
  6. const Child = {
  7. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  8. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  9. }
  10. }
  11. const Comp = {
  12. __scopeId: 'parent',
  13. ssrRender: (ctx: any, push: any, parent: any) => {
  14. push(ssrRenderComponent(Child), null, null, parent)
  15. }
  16. }
  17. const result = await renderToString(createApp(Comp))
  18. expect(result).toBe(`<div parent></div>`)
  19. })
  20. test('id and :slotted on component root', async () => {
  21. const Child = {
  22. // <div></div>
  23. ssrRender: (_: any, push: any, _parent: any, attrs: any) => {
  24. push(`<div${ssrRenderAttrs(attrs)} child></div>`)
  25. }
  26. }
  27. const Wrapper = {
  28. __scopeId: 'wrapper',
  29. ssrRender: (ctx: any, push: any, parent: any) => {
  30. // <slot/>
  31. ssrRenderSlot(
  32. ctx.$slots,
  33. 'default',
  34. {},
  35. null,
  36. push,
  37. parent,
  38. 'wrapper-s'
  39. )
  40. }
  41. }
  42. const Comp = {
  43. __scopeId: 'parent',
  44. ssrRender: (_: any, push: any, parent: any) => {
  45. // <Wrapper><Child/></Wrapper>
  46. push(
  47. ssrRenderComponent(
  48. Wrapper,
  49. null,
  50. {
  51. default: withCtx(
  52. (_: any, push: any, parent: any, scopeId: string) => {
  53. push(ssrRenderComponent(Child, null, null, parent, scopeId))
  54. }
  55. ),
  56. _: 1
  57. } as any,
  58. parent
  59. )
  60. )
  61. }
  62. }
  63. const result = await renderToString(createApp(Comp))
  64. expect(result).toBe(`<!--[--><div parent wrapper-s child></div><!--]-->`)
  65. })
  66. // #2892
  67. test(':slotted on forwarded slots', async () => {
  68. const Wrapper = {
  69. __scopeId: 'wrapper',
  70. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  71. // <div class="wrapper"><slot/></div>
  72. push(
  73. `<div${ssrRenderAttrs(
  74. mergeProps({ class: 'wrapper' }, attrs)
  75. )} wrapper>`
  76. )
  77. ssrRenderSlot(
  78. ctx.$slots,
  79. 'default',
  80. {},
  81. null,
  82. push,
  83. parent,
  84. 'wrapper-s'
  85. )
  86. push(`</div>`)
  87. }
  88. }
  89. const Slotted = {
  90. __scopeId: 'slotted',
  91. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  92. // <Wrapper><slot/></Wrapper>
  93. push(
  94. ssrRenderComponent(
  95. Wrapper,
  96. attrs,
  97. {
  98. default: withCtx(
  99. (_: any, push: any, parent: any, scopeId: string) => {
  100. ssrRenderSlot(
  101. ctx.$slots,
  102. 'default',
  103. {},
  104. null,
  105. push,
  106. parent,
  107. 'slotted-s' + scopeId
  108. )
  109. }
  110. ),
  111. _: 1
  112. } as any,
  113. parent
  114. )
  115. )
  116. }
  117. }
  118. const Root = {
  119. __scopeId: 'root',
  120. // <Slotted><div></div></Slotted>
  121. ssrRender: (_: any, push: any, parent: any, attrs: any) => {
  122. push(
  123. ssrRenderComponent(
  124. Slotted,
  125. attrs,
  126. {
  127. default: withCtx(
  128. (_: any, push: any, parent: any, scopeId: string) => {
  129. push(`<div root${scopeId}></div>`)
  130. }
  131. ),
  132. _: 1
  133. } as any,
  134. parent
  135. )
  136. )
  137. }
  138. }
  139. const result = await renderToString(createApp(Root))
  140. expect(result).toBe(
  141. `<div class="wrapper" root slotted wrapper>` +
  142. `<!--[--><!--[--><div root slotted-s wrapper-s></div><!--]--><!--]-->` +
  143. `</div>`
  144. )
  145. })
  146. // #3513
  147. test('scopeId inheritance across ssr-compiled and on-ssr compiled parent chain', async () => {
  148. const Child = {
  149. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  150. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  151. }
  152. }
  153. const Middle = {
  154. render() {
  155. return h(Child)
  156. }
  157. }
  158. const Comp = {
  159. __scopeId: 'parent',
  160. ssrRender: (ctx: any, push: any, parent: any) => {
  161. push(ssrRenderComponent(Middle, null, null, parent))
  162. }
  163. }
  164. const result = await renderToString(createApp(Comp)) // output: `<div></div>`
  165. expect(result).toBe(`<div parent></div>`)
  166. })
  167. })