From afb21f7813dec151773675737596e9f43dd871e7 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 21 Nov 2023 17:57:44 +0800 Subject: [PATCH] test: fix keepalive transition out-in test case --- packages/runtime-core/__tests__/hmr.spec.ts | 89 ++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/__tests__/hmr.spec.ts b/packages/runtime-core/__tests__/hmr.spec.ts index a5ec90ad7..f11e7e570 100644 --- a/packages/runtime-core/__tests__/hmr.spec.ts +++ b/packages/runtime-core/__tests__/hmr.spec.ts @@ -176,7 +176,8 @@ describe('hot module replacement', () => { return { toggle: true } }, render: compileToFunction( - `` + `` + ``) + expect(unmountSpy).toHaveBeenCalledTimes(1) + expect(mountSpy).toHaveBeenCalledTimes(1) + expect(activeSpy).toHaveBeenCalledTimes(1) + expect(deactiveSpy).toHaveBeenCalledTimes(1) + + // should not mount when toggling + triggerEvent(root.children[1] as TestElement, 'click') + await nextTick() + expect(serializeInner(root)).toBe(`
1
`) + expect(unmountSpy).toHaveBeenCalledTimes(1) + expect(mountSpy).toHaveBeenCalledTimes(1) + expect(activeSpy).toHaveBeenCalledTimes(2) + expect(deactiveSpy).toHaveBeenCalledTimes(1) + }) + + test('reload KeepAlive slot in Transition with out-in', async () => { + const root = nodeOps.createElement('div') + const childId = 'test-transition-keep-alive-reload-with-out-in' + const unmountSpy = vi.fn() + const mountSpy = vi.fn() + const activeSpy = vi.fn() + const deactiveSpy = vi.fn() + + const Child: ComponentOptions = { + __hmrId: childId, + name: 'original', + data() { + return { count: 0 } + }, + unmounted: unmountSpy, + render: compileToFunction(`
{{ count }}
`) + } + createRecord(childId, Child) + + const Parent: ComponentOptions = { + components: { Child }, + data() { + return { toggle: true } + }, + methods: { + // @ts-ignore + onLeave(_, done) { + setTimeout(done, 0) + } + }, + render: compileToFunction( + `
0
`) + + reload(childId, { + __hmrId: childId, + name: 'updated', + data() { + return { count: 1 } + }, + mounted: mountSpy, + unmounted: unmountSpy, + activated: activeSpy, + deactivated: deactiveSpy, + render: compileToFunction(`
{{ count }}
`) + }) + await nextTick() + await new Promise(r => setTimeout(r, 0)) + expect(serializeInner(root)).toBe(`
1
`) + expect(unmountSpy).toHaveBeenCalledTimes(1) + expect(mountSpy).toHaveBeenCalledTimes(1) + expect(activeSpy).toHaveBeenCalledTimes(1) + expect(deactiveSpy).toHaveBeenCalledTimes(0) + + // should not unmount when toggling + triggerEvent(root.children[1] as TestElement, 'click') + await nextTick() + await new Promise(r => setTimeout(r, 0)) expect(serializeInner(root)).toBe(``) expect(unmountSpy).toHaveBeenCalledTimes(1) expect(mountSpy).toHaveBeenCalledTimes(1)