Skip to content

Commit

Permalink
fix: allow unmounting with no wrapper element (#312)
Browse files Browse the repository at this point in the history
* fix: check wrapper element to fix #311
  • Loading branch information
hcg1023 authored Mar 18, 2024
1 parent f5a3483 commit 3ecde9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/__tests__/render.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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()
})
5 changes: 1 addition & 4 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 3ecde9e

Please sign in to comment.