Skip to content
Merged
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
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('')
})
})