From b08e601af887e12833caa0a08d12e675c1ace511 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Tue, 8 Nov 2022 15:35:41 +0800 Subject: [PATCH] test: add activated and deactivated for testing --- packages/runtime-core/__tests__/hmr.spec.ts | 34 +++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/hmr.spec.ts b/packages/runtime-core/__tests__/hmr.spec.ts index 47560c41ca4..07fc2270cf0 100644 --- a/packages/runtime-core/__tests__/hmr.spec.ts +++ b/packages/runtime-core/__tests__/hmr.spec.ts @@ -156,6 +156,8 @@ describe('hot module replacement', () => { const childId = 'test-child-keep-alive' const unmountSpy = jest.fn() const mountSpy = jest.fn() + const activeSpy = jest.fn() + const deactiveSpy = jest.fn() const Child: ComponentOptions = { __hmrId: childId, @@ -169,11 +171,16 @@ describe('hot module replacement', () => { const Parent: ComponentOptions = { components: { Child }, - render: compileToFunction(``) + data() { + return { toggle: true } + }, + render: compileToFunction( + `` + ) } render(h(Parent), root) - expect(serializeInner(root)).toBe(`
0
`) + expect(serializeInner(root)).toBe(`
0
`) reload(childId, { __hmrId: childId, @@ -181,12 +188,33 @@ describe('hot module replacement', () => { return { count: 1 } }, mounted: mountSpy, + unmounted: unmountSpy, + activated: activeSpy, + deactivated: deactiveSpy, render: compileToFunction(`
{{ count }}
`) }) await nextTick() - expect(serializeInner(root)).toBe(`
1
`) + 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() + 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(unmountSpy).toHaveBeenCalledTimes(1) expect(mountSpy).toHaveBeenCalledTimes(1) + expect(activeSpy).toHaveBeenCalledTimes(2) + expect(deactiveSpy).toHaveBeenCalledTimes(1) }) test('reload class component', async () => {