Skip to content

Commit 43b3cc1

Browse files
author
Kent C. Dodds
committed
chore: refactor pretty-dom stuff
1 parent 3dac132 commit 43b3cc1

File tree

5 files changed

+84
-53
lines changed

5 files changed

+84
-53
lines changed

src/__tests__/pretty-dom.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import {prettyDOM} from '../pretty-dom'
1+
import {prettyDOM, debugDOM} from '../pretty-dom'
22
import {render} from './helpers/test-utils'
33

4-
test('prints out the given DOM element tree', () => {
4+
beforeEach(() => {
5+
jest.spyOn(console, 'log').mockImplementation(() => {})
6+
})
7+
8+
afterEach(() => {
9+
console.log.mockRestore()
10+
})
11+
12+
test('prettyDOM prints out the given DOM element tree', () => {
513
const {container} = render('<div>Hello World!</div>')
614
expect(prettyDOM(container)).toMatchInlineSnapshot(`
715
"<div>
@@ -12,16 +20,31 @@ test('prints out the given DOM element tree', () => {
1220
`)
1321
})
1422

15-
test('supports truncating the output length', () => {
23+
test('prettyDOM supports truncating the output length', () => {
1624
const {container} = render('<div>Hello World!</div>')
1725
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
1826
})
1927

20-
test('supports receiving the document element', () => {
28+
test('prettyDOM supports receiving the document element', () => {
2129
expect(prettyDOM(document)).toMatchInlineSnapshot(`
2230
"<html>
2331
<head />
2432
<body />
2533
</html>"
2634
`)
2735
})
36+
37+
test('debugDOM logs prettyDOM to the console', () => {
38+
const {container} = render('<div>Hello World!</div>')
39+
debugDOM(container)
40+
expect(console.log).toHaveBeenCalledTimes(1)
41+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
42+
"<div>
43+
<div>
44+
Hello World!
45+
</div>
46+
</div>"
47+
`)
48+
})
49+
50+
/* eslint no-console:0 */

src/__tests__/role-helpers.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import {getRoles, logRoles, getImplicitAriaRoles} from '../role-helpers'
22
import {render, cleanup} from './helpers/test-utils'
33

4+
jest.mock('../helpers', () => {
5+
const helpers = jest.requireActual('../helpers')
6+
return {
7+
...helpers,
8+
debug: jest.fn(),
9+
}
10+
})
11+
412
afterEach(cleanup)
513

14+
beforeEach(() => {
15+
jest.spyOn(console, 'log').mockImplementation(() => {})
16+
})
17+
18+
afterEach(() => {
19+
console.log.mockRestore()
20+
})
21+
622
function setup() {
723
const {getByTestId} = render(`
824
<section data-testid='a-section'>
@@ -139,16 +155,9 @@ test('getRoles returns expected roles for various dom nodes', () => {
139155

140156
test('logRoles calls console.log with output from prettyRoles', () => {
141157
const {section} = setup()
142-
143-
jest.spyOn(console, 'log').mockImplementationOnce(() => {})
144-
145158
logRoles(section)
146-
// eslint-disable-next-line no-console
147159
expect(console.log).toHaveBeenCalledTimes(1)
148-
// eslint-disable-next-line no-console
149160
expect(console.log.mock.calls[0][0]).toMatchSnapshot()
150-
// eslint-disable-next-line no-console
151-
console.log.mockRestore()
152161
})
153162

154163
test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
@@ -160,3 +169,5 @@ test('getImplicitAriaRoles returns expected roles for various dom nodes', () =>
160169
expect(getImplicitAriaRoles(radio)).toEqual(['radio'])
161170
expect(getImplicitAriaRoles(input)).toEqual(['textbox'])
162171
})
172+
173+
/* eslint no-console:0 */

src/pretty-dom.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
import prettyFormat from 'pretty-format'
22

3+
function inCypress(dom) {
4+
const window =
5+
(dom.ownerDocument && dom.ownerDocument.defaultView) || undefined
6+
return (
7+
(typeof global !== 'undefined' && global.Cypress) ||
8+
(typeof window !== 'undefined' && window.Cypress)
9+
)
10+
}
11+
12+
const inNode = () =>
13+
typeof process !== 'undefined' &&
14+
process.versions !== undefined &&
15+
process.versions.node !== undefined
16+
17+
const getMaxLength = dom =>
18+
inCypress(dom) ? 0 : process.env.DEBUG_PRINT_LIMIT || 7000
19+
320
const {DOMElement, DOMCollection} = prettyFormat.plugins
421

5-
function prettyDOM(htmlElement, maxLength, options) {
6-
if (htmlElement.documentElement) {
7-
htmlElement = htmlElement.documentElement
22+
function prettyDOM(dom, maxLength = getMaxLength(dom), options) {
23+
if (maxLength === 0) {
24+
return ''
25+
}
26+
if (dom.documentElement) {
27+
dom = dom.documentElement
828
}
929

10-
const debugContent = prettyFormat(htmlElement, {
30+
const debugContent = prettyFormat(dom, {
1131
plugins: [DOMElement, DOMCollection],
1232
printFunctionName: false,
13-
highlight: true,
33+
highlight: inNode(),
1434
...options,
1535
})
16-
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength
36+
return maxLength !== undefined && dom.outerHTML.length > maxLength
1737
? `${debugContent.slice(0, maxLength)}...`
1838
: debugContent
1939
}
2040

21-
export {prettyDOM}
41+
const debugDOM = (...args) => console.log(prettyDOM(...args))
42+
43+
export {prettyDOM, debugDOM}
44+
45+
/* eslint no-console:0 */

src/query-helpers.js

+1-27
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,8 @@ import {prettyDOM} from './pretty-dom'
22
import {fuzzyMatches, matches, makeNormalizer} from './matches'
33
import {waitForElement} from './wait-for-element'
44

5-
/* eslint-disable complexity */
6-
function debugDOM(htmlElement) {
7-
const limit = process.env.DEBUG_PRINT_LIMIT || 7000
8-
const inNode =
9-
typeof process !== 'undefined' &&
10-
process.versions !== undefined &&
11-
process.versions.node !== undefined
12-
/* istanbul ignore next */
13-
const window =
14-
(htmlElement.ownerDocument && htmlElement.ownerDocument.defaultView) ||
15-
undefined
16-
const inCypress =
17-
(typeof global !== 'undefined' && global.Cypress) ||
18-
(typeof window !== 'undefined' && window.Cypress)
19-
/* istanbul ignore else */
20-
if (inCypress) {
21-
return ''
22-
} else if (inNode) {
23-
return prettyDOM(htmlElement, limit)
24-
} else {
25-
return prettyDOM(htmlElement, limit, {highlight: false})
26-
}
27-
}
28-
/* eslint-enable complexity */
29-
305
function getElementError(message, container) {
31-
return new Error([message, debugDOM(container)].filter(Boolean).join('\n\n'))
6+
return new Error([message, prettyDOM(container)].filter(Boolean).join('\n\n'))
327
}
338

349
function getMultipleElementsFoundError(message, container) {
@@ -111,7 +86,6 @@ function buildQueries(queryAllBy, getMultipleError, getMissingError) {
11186
}
11287

11388
export {
114-
debugDOM,
11589
getElementError,
11690
getMultipleElementsFoundError,
11791
queryAllByAttribute,

src/role-helpers.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {elementRoles} from 'aria-query'
2-
import {debugDOM} from './query-helpers'
2+
import {prettyDOM} from './pretty-dom'
33

44
const elementRoleList = buildElementRoleList(elementRoles)
55

@@ -73,24 +73,23 @@ function getRoles(container) {
7373
}, {})
7474
}
7575

76-
function prettyRoles(container) {
77-
const roles = getRoles(container)
76+
function prettyRoles(dom) {
77+
const roles = getRoles(dom)
7878

7979
return Object.entries(roles)
8080
.map(([role, elements]) => {
8181
const delimiterBar = '-'.repeat(50)
8282
const elementsString = elements
83-
.map(el => debugDOM(el.cloneNode(false)))
83+
.map(el => prettyDOM(el.cloneNode(false)))
8484
.join('\n\n')
8585

8686
return `${role}:\n\n${elementsString}\n\n${delimiterBar}`
8787
})
8888
.join('\n')
8989
}
9090

91-
function logRoles(container) {
92-
// eslint-disable-next-line no-console
93-
console.log(prettyRoles(container))
94-
}
91+
const logRoles = dom => console.log(prettyRoles(dom))
9592

9693
export {getRoles, logRoles, getImplicitAriaRoles, prettyRoles}
94+
95+
/* eslint no-console:0 */

0 commit comments

Comments
 (0)