ssrScopeId.spec.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { createApp, withScopeId } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. import { ssrRenderComponent, ssrRenderAttrs, ssrRenderSlot } from '../src'
  4. describe('ssr: scoped id on component root', () => {
  5. test('basic', async () => {
  6. const withParentId = withScopeId('parent')
  7. const Child = {
  8. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  9. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  10. }
  11. }
  12. const Comp = {
  13. ssrRender: withParentId((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('inside slot', async () => {
  21. const withParentId = withScopeId('parent')
  22. const Child = {
  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. ssrRenderSlot(ctx.$slots, 'default', {}, null, push, parent)
  31. }
  32. }
  33. const Comp = {
  34. ssrRender: withParentId((_: any, push: any, parent: any) => {
  35. push(
  36. ssrRenderComponent(
  37. Wrapper,
  38. null,
  39. {
  40. default: withParentId((_: any, push: any, parent: any) => {
  41. push(ssrRenderComponent(Child, null, null, parent))
  42. }),
  43. _: 1
  44. } as any,
  45. parent
  46. )
  47. )
  48. })
  49. }
  50. const result = await renderToString(createApp(Comp))
  51. expect(result).toBe(`<!--[--><div parent wrapper-s child></div><!--]-->`)
  52. })
  53. })