forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds HtmlElement pretty-format plugin. (jestjs#3230)
* Adds HtmlElement pretty-format plugin. * Adds missing copyright and 'use strict' * Fixes variable naming / formatting. Reworks short circuiting logic for `isHTMLElement` function. * Improves performance for isHtmlElement. * Updating snapshots * Revert "Updating snapshots" This reverts commit 55f797f. * Fixes node 4 syntax * - Switches to tagName rather than constructor. * Adds HTMLElement Plugin usage. * Removes `HTMLElement` fallback in favor of tagName
- Loading branch information
Showing
8 changed files
with
267 additions
and
48 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
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
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
94 changes: 94 additions & 0 deletions
94
packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable max-len */ | ||
/* eslint-env browser*/ | ||
|
||
'use strict'; | ||
|
||
const HTMLElementPlugin = require('../plugins/HTMLElement'); | ||
const toPrettyPrintTo = require('./expect-util').getPrettyPrint([ | ||
HTMLElementPlugin, | ||
]); | ||
|
||
expect.extend({toPrettyPrintTo}); | ||
|
||
describe('HTMLElement Plugin', () => { | ||
it('supports a single HTML element', () => { | ||
expect(document.createElement('div')).toPrettyPrintTo('<div />'); | ||
}); | ||
|
||
it('supports an HTML element with a class property', () => { | ||
const parent = document.createElement('div'); | ||
parent.className = 'classy'; | ||
|
||
expect(parent).toPrettyPrintTo('<div\n class="classy"\n/>'); | ||
}); | ||
|
||
it('supports an HTML element with a title property', () => { | ||
const parent = document.createElement('div'); | ||
parent.title = 'title text'; | ||
|
||
expect(parent).toPrettyPrintTo('<div\n title="title text"\n/>'); | ||
}); | ||
|
||
it('supports an HTML element with a single attribute', () => { | ||
const parent = document.createElement('div'); | ||
parent.setAttribute('class', 'classy'); | ||
|
||
expect(parent).toPrettyPrintTo('<div\n class="classy"\n/>'); | ||
}); | ||
|
||
it('supports an HTML element with multiple attributes', () => { | ||
const parent = document.createElement('div'); | ||
parent.setAttribute('id', 123); | ||
parent.setAttribute('class', 'classy'); | ||
|
||
expect(parent).toPrettyPrintTo('<div\n id="123"\n class="classy"\n/>', { | ||
}); | ||
}); | ||
|
||
it('supports an element with text content', () => { | ||
const parent = document.createElement('div'); | ||
parent.innerHTML = 'texty texty'; | ||
|
||
expect(parent).toPrettyPrintTo('<div>\n texty texty\n</div>'); | ||
}); | ||
|
||
it('supports nested elements', () => { | ||
const parent = document.createElement('div'); | ||
const child = document.createElement('span'); | ||
parent.appendChild(child); | ||
expect(parent).toPrettyPrintTo('<div>\n <span />\n</div>'); | ||
}); | ||
|
||
it('supports nested elements with attributes', () => { | ||
const parent = document.createElement('div'); | ||
const child = document.createElement('span'); | ||
parent.appendChild(child); | ||
|
||
child.setAttribute('id', 123); | ||
child.setAttribute('class', 'classy'); | ||
|
||
expect(parent).toPrettyPrintTo( | ||
'<div>\n <span\n id="123"\n class="classy"\n />\n</div>', | ||
); | ||
}); | ||
|
||
it('supports nested elements with text content', () => { | ||
const parent = document.createElement('div'); | ||
const child = document.createElement('span'); | ||
parent.appendChild(child); | ||
child.textContent = 'texty texty'; | ||
|
||
expect(parent).toPrettyPrintTo( | ||
'<div>\n <span>\n texty texty\n </span>\n</div>', | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const diff = require('jest-diff'); | ||
const prettyFormat = require('../'); | ||
|
||
module.exports = { | ||
getPrettyPrint: plugins => | ||
(received, expected, opts) => { | ||
const prettyFormatted = prettyFormat( | ||
received, | ||
Object.assign( | ||
{ | ||
plugins, | ||
}, | ||
opts, | ||
), | ||
); | ||
const pass = prettyFormatted === expected; | ||
|
||
const message = pass | ||
? () => | ||
this.utils.matcherHint('.not.toBe') + | ||
'\n\n' + | ||
`Expected value to not be:\n` + | ||
` ${this.utils.printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${this.utils.printReceived(prettyFormatted)}` | ||
: () => { | ||
const diffString = diff(expected, prettyFormatted, { | ||
expand: this.expand, | ||
}); | ||
return this.utils.matcherHint('.toBe') + | ||
'\n\n' + | ||
`Expected value to be:\n` + | ||
` ${this.utils.printExpected(expected)}\n` + | ||
`Received:\n` + | ||
` ${this.utils.printReceived(prettyFormatted)}` + | ||
(diffString ? `\n\nDifference:\n\n${diffString}` : ''); | ||
}; | ||
|
||
return {actual: prettyFormatted, message, pass}; | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; | ||
|
||
const escapeHTML = require('./lib/escapeHTML'); | ||
const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)/; | ||
const test = isHTMLElement; | ||
|
||
function isHTMLElement(value: any) { | ||
return value !== undefined && | ||
value !== null && | ||
value.nodeType === 1 && | ||
value.constructor !== undefined && | ||
value.constructor.name !== undefined && | ||
HTML_ELEMENT_REGEXP.test(value.constructor.name); | ||
} | ||
|
||
function printChildren(flatChildren, print, indent, colors, opts) { | ||
return flatChildren | ||
.map(node => { | ||
if (typeof node === 'object') { | ||
return print(node, print, indent, colors, opts); | ||
} else if (typeof node === 'string') { | ||
return colors.content.open + escapeHTML(node) + colors.content.close; | ||
} else { | ||
return print(node); | ||
} | ||
}) | ||
.join(opts.edgeSpacing); | ||
} | ||
|
||
function printAttributes(attributes, print, indent, colors, opts) { | ||
return attributes | ||
.sort() | ||
.map(attribute => { | ||
return opts.spacing + | ||
indent(colors.prop.open + attribute.name + colors.prop.close + '=') + | ||
colors.value.open + | ||
`"${attribute.value}"` + | ||
colors.value.close; | ||
}) | ||
.join(''); | ||
} | ||
|
||
const print = ( | ||
element: any, | ||
print: Print, | ||
indent: Indent, | ||
opts: Options, | ||
colors: Colors, | ||
) => { | ||
let result = colors.tag.open + '<'; | ||
const elementName = element.tagName.toLowerCase(); | ||
result += elementName + colors.tag.close; | ||
|
||
const hasAttributes = element.attributes && element.attributes.length; | ||
if (hasAttributes) { | ||
const attributes = Array.prototype.slice.call(element.attributes); | ||
result += printAttributes(attributes, print, indent, colors, opts); | ||
} | ||
|
||
const flatChildren = Array.prototype.slice.call(element.children); | ||
if (!flatChildren.length && element.textContent) { | ||
flatChildren.push(element.textContent); | ||
} | ||
|
||
const closeInNewLine = hasAttributes && !opts.min; | ||
if (flatChildren.length) { | ||
const children = printChildren(flatChildren, print, indent, colors, opts); | ||
result += colors.tag.open + | ||
(closeInNewLine ? '\n' : '') + | ||
'>' + | ||
colors.tag.close + | ||
opts.edgeSpacing + | ||
indent(children) + | ||
opts.edgeSpacing + | ||
colors.tag.open + | ||
'</' + | ||
elementName + | ||
'>' + | ||
colors.tag.close; | ||
} else { | ||
result += colors.tag.open + | ||
(closeInNewLine ? '\n' : ' ') + | ||
'/>' + | ||
colors.tag.close; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
module.exports = ({print, test}: Plugin); |