ssrSuspense.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { createApp, h, Suspense } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. describe('SSR Suspense', () => {
  4. const ResolvingAsync = {
  5. async setup() {
  6. return () => h('div', 'async')
  7. }
  8. }
  9. const RejectingAsync = {
  10. setup() {
  11. return new Promise((_, reject) => reject())
  12. }
  13. }
  14. test('render', async () => {
  15. const Comp = {
  16. render() {
  17. return h(Suspense, null, {
  18. default: h(ResolvingAsync),
  19. fallback: h('div', 'fallback')
  20. })
  21. }
  22. }
  23. expect(await renderToString(createApp(Comp))).toBe(`<div>async</div>`)
  24. })
  25. test('fallback', async () => {
  26. const Comp = {
  27. render() {
  28. return h(Suspense, null, {
  29. default: h(RejectingAsync),
  30. fallback: h('div', 'fallback')
  31. })
  32. }
  33. }
  34. expect(await renderToString(createApp(Comp))).toBe(`<div>fallback</div>`)
  35. })
  36. test('2 components', async () => {
  37. const Comp = {
  38. render() {
  39. return h(Suspense, null, {
  40. default: h('div', [h(ResolvingAsync), h(ResolvingAsync)]),
  41. fallback: h('div', 'fallback')
  42. })
  43. }
  44. }
  45. expect(await renderToString(createApp(Comp))).toBe(
  46. `<div><div>async</div><div>async</div></div>`
  47. )
  48. })
  49. test('resolving component + rejecting component', async () => {
  50. const Comp = {
  51. render() {
  52. return h(Suspense, null, {
  53. default: h('div', [h(ResolvingAsync), h(RejectingAsync)]),
  54. fallback: h('div', 'fallback')
  55. })
  56. }
  57. }
  58. expect(await renderToString(createApp(Comp))).toBe(`<div>fallback</div>`)
  59. })
  60. test('failing suspense in passing suspense', async () => {
  61. const Comp = {
  62. render() {
  63. return h(Suspense, null, {
  64. default: h('div', [
  65. h(ResolvingAsync),
  66. h(Suspense, null, {
  67. default: h('div', [h(RejectingAsync)]),
  68. fallback: h('div', 'fallback 2')
  69. })
  70. ]),
  71. fallback: h('div', 'fallback 1')
  72. })
  73. }
  74. }
  75. expect(await renderToString(createApp(Comp))).toBe(
  76. `<div><div>async</div><div>fallback 2</div></div>`
  77. )
  78. })
  79. test('passing suspense in failing suspense', async () => {
  80. const Comp = {
  81. render() {
  82. return h(Suspense, null, {
  83. default: h('div', [
  84. h(RejectingAsync),
  85. h(Suspense, null, {
  86. default: h('div', [h(ResolvingAsync)]),
  87. fallback: h('div', 'fallback 2')
  88. })
  89. ]),
  90. fallback: h('div', 'fallback 1')
  91. })
  92. }
  93. }
  94. expect(await renderToString(createApp(Comp))).toBe(`<div>fallback 1</div>`)
  95. })
  96. })