Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose pretty-dom utility #25

Merged
merged 7 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions src/__tests__/__snapshots__/pretty-dom.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`it prints out the given DOM element tree 1`] = `
"<div>
<div>
Hello World!
</div>
</div>"
`;
12 changes: 12 additions & 0 deletions src/__tests__/pretty-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {prettyDOM} from '../pretty-dom'
import {render} from './helpers/test-utils'

test('it prints out the given DOM element tree', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container)).toMatchSnapshot()
})

test('it supports truncating the output length', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
})
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './matches'
export * from './get-node-text'
export * from './events'
export * from './bind-element-to-queries'
export * from './pretty-dom'
16 changes: 16 additions & 0 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import prettyFormat from 'pretty-format'

const {DOMElement, DOMCollection} = prettyFormat.plugins

function prettyDOM(htmlElement, maxLength = undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't need the = undefined because that's functionally equivalent to what the value of maxLength would be anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I know, I just like to be more explicit in the declaration that the argument is not expected, to anyone that passes by and read the code. Guess flow or typescript would be clearer for that purpose but I have yet to learn to use one of them, it's on my list ;)

const debugContent = prettyFormat(htmlElement, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
highlight: true,
})
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent
}

export {prettyDOM}
30 changes: 10 additions & 20 deletions src/queries.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import prettyFormat from 'pretty-format'
import {matches} from './matches'
import {getNodeText} from './get-node-text'
import {prettyDOM} from './pretty-dom'

const {DOMElement, DOMCollection} = prettyFormat.plugins
function debugDOM(htmlElement) {
return prettyDOM(htmlElement, process.env.DEBUG_PRINT_LIMIT || 7000)
}

// Here are the queries for the library.
// The queries here should only be things that are accessible to both users who are using a screen reader
Expand Down Expand Up @@ -74,7 +76,7 @@ function getByTestId(container, id, ...rest) {
const el = queryByTestId(container, id, ...rest)
if (!el) {
throw new Error(
`Unable to find an element by: [data-testid="${id}"] \n\n${htmlElementToDisplay(
`Unable to find an element by: [data-testid="${id}"] \n\n${debugDOM(
container,
)}`,
)
Expand All @@ -86,7 +88,7 @@ function getByPlaceholderText(container, text, ...rest) {
const el = queryByPlaceholderText(container, text, ...rest)
if (!el) {
throw new Error(
`Unable to find an element with the placeholder text of: ${text} \n\n${htmlElementToDisplay(
`Unable to find an element with the placeholder text of: ${text} \n\n${debugDOM(
container,
)}`,
)
Expand All @@ -100,13 +102,13 @@ function getByLabelText(container, text, ...rest) {
const label = queryLabelByText(container, text)
if (label) {
throw new Error(
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. \n\n${htmlElementToDisplay(
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. \n\n${debugDOM(
container,
)}`,
)
} else {
throw new Error(
`Unable to find a label with the text of: ${text} \n\n${htmlElementToDisplay(
`Unable to find a label with the text of: ${text} \n\n${debugDOM(
container,
)}`,
)
Expand All @@ -119,7 +121,7 @@ function getByText(container, text, ...rest) {
const el = queryByText(container, text, ...rest)
if (!el) {
throw new Error(
`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. \n\n${htmlElementToDisplay(
`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. \n\n${debugDOM(
container,
)}`,
)
Expand All @@ -139,26 +141,14 @@ function getByAltText(container, alt) {
const el = queryByAltText(container, alt)
if (!el) {
throw new Error(
`Unable to find an element with the alt text: ${alt} \n\n${htmlElementToDisplay(
`Unable to find an element with the alt text: ${alt} \n\n${debugDOM(
container,
)}`,
)
}
return el
}

function htmlElementToDisplay(htmlElement) {
const debugContent = prettyFormat(htmlElement, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
highlight: true,
})
const maxLength = process.env.DEBUG_PRINT_LIMIT || 7000
return htmlElement.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent
}

export {
queryByPlaceholderText,
getByPlaceholderText,
Expand Down