]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): ensure custom events are not emitted anymore after unmount. (...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Thu, 14 Apr 2022 03:47:24 +0000 (05:47 +0200)
committerGitHub <noreply@github.com>
Thu, 14 Apr 2022 03:47:24 +0000 (23:47 -0400)
close #5674

packages/runtime-core/__tests__/componentEmits.spec.ts
packages/runtime-core/src/componentEmits.ts

index d88981a43d4f553f0c0bc9a6d8a3986be407fc2d..8963bda2649043fc4c50403bee2c86c976b67d3f 100644 (file)
@@ -6,7 +6,8 @@ import {
   defineComponent,
   h,
   nodeOps,
-  toHandlers
+  toHandlers,
+  nextTick
 } from '@vue/runtime-test'
 import { isEmitListener } from '../src/componentEmits'
 
@@ -374,4 +375,29 @@ describe('component: emit', () => {
     // PascalCase option
     expect(isEmitListener(options, 'onFooBaz')).toBe(true)
   })
+
+  test('does not emit after unmount', async () => {
+    const fn = jest.fn()
+    const Foo = defineComponent({
+      emits: ['closing'],
+      async beforeUnmount() {
+        await this.$nextTick()
+        this.$emit('closing', true)
+      },
+      render() {
+        return h('div')
+      }
+    })
+    const Comp = () =>
+      h(Foo, {
+        onClosing: fn
+      })
+
+    const el = nodeOps.createElement('div')
+    render(h(Comp), el)
+    await nextTick()
+    render(null, el)
+    await nextTick()
+    expect(fn).not.toHaveBeenCalled()
+  })
 })
index 4d028913d15a0bf6aa85fc0aedfd92a384bae30a..3983e5885b3f3522d4a34febe77765dfd351b3a7 100644 (file)
@@ -73,6 +73,7 @@ export function emit(
   event: string,
   ...rawArgs: any[]
 ) {
+  if (instance.isUnmounted) return
   const props = instance.vnode.props || EMPTY_OBJ
 
   if (__DEV__) {