Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly clean up multi-root components (#295) #323

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/auto-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,33 @@ test('renders the component', () => {
test('cleans up after each test by default', () => {
expect(document.body.innerHTML).toMatchInlineSnapshot(``)
})

test('renders multi-root component', () => {
render({
template: `
<h1>Hello World</h1>
<h2>Hello World</h2>
`,
})

expect(document.body.innerHTML).toMatchInlineSnapshot(`
<div>
<h1>Hello World</h1>
<h2>Hello World</h2>
</div>
`)
})

test('cleans up after rendering multi-root node', () => {
expect(document.body.innerHTML).toMatchInlineSnapshot(``)
})

test('renders single slot component', () => {
render({template: `<slot />`})

expect(document.body.innerHTML).toMatchInlineSnapshot(`<div></div>`)
})

test('cleans up after rendering slot component', () => {
expect(document.body.innerHTML).toMatchInlineSnapshot(``)
})
12 changes: 7 additions & 5 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Check out the test examples on GitHub for further details.`)
// https://github.com/vuejs/vue-test-utils-next/blob/master/src/mount.ts#L309
unwrapNode(wrapper.parentElement)

mountedWrappers.add(wrapper)
mountedWrappers.add({wrapper, container})

return {
container,
Expand All @@ -59,13 +59,15 @@ function cleanup() {
mountedWrappers.forEach(cleanupAtWrapper)
}

function cleanupAtWrapper(wrapper) {
if (wrapper.element?.parentNode?.parentNode === document.body) {
document.body.removeChild(wrapper.element.parentNode)
function cleanupAtWrapper(entry) {
const {wrapper, container} = entry

if (container.parentNode === document.body) {
document.body.removeChild(container)
}

wrapper.unmount()
mountedWrappers.delete(wrapper)
mountedWrappers.delete(entry)
}

export {render, cleanup}