ssrSuspense.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { createApp, h, Suspense } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. import { ssrRenderSuspense } from '../src/helpers/ssrRenderSuspense'
  4. import { ssrRenderComponent } from '../src'
  5. describe('SSR Suspense', () => {
  6. let logError: jest.SpyInstance
  7. beforeEach(() => {
  8. logError = jest.spyOn(console, 'error').mockImplementation(() => {})
  9. })
  10. afterEach(() => {
  11. logError.mockRestore()
  12. })
  13. const ResolvingAsync = {
  14. async setup() {
  15. return () => h('div', 'async')
  16. }
  17. }
  18. const RejectingAsync = {
  19. setup() {
  20. return new Promise((_, reject) => reject('foo'))
  21. }
  22. }
  23. describe('compiled', () => {
  24. test('basic', async () => {
  25. const app = createApp({
  26. ssrRender(_ctx, _push) {
  27. _push(
  28. ssrRenderSuspense({
  29. default: _push => {
  30. _push('<div>async</div>')
  31. }
  32. })
  33. )
  34. }
  35. })
  36. expect(await renderToString(app)).toBe(`<div>async</div>`)
  37. expect(logError).not.toHaveBeenCalled()
  38. })
  39. test('with async component', async () => {
  40. const app = createApp({
  41. ssrRender(_ctx, _push) {
  42. _push(
  43. ssrRenderSuspense({
  44. default: _push => {
  45. _push(ssrRenderComponent(ResolvingAsync))
  46. }
  47. })
  48. )
  49. }
  50. })
  51. expect(await renderToString(app)).toBe(`<div>async</div>`)
  52. expect(logError).not.toHaveBeenCalled()
  53. })
  54. test('fallback', async () => {
  55. const app = createApp({
  56. ssrRender(_ctx, _push) {
  57. _push(
  58. ssrRenderSuspense({
  59. default: _push => {
  60. _push(ssrRenderComponent(RejectingAsync))
  61. },
  62. fallback: _push => {
  63. _push('<div>fallback</div>')
  64. }
  65. })
  66. )
  67. }
  68. })
  69. expect(await renderToString(app)).toBe(`<div>fallback</div>`)
  70. expect(logError).toHaveBeenCalled()
  71. })
  72. })
  73. describe('vnode', () => {
  74. test('content', async () => {
  75. const Comp = {
  76. render() {
  77. return h(Suspense, null, {
  78. default: h(ResolvingAsync),
  79. fallback: h('div', 'fallback')
  80. })
  81. }
  82. }
  83. expect(await renderToString(createApp(Comp))).toBe(`<div>async</div>`)
  84. expect(logError).not.toHaveBeenCalled()
  85. })
  86. test('fallback', async () => {
  87. const Comp = {
  88. render() {
  89. return h(Suspense, null, {
  90. default: h(RejectingAsync),
  91. fallback: h('div', 'fallback')
  92. })
  93. }
  94. }
  95. expect(await renderToString(createApp(Comp))).toBe(`<div>fallback</div>`)
  96. expect(logError).toHaveBeenCalled()
  97. })
  98. test('2 components', async () => {
  99. const Comp = {
  100. render() {
  101. return h(Suspense, null, {
  102. default: h('div', [h(ResolvingAsync), h(ResolvingAsync)]),
  103. fallback: h('div', 'fallback')
  104. })
  105. }
  106. }
  107. expect(await renderToString(createApp(Comp))).toBe(
  108. `<div><div>async</div><div>async</div></div>`
  109. )
  110. expect(logError).not.toHaveBeenCalled()
  111. })
  112. test('resolving component + rejecting component', async () => {
  113. const Comp = {
  114. render() {
  115. return h(Suspense, null, {
  116. default: h('div', [h(ResolvingAsync), h(RejectingAsync)]),
  117. fallback: h('div', 'fallback')
  118. })
  119. }
  120. }
  121. expect(await renderToString(createApp(Comp))).toBe(`<div>fallback</div>`)
  122. expect(logError).toHaveBeenCalled()
  123. })
  124. test('failing suspense in passing suspense', async () => {
  125. const Comp = {
  126. render() {
  127. return h(Suspense, null, {
  128. default: h('div', [
  129. h(ResolvingAsync),
  130. h(Suspense, null, {
  131. default: h('div', [h(RejectingAsync)]),
  132. fallback: h('div', 'fallback 2')
  133. })
  134. ]),
  135. fallback: h('div', 'fallback 1')
  136. })
  137. }
  138. }
  139. expect(await renderToString(createApp(Comp))).toBe(
  140. `<div><div>async</div><div>fallback 2</div></div>`
  141. )
  142. expect(logError).toHaveBeenCalled()
  143. })
  144. test('passing suspense in failing suspense', async () => {
  145. const Comp = {
  146. render() {
  147. return h(Suspense, null, {
  148. default: h('div', [
  149. h(RejectingAsync),
  150. h(Suspense, null, {
  151. default: h('div', [h(ResolvingAsync)]),
  152. fallback: h('div', 'fallback 2')
  153. })
  154. ]),
  155. fallback: h('div', 'fallback 1')
  156. })
  157. }
  158. }
  159. expect(await renderToString(createApp(Comp))).toBe(
  160. `<div>fallback 1</div>`
  161. )
  162. expect(logError).toHaveBeenCalled()
  163. })
  164. })
  165. })