diff --git a/src/index.js b/src/index.js index 450b82f..2258231 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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) } diff --git a/tests/render.spec.js b/tests/render.spec.js index 8f209be..ed788b2 100644 --- a/tests/render.spec.js +++ b/tests/render.spec.js @@ -38,6 +38,8 @@ describe('render', () => { }) test('debug', () => { + const originalConsole = global.console + global.console = {log: jest.fn()} const {debug} = render(App) @@ -45,6 +47,8 @@ describe('render', () => { debug() expect(global.console.log).toHaveBeenCalledWith(prettyDOM(document.body)) + + global.console = originalConsole }) test('custom container target', () => { @@ -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('') + }) })