} from '@vue/runtime-test'
describe('renderer: suspense', () => {
- it('should work', async () => {
+ it('basic usage (nested + multiple deps)', async () => {
const msg = ref('hello')
const deps: Promise<any>[] = []
}
const Comp = {
- name: 'root',
setup() {
return () =>
h(Suspense, [msg.value, h(Mid), h(AsyncChild2, { msg: 'child 2' })])
)
})
- test.todo('buffer mounted/updated hooks & watch callbacks')
+ test('fallback content', async () => {
+ const deps: Promise<any>[] = []
+
+ const Async = {
+ async setup() {
+ const p = new Promise(r => setTimeout(r, 1))
+ deps.push(p)
+ await p
+ // test resume for returning bindings
+ return {
+ msg: 'async'
+ }
+ },
+ render(this: any) {
+ return h('div', this.msg)
+ }
+ }
+
+ const Comp = {
+ setup() {
+ return () =>
+ h(Suspense, null, {
+ default: h(Async),
+ fallback: h('div', 'fallback')
+ })
+ }
+ }
- test.todo('fallback content')
+ const root = nodeOps.createElement('div')
+ render(h(Comp), root)
+ expect(serializeInner(root)).toBe(`<div>fallback</div>`)
+
+ await Promise.all(deps)
+ await nextTick()
+ expect(serializeInner(root)).toBe(`<div>async</div>`)
+ })
+
+ test.todo('buffer mounted/updated hooks & watch callbacks')
test.todo('content update before suspense resolve')
test.todo('unmount before suspense resolve')
test.todo('nested suspense')
+
+ test.todo('error handling')
})
import { VNode, normalizeVNode } from './vnode'
import { ShapeFlags } from '.'
+import { isFunction } from '@vue/shared'
export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol()
content: VNode
fallback: VNode
} {
- const { shapeFlag } = vnode
- const children = vnode.children as any
+ const { shapeFlag, children } = vnode
if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
+ const { default: d, fallback } = children as any
return {
- content: normalizeVNode(children.default()),
- fallback: normalizeVNode(children.fallback ? children.fallback() : null)
+ content: normalizeVNode(isFunction(d) ? d() : d),
+ fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
}
} else {
return {
- content: normalizeVNode(children),
+ content: normalizeVNode(children as any),
fallback: normalizeVNode(null)
}
}