From 0ff2a4f1c1847a4e173dcab810e6438143a4272c Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sat, 28 Nov 2020 03:01:01 +0800 Subject: [PATCH] fix(runtime-core): should pause tracking when initializing legacy options (#2524) fix #2521 --- .../__tests__/rendererComponent.spec.ts | 43 +++++++++++++++++++ packages/runtime-core/src/component.ts | 2 + 2 files changed, 45 insertions(+) diff --git a/packages/runtime-core/__tests__/rendererComponent.spec.ts b/packages/runtime-core/__tests__/rendererComponent.spec.ts index 5616c22a1b0..29a7d076d94 100644 --- a/packages/runtime-core/__tests__/rendererComponent.spec.ts +++ b/packages/runtime-core/__tests__/rendererComponent.spec.ts @@ -232,4 +232,47 @@ describe('renderer: component', () => { await nextTick() expect(serializeInner(root)).toBe(`
1
1
`) }) + + // #2521 + test('should pause tracking deps when initializing legacy options', async () => { + let childInstance = null as any + const Child = { + props: ['foo'], + data() { + return { + count: 0 + } + }, + watch: { + foo: { + immediate: true, + handler() { + ;(this as any).count + } + } + }, + created() { + childInstance = this as any + childInstance.count + }, + render() { + return h('h1', (this as any).count) + } + } + + const App = { + setup() { + return () => h(Child) + }, + updated: jest.fn() + } + + const root = nodeOps.createElement('div') + render(h(App), root) + expect(App.updated).toHaveBeenCalledTimes(0) + + childInstance.count++ + await nextTick() + expect(App.updated).toHaveBeenCalledTimes(0) + }) }) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 43f700cc2d3..9b1fdcef18f 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -704,7 +704,9 @@ function finishComponentSetup( // support for 2.x options if (__FEATURE_OPTIONS_API__) { currentInstance = instance + pauseTracking() applyOptions(instance, Component) + resetTracking() currentInstance = null }