ssrDynamicComponent.spec.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { createApp, createVNode } from 'vue'
  2. import { renderToString } from '../src'
  3. describe('ssr: dynamic component', () => {
  4. test('resolved to component', async () => {
  5. expect(
  6. await renderToString(
  7. createApp({
  8. components: {
  9. one: {
  10. template: `<div><slot/></div>`,
  11. },
  12. },
  13. template: `<component :is="'one'"><span>slot</span></component>`,
  14. }),
  15. ),
  16. ).toBe(`<div><!--[--><span>slot</span><!--]--></div>`)
  17. })
  18. test('resolved to component with v-show', async () => {
  19. expect(
  20. await renderToString(
  21. createApp({
  22. components: {
  23. one: {
  24. template: `<component is="div"><slot/></component>`,
  25. },
  26. },
  27. template: `<one><one v-show="false">hi</one></one>`,
  28. }),
  29. ),
  30. ).toBe(
  31. `<div><!--[--><div style=\"display:none;\"><!--[-->hi<!--]--></div><!--]--></div>`,
  32. )
  33. })
  34. test('resolve to element', async () => {
  35. expect(
  36. await renderToString(
  37. createApp({
  38. template: `<component :is="'p'"><span>slot</span></component>`,
  39. }),
  40. ),
  41. ).toBe(`<p><span>slot</span></p>`)
  42. })
  43. test('resolve to component vnode', async () => {
  44. const Child = {
  45. props: ['id'],
  46. template: `<div>{{ id }}<slot/></div>`,
  47. }
  48. expect(
  49. await renderToString(
  50. createApp({
  51. setup() {
  52. return {
  53. vnode: createVNode(Child, { id: 'test' }),
  54. }
  55. },
  56. template: `<component :is="vnode"><span>slot</span></component>`,
  57. }),
  58. ),
  59. ).toBe(`<div>test<!--[--><span>slot</span><!--]--></div>`)
  60. })
  61. test('resolve to element vnode', async () => {
  62. expect(
  63. await renderToString(
  64. createApp({
  65. setup() {
  66. return {
  67. vnode: createVNode('div', { id: 'test' }),
  68. }
  69. },
  70. template: `<component :is="vnode"><span>slot</span></component>`,
  71. }),
  72. ),
  73. ).toBe(`<div id="test"><span>slot</span></div>`)
  74. })
  75. })