From f0a2e52890102f0a5aaee1205923788b5fe8f11d Mon Sep 17 00:00:00 2001 From: Eduardo Rabelo Date: Sat, 4 May 2019 15:10:17 +1000 Subject: [PATCH 1/3] add custom container target --- src/index.js | 6 +++++- tests/render.spec.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index fabf240..1b13e9b 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,11 @@ import {getQueriesForElement, prettyDOM} from 'dom-testing-library' export * from 'dom-testing-library' const mountedContainers = new Set() export const render = (Component, options) => { - const target = document.body.appendChild(document.createElement('div')) + let target = document.body.appendChild(document.createElement('div')) + + if (options && options.target) { + target = options.target + } const component = new Component({ ...options, diff --git a/tests/render.spec.js b/tests/render.spec.js index ecf9634..8f209be 100644 --- a/tests/render.spec.js +++ b/tests/render.spec.js @@ -46,4 +46,19 @@ describe('render', () => { expect(global.console.log).toHaveBeenCalledWith(prettyDOM(document.body)) }) + + test('custom container target', () => { + const customTarget = document.createElement('main') + customTarget.dataset.testid = 'custom-target' + + document.body.appendChild(customTarget) + + const {getByText, getByTestId} = render(App, { + target: customTarget, + props: {name: 'world'}, + }) + + expect(getByText('Hello world!')).toBeInTheDocument() + expect(getByTestId('custom-target')).toBeInTheDocument() + }) }) From ede7cad70f35ce58e7cc166e4e81bc73a7cb647d Mon Sep 17 00:00:00 2001 From: Eduardo Rabelo Date: Sat, 4 May 2019 15:37:45 +1000 Subject: [PATCH 2/3] using default params --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 1b13e9b..6a2c378 100644 --- a/src/index.js +++ b/src/index.js @@ -2,10 +2,10 @@ import {getQueriesForElement, prettyDOM} from 'dom-testing-library' export * from 'dom-testing-library' const mountedContainers = new Set() -export const render = (Component, options) => { +export const render = (Component, options = {}) => { let target = document.body.appendChild(document.createElement('div')) - if (options && options.target) { + if (options.target) { target = options.target } From a42bc642d768e7686ce1e6ca5fff81e512ec15bc Mon Sep 17 00:00:00 2001 From: Eduardo Rabelo Date: Sun, 5 May 2019 00:35:10 +1000 Subject: [PATCH 3/3] addressing comment "pull/12#discussion_r280982845" --- src/index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 6a2c378..450b82f 100644 --- a/src/index.js +++ b/src/index.js @@ -2,12 +2,8 @@ import {getQueriesForElement, prettyDOM} from 'dom-testing-library' export * from 'dom-testing-library' const mountedContainers = new Set() -export const render = (Component, options = {}) => { - let target = document.body.appendChild(document.createElement('div')) - - if (options.target) { - target = options.target - } +export const render = (Component, {target = document.createElement('div'), ...options} = {}) => { + document.body.appendChild(target) const component = new Component({ ...options,