From 3ecde9e25adb2e3f2da309e1d4fa00a89ba8f1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E6=99=A8=E5=85=89?= <2293885211@qq.com> Date: Mon, 18 Mar 2024 15:45:49 +0800 Subject: [PATCH] fix: allow unmounting with no wrapper element (#312) * fix: check wrapper element to fix #311 --- src/__tests__/render.js | 15 ++++++++++++++- src/render.js | 5 +---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/__tests__/render.js b/src/__tests__/render.js index 151bd8bc..ea55817f 100644 --- a/src/__tests__/render.js +++ b/src/__tests__/render.js @@ -1,4 +1,5 @@ -import {render} from '..' +import {render, cleanup} from '..' +import {h, defineComponent} from 'vue' import '@testing-library/jest-dom' test('baseElement defaults to document.body', () => { @@ -87,3 +88,15 @@ test('unmounts', () => { expect(queryByTestId('node')).not.toBeInTheDocument() }) + +test('unmounts when no wrapper element is present', () => { + const Comp = defineComponent((_, ctx) => () => ctx.slots.default?.()) + + const {unmount} = render({ + render: () => h(Comp, () => h('div')), + }) + + unmount() + + expect(() => cleanup()).not.toThrow() +}) diff --git a/src/render.js b/src/render.js index 6f635cb2..997ba4c0 100644 --- a/src/render.js +++ b/src/render.js @@ -60,10 +60,7 @@ function cleanup() { } function cleanupAtWrapper(wrapper) { - if ( - wrapper.element.parentNode && - wrapper.element.parentNode.parentNode === document.body - ) { + if (wrapper.element?.parentNode?.parentNode === document.body) { document.body.removeChild(wrapper.element.parentNode) }