ssrSuspense.spec.ts 3.2 KB

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