Skip to content

Commit

Permalink
fix: clean up document targets and components (#19)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
oieduardorabelo authored and benmonro committed May 5, 2019
1 parent 8ed5e12 commit 9e6594d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const render = (Component, {target = document.createElement('div'), ...op
target,
})

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

const cleanupAtContainer = container => {
container.$destroy()
const {target, component} = container
component.$destroy()
/* istanbul ignore else */
if (target.parentNode === document.body) {
document.body.removeChild(target)
}
mountedContainers.delete(container)
}

Expand Down
8 changes: 8 additions & 0 deletions tests/render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ describe('render', () => {
})

test('debug', () => {
const originalConsole = global.console

global.console = {log: jest.fn()}

const {debug} = render(App)

debug()

expect(global.console.log).toHaveBeenCalledWith(prettyDOM(document.body))

global.console = originalConsole
})

test('custom container target', () => {
Expand All @@ -61,4 +65,8 @@ describe('render', () => {
expect(getByText('Hello world!')).toBeInTheDocument()
expect(getByTestId('custom-target')).toBeInTheDocument()
})

test('after each test above, document is clean from targets and components', () => {
expect(document.body.innerHTML).toBe('')
})
})

0 comments on commit 9e6594d

Please sign in to comment.