Skip to content

Commit f5184ce

Browse files
alexkrolickKent C. Dodds
authored andcommitted
fix: unsafe attribute reference (#129)
Fixes an unsafe reference that can throw an error in `debugDOM` when it receives a reference to a `document` in a real browser (ie puppeteer/codepen) <!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: <!-- Why are these changes necessary? --> **Why**: <!-- How were these changes implemented? --> **How**: <!-- Have you done all of these things? --> **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [ ] Documentation - [ ] Tests - [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> - [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments -->
1 parent fe08240 commit f5184ce

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/__tests__/element-queries.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'jest-dom/extend-expect'
2-
import {render} from './helpers/test-utils'
2+
import {render, renderIntoDocument} from './helpers/test-utils'
33
import document from './helpers/document'
44

55
beforeEach(() => {
@@ -486,19 +486,19 @@ test('test the debug helper prints the dom state here', () => {
486486
})}
487487
</div>`
488488

489-
const {getByText} = render(Large) // render large DOM which exceeds 7000 limit
489+
const {getByText} = renderIntoDocument(Large) // render large DOM which exceeds 7000 limit
490490
expect(() => expect(getByText('not present')).toBeTruthy()).toThrowError()
491491

492492
const Hello = `<div data-testid="debugging" data-otherid="debugging">
493493
Hello World!
494494
</div>`
495-
const {getByTestId} = render(Hello)
495+
const {getByTestId} = renderIntoDocument(Hello)
496496
process.env.DEBUG_PRINT_LIMIT = 5 // user should see `...`
497497
expect(() => expect(getByTestId('not present')).toBeTruthy()).toThrowError(
498-
/\.\.\./,
498+
/\.\.\.$/,
499499
)
500500

501-
const {getByLabelText} = render(Hello)
501+
const {getByLabelText} = renderIntoDocument(Hello)
502502
process.env.DEBUG_PRINT_LIMIT = 10000 // user shouldn't see `...`
503503
expect(() =>
504504
expect(getByLabelText('not present')).toBeTruthy(/^((?!\.\.\.).)*$/),

src/__tests__/helpers/test-utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ function render(html) {
88
return {container, ...containerQueries}
99
}
1010

11-
export {render}
11+
function renderIntoDocument(html) {
12+
document.body.innerHTML = html
13+
const containerQueries = getQueriesForElement(document)
14+
return {container: document, ...containerQueries}
15+
}
16+
17+
export {render, renderIntoDocument}

src/query-helpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ function debugDOM(htmlElement) {
88
typeof process !== 'undefined' &&
99
process.versions !== undefined &&
1010
process.versions.node !== undefined
11-
const window = htmlElement.ownerDocument.defaultView
11+
const window =
12+
(htmlElement.ownerDocument && htmlElement.ownerDocument.defaultView) ||
13+
undefined
1214
const inCypress = typeof window !== 'undefined' && window.Cypress
1315
/* istanbul ignore else */
1416
if (inCypress) {

0 commit comments

Comments
 (0)