Skip to content
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
40 changes: 40 additions & 0 deletions src/__tests__/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,46 @@ test('cleanup does not error when an element is not a child', () => {
cleanup()
})

describe('container is the document', () => {
let documentElement
beforeEach(() => {
documentElement = document.documentElement
})

afterEach(() => {
// React 18 removes the `<html>` element when unmounting a root rendered into `document`
if (document.documentElement === null) {
document.appendChild(documentElement)
}
})

test('cleanup does not error when the container is the document', () => {
render(
<html lang="en">
<body>
<div />
</body>
</html>,
{container: document},
)

cleanup()
})

test('cleanup does not error when hydrating the document', () => {
render(
// eslint-disable-next-line jsx-a11y/html-has-lang -- matches the existing document
<html>
<head />
<body />
</html>,
{container: document, hydrate: true},
)

cleanup()
})
})

test('cleanup runs effect cleanup functions', () => {
const spy = jest.fn()

Expand Down
3 changes: 2 additions & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ function cleanup() {
act(() => {
root.unmount()
})
if (container.parentNode === document.body) {
// `document.body` is `null` if unmounting a root rendered into `document` removed `<html>` (React 18)
if (document.body !== null && container.parentNode === document.body) {
document.body.removeChild(container)
}
})
Expand Down
Loading