From 3959a2a2e1898ccd4a331b50222a1ad9137b8d9a Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 10 Sep 2019 11:17:26 -0400 Subject: [PATCH] test: test suspense fallback --- .../__tests__/rendererSuspense.spec.ts | 44 +++++++++++++++++-- packages/runtime-core/src/suspense.ts | 11 ++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/runtime-core/__tests__/rendererSuspense.spec.ts b/packages/runtime-core/__tests__/rendererSuspense.spec.ts index faafcbf3..acc2ff37 100644 --- a/packages/runtime-core/__tests__/rendererSuspense.spec.ts +++ b/packages/runtime-core/__tests__/rendererSuspense.spec.ts @@ -10,7 +10,7 @@ import { } 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[] = [] @@ -59,7 +59,6 @@ describe('renderer: suspense', () => { } const Comp = { - name: 'root', setup() { return () => h(Suspense, [msg.value, h(Mid), h(AsyncChild2, { msg: 'child 2' })]) @@ -77,13 +76,50 @@ describe('renderer: suspense', () => { ) }) - test.todo('buffer mounted/updated hooks & watch callbacks') + test('fallback content', async () => { + const deps: Promise[] = [] - test.todo('fallback content') + 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') + }) + } + } + + const root = nodeOps.createElement('div') + render(h(Comp), root) + expect(serializeInner(root)).toBe(`
fallback
`) + + await Promise.all(deps) + await nextTick() + expect(serializeInner(root)).toBe(`
async
`) + }) + + 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') }) diff --git a/packages/runtime-core/src/suspense.ts b/packages/runtime-core/src/suspense.ts index 16571af4..e3f04c51 100644 --- a/packages/runtime-core/src/suspense.ts +++ b/packages/runtime-core/src/suspense.ts @@ -1,5 +1,6 @@ import { VNode, normalizeVNode } from './vnode' import { ShapeFlags } from '.' +import { isFunction } from '@vue/shared' export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol() @@ -48,16 +49,16 @@ export function normalizeSuspenseChildren( 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) } }