]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): vnode.el is null in watcher after rerendering (#2295)
authorPhilipp Rudloff <philipprudloff@fastmail.com>
Mon, 5 Oct 2020 20:05:43 +0000 (22:05 +0200)
committerGitHub <noreply@github.com>
Mon, 5 Oct 2020 20:05:43 +0000 (16:05 -0400)
fix #2170

packages/runtime-core/__tests__/rendererComponent.spec.ts
packages/runtime-core/src/renderer.ts

index 4253779d4cfe3a6ee079899b87b49d98ac7866bb..d178c21e82ca614a01e755333d159bd427819c92 100644 (file)
@@ -137,4 +137,83 @@ describe('renderer: component', () => {
     await nextTick()
     expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
   })
+
+  // #2170
+  test('should have access to instance’s “$el” property in watcher when setting instance data', async () => {
+    function returnThis(this: any) {
+      return this
+    }
+    const dataWatchSpy = jest.fn(returnThis)
+    let instance: any
+    const Comp = {
+      data() {
+        return {
+          testData: undefined
+        }
+      },
+
+      watch: {
+        testData() {
+          // @ts-ignore
+          dataWatchSpy(this.$el)
+        }
+      },
+
+      created() {
+        instance = this
+      },
+
+      render() {
+        return h('div')
+      }
+    }
+
+    const root = nodeOps.createElement('div')
+    render(h(Comp), root)
+
+    expect(dataWatchSpy).not.toHaveBeenCalled()
+    instance.testData = 'data'
+
+    await nextTick()
+    expect(dataWatchSpy).toHaveBeenCalledWith(instance.$el)
+  })
+
+  // #2170
+  test('should have access to instance’s “$el” property in watcher when rendereing with watched prop', async () => {
+    function returnThis(this: any) {
+      return this
+    }
+    const propWatchSpy = jest.fn(returnThis)
+    let instance: any
+    const Comp = {
+      props: {
+        testProp: String
+      },
+
+      watch: {
+        testProp() {
+          // @ts-ignore
+          propWatchSpy(this.$el)
+        }
+      },
+
+      created() {
+        instance = this
+      },
+
+      render() {
+        return h('div')
+      }
+    }
+
+    const root = nodeOps.createElement('div')
+
+    render(h(Comp), root)
+    await nextTick()
+    expect(propWatchSpy).not.toHaveBeenCalled()
+
+    render(h(Comp, { testProp: 'prop ' }), root)
+    await nextTick()
+    expect(propWatchSpy).toHaveBeenCalledWith(instance.$el)
+  })
 })
index bb3129bd7af612379fc98fb7a8bd93d39d3a23e9..217e9a4c2099189fe9628897a10e0f2103a7f678 100644 (file)
@@ -1425,11 +1425,11 @@ function baseCreateRenderer(
         }
 
         if (next) {
+          next.el = vnode.el
           updateComponentPreRender(instance, next, optimized)
         } else {
           next = vnode
         }
-        next.el = vnode.el
 
         // beforeUpdate hook
         if (bu) {