Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'pretty-format' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
import '../extend-expect'
import {plugins} from 'pretty-format'
import {render} from './helpers/test-utils'
expect.addSnapshotSerializer(plugins.ConvertAnsi)
test('.toBeInTheDOM', () => {
const {queryByTestId} = render(`
<span data-testid="count-container">
<span data-testid="count-value"></span>
<svg data-testid="svg-element"></svg>
</span>`)
const containerElement = queryByTestId('count-container')
const valueElement = queryByTestId('count-value')
const nonExistantElement = queryByTestId('not-exists')
const svgElement = queryByTestId('svg-element')
const fakeElement = {thisIsNot: 'an html element'}
// Testing toBeInTheDOM without container
expect(valueElement).toBeInTheDOM()
}
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,
highlight: inNode(),
...options,
})
return maxLength !== undefined && dom.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent
}
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import prettyFormat from 'pretty-format'
import { isElement } from './isElement'
const { DOMElement, DOMCollection } = prettyFormat.plugins
function format (htmlElement, maxLength, options) {
if (htmlElement.documentElement) {
// eslint-disable-next-line no-param-reassign
htmlElement = htmlElement.documentElement
}
const debugContent = prettyFormat(htmlElement, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
highlight: true,
...options,
})
return typeof maxLength !== 'undefined' && htmlElement.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent
return (accOutputs || []).concat(outputs || []);
};
/**
*
* @param input
* @param [generatorState]
* @returns {function(*, *): {hasGeneratedInput: boolean, input: *, generatorState: *}}
*/
export function constGen(input, generatorState) {
return function constGen(extS, genS) {
return { hasGeneratedInput: true, input, generatorState };
};
}
const { DOMElement, DOMCollection } = prettyFormat.plugins;
export function prettyDOM(htmlElement, maxLength, options) {
if (htmlElement.documentElement) {
htmlElement = htmlElement.documentElement;
}
const debugContent = prettyFormat(htmlElement, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
// highlight: true,
...options
});
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent;
}
// - can minify output
// - handles circular references
// - can serialize DOM
const serialize = function(param) {
return prettyFormat(param, PRETTY_FORMAT_OPTS)
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Make names short by truncating them
const truncateName = function(name) {
if (name.length <= MAX_NAME_LENGTH) {
return name
}
const start = name.slice(0, TRUNCATE_START_LENGTH)
const end = name.slice(name.length - TRUNCATE_END_LENGTH)
return `${start}...${end}`
}
const MAX_NAME_LENGTH = 120
const TRUNCATE_START_LENGTH = Math.ceil((MAX_NAME_LENGTH - 3) / 2)
// - can serialize DOM
const serialize = function(value) {
const title = prettyFormat(value, PRETTY_FORMAT_OPTS)
const titleA = ESCAPE_SEQUENCES.reduce(escapeSequence, title)
return titleA
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Escape newline characters to ensure title is on a single line
const escapeSequence = function(title, [regExp, replacement]) {
return title.replace(regExp, replacement)
}
const ESCAPE_SEQUENCES = [
[/\n/gu, '\\n'],
[/\r/gu, '\\r'],
[/\f/gu, '\\f'],
[/\v/gu, '\\v'],
]
// Make titles short by truncating them in the middle
// - works in browsers
// - is fast
// - has human-friendly output
// - can minify output
// - handles circular references
// - can serialize DOM
const serialize = function(param) {
return prettyFormat(param, PRETTY_FORMAT_OPTS)
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Make names short by truncating them
const truncateName = function(name) {
if (name.length <= MAX_NAME_LENGTH) {
return name
}
const start = name.slice(0, TRUNCATE_START_LENGTH)
const end = name.slice(name.length - TRUNCATE_END_LENGTH)
return `${start}...${end}`
}
// - has human-friendly output
// - can minify output (including maxDepth)
// - handles circular references
// - can serialize DOM
const serialize = function(value) {
const title = prettyFormat(value, PRETTY_FORMAT_OPTS)
const titleA = ESCAPE_SEQUENCES.reduce(escapeSequence, title)
return titleA
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Escape newline characters to ensure title is on a single line
const escapeSequence = function(title, [regExp, replacement]) {
return title.replace(regExp, replacement)
}
const ESCAPE_SEQUENCES = [
[/\n/gu, '\\n'],
[/\r/gu, '\\r'],
[/\f/gu, '\\f'],
[/\v/gu, '\\v'],
// - is fast
// - has human-friendly output
// - can minify output (including maxDepth)
// - handles circular references
// - can serialize DOM
const serialize = function(value) {
const title = prettyFormat(value, PRETTY_FORMAT_OPTS)
const titleA = ESCAPE_SEQUENCES.reduce(escapeSequence, title)
return titleA
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Escape newline characters to ensure title is on a single line
const escapeSequence = function(title, [regExp, replacement]) {
return title.replace(regExp, replacement)
}
const ESCAPE_SEQUENCES = [
[/\n/gu, '\\n'],
[/\r/gu, '\\r'],
[/\f/gu, '\\f'],
// - handles most JavaScript types
// - works in browsers
// - is fast
// - has human-friendly output
// - can minify output
// - handles circular references
// - can serialize DOM
const serialize = function(param) {
return prettyFormat(param, PRETTY_FORMAT_OPTS)
}
const PRETTY_FORMAT_OPTS = {
min: true,
maxDepth: 2,
plugins: [
plugins.DOMElement,
plugins.DOMCollection,
plugins.ReactElement,
plugins.Immutable,
plugins.ConvertAnsi,
],
}
// Make names short by truncating them
const truncateName = function(name) {
if (name.length <= MAX_NAME_LENGTH) {
return name
}
const start = name.slice(0, TRUNCATE_START_LENGTH)
const end = name.slice(name.length - TRUNCATE_END_LENGTH)
return `${start}...${end}`