ssrSuspense.spec.ts 3.3 KB

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