Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/__tests__/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ test('prettyDOM supports truncating the output length', () => {
})

test('prettyDOM defaults to document.body', () => {
const defaultInlineSnapshot = `
"<body>
<div>
Hello World!
</div>
</body>"
`
renderIntoDocument('<div>Hello World!</div>')
expect(prettyDOM()).toMatchInlineSnapshot(`
"<body>
<div>
Hello World!
</div>
</body>"
`)
expect(prettyDOM()).toMatchInlineSnapshot(defaultInlineSnapshot)
expect(prettyDOM(null)).toMatchInlineSnapshot(defaultInlineSnapshot)
})

test('prettyDOM supports receiving the document element', () => {
Expand All @@ -58,4 +60,22 @@ test('logDOM logs prettyDOM to the console', () => {
`)
})

describe('prettyDOM fails with first parameter without outerHTML field', () => {
test('with array', () => {
expect(() => prettyDOM(['outerHTML'])).toThrowErrorMatchingInlineSnapshot(
`"Expected an element or document but got Array"`,
)
})
test('with number', () => {
expect(() => prettyDOM(1)).toThrowErrorMatchingInlineSnapshot(
`"Expected an element or document but got number"`,
)
})
test('with object', () => {
expect(() => prettyDOM({})).toThrowErrorMatchingInlineSnapshot(
`"Expected an element or document but got Object"`,
)
})
})

/* eslint no-console:0 */
26 changes: 21 additions & 5 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,34 @@ const getMaxLength = dom =>

const {DOMElement, DOMCollection} = prettyFormat.plugins

function prettyDOM(
dom = getDocument().body,
maxLength = getMaxLength(dom),
options,
) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inCypress has failed with null parameter so in addition I replaced arguments default initializations.

function prettyDOM(dom, maxLength, options) {
if (!dom) {
dom = getDocument().body
}
if (typeof maxLength !== 'number') {
maxLength = getMaxLength(dom)
}

if (maxLength === 0) {
return ''
}
if (dom.documentElement) {
dom = dom.documentElement
}

let domTypeName = typeof dom
if (domTypeName === 'object') {
domTypeName = dom.constructor.name
} else {
// To don't fall with `in` operator
dom = {}
}
if (!('outerHTML' in dom)) {
throw new TypeError(
`Expected an element or document but got ${domTypeName}`,
)
}

const debugContent = prettyFormat(dom, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
Expand Down