Skip to content

Commit 9e6594d

Browse files
oieduardorabelobenmonro
authored andcommitted
fix: clean up document targets and components (#19)
* clean up document targets and components * jest cache was returning `<div></div>`, running with `--no-cache` fixed it * we can safely ignore `else` for code coverage here - https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignore-an-else-path
1 parent 8ed5e12 commit 9e6594d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const render = (Component, {target = document.createElement('div'), ...op
1010
target,
1111
})
1212

13-
mountedContainers.add(component)
13+
mountedContainers.add({target, component})
1414
return {
1515
component,
1616
// eslint-disable-next-line no-console
@@ -20,7 +20,12 @@ export const render = (Component, {target = document.createElement('div'), ...op
2020
}
2121

2222
const cleanupAtContainer = container => {
23-
container.$destroy()
23+
const {target, component} = container
24+
component.$destroy()
25+
/* istanbul ignore else */
26+
if (target.parentNode === document.body) {
27+
document.body.removeChild(target)
28+
}
2429
mountedContainers.delete(container)
2530
}
2631

Diff for: tests/render.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ describe('render', () => {
3838
})
3939

4040
test('debug', () => {
41+
const originalConsole = global.console
42+
4143
global.console = {log: jest.fn()}
4244

4345
const {debug} = render(App)
4446

4547
debug()
4648

4749
expect(global.console.log).toHaveBeenCalledWith(prettyDOM(document.body))
50+
51+
global.console = originalConsole
4852
})
4953

5054
test('custom container target', () => {
@@ -61,4 +65,8 @@ describe('render', () => {
6165
expect(getByText('Hello world!')).toBeInTheDocument()
6266
expect(getByTestId('custom-target')).toBeInTheDocument()
6367
})
68+
69+
test('after each test above, document is clean from targets and components', () => {
70+
expect(document.body.innerHTML).toBe('')
71+
})
6472
})

0 commit comments

Comments
 (0)