-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kent C. Dodds
committed
Aug 9, 2019
1 parent
3dac132
commit 4597576
Showing
5 changed files
with
114 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,63 @@ | ||
import {prettyDOM} from '../pretty-dom' | ||
import {render} from './helpers/test-utils' | ||
import {prettyDOM, debugDOM} from '../pretty-dom' | ||
import {render, renderIntoDocument, cleanup} from './helpers/test-utils' | ||
|
||
test('prints out the given DOM element tree', () => { | ||
afterEach(cleanup) | ||
|
||
beforeEach(() => { | ||
jest.spyOn(console, 'log').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
console.log.mockRestore() | ||
}) | ||
|
||
test('prettyDOM prints out the given DOM element tree', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
expect(prettyDOM(container)).toMatchInlineSnapshot(` | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
}) | ||
|
||
test('supports truncating the output length', () => { | ||
test('prettyDOM supports truncating the output length', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
expect(prettyDOM(container, 5)).toMatch(/\.\.\./) | ||
}) | ||
|
||
test('supports receiving the document element', () => { | ||
test('prettyDOM defaults to document.body', () => { | ||
renderIntoDocument('<div>Hello World!</div>') | ||
expect(prettyDOM()).toMatchInlineSnapshot(` | ||
"<body> | ||
<div> | ||
Hello World! | ||
</div> | ||
</body>" | ||
`) | ||
}) | ||
|
||
test('prettyDOM supports receiving the document element', () => { | ||
expect(prettyDOM(document)).toMatchInlineSnapshot(` | ||
"<html> | ||
<head /> | ||
<body /> | ||
</html>" | ||
`) | ||
"<html> | ||
<head /> | ||
<body /> | ||
</html>" | ||
`) | ||
}) | ||
|
||
test('debugDOM logs prettyDOM to the console', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
debugDOM(container) | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(` | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
}) | ||
|
||
/* eslint no-console:0 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,50 @@ | ||
import prettyFormat from 'pretty-format' | ||
import {getDocument} from './helpers' | ||
|
||
function inCypress(dom) { | ||
const window = | ||
(dom.ownerDocument && dom.ownerDocument.defaultView) || undefined | ||
return ( | ||
(typeof global !== 'undefined' && global.Cypress) || | ||
(typeof window !== 'undefined' && window.Cypress) | ||
) | ||
} | ||
|
||
const inNode = () => | ||
typeof process !== 'undefined' && | ||
process.versions !== undefined && | ||
process.versions.node !== undefined | ||
|
||
const getMaxLength = dom => | ||
inCypress(dom) ? 0 : process.env.DEBUG_PRINT_LIMIT || 7000 | ||
|
||
const {DOMElement, DOMCollection} = prettyFormat.plugins | ||
|
||
function prettyDOM(htmlElement, maxLength, options) { | ||
if (htmlElement.documentElement) { | ||
htmlElement = htmlElement.documentElement | ||
function prettyDOM( | ||
dom = getDocument().body, | ||
maxLength = getMaxLength(dom), | ||
options, | ||
) { | ||
if (maxLength === 0) { | ||
return '' | ||
} | ||
if (dom.documentElement) { | ||
dom = dom.documentElement | ||
} | ||
|
||
const debugContent = prettyFormat(htmlElement, { | ||
const debugContent = prettyFormat(dom, { | ||
plugins: [DOMElement, DOMCollection], | ||
printFunctionName: false, | ||
highlight: true, | ||
highlight: inNode(), | ||
...options, | ||
}) | ||
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength | ||
return maxLength !== undefined && dom.outerHTML.length > maxLength | ||
? `${debugContent.slice(0, maxLength)}...` | ||
: debugContent | ||
} | ||
|
||
export {prettyDOM} | ||
const debugDOM = (...args) => console.log(prettyDOM(...args)) | ||
|
||
export {prettyDOM, debugDOM} | ||
|
||
/* eslint no-console:0 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters