Skip to content

Commit

Permalink
[WIP] Support sibling elements in html pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 23, 2017
1 parent a9288e4 commit 2047cd1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
32 changes: 31 additions & 1 deletion packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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 */
Expand Down Expand Up @@ -93,4 +93,34 @@ describe('HTMLElement Plugin', () => {
'<div>\n <span>\n texty texty\n </span>\n</div>',
);
});

it('supports siblings', () => {
const parent = document.createElement('div');
parent.innerHTML = '<span>some </span><span>text</span>';

expect(parent).toPrettyPrintTo([
'<div>',
' <span>',
' some ',
' </span>',
' <span>',
' text',
' </span>',
'</div>',
].join('\n'));
});

it('supports text siblings', () => {
const parent = document.createElement('div');
parent.innerHTML = 'some <span>text</span>';

expect(parent).toPrettyPrintTo([
'<div>',
' some ',
' <span>',
' text',
' </span>',
'</div>',
].join('\n'));
});
});
67 changes: 34 additions & 33 deletions packages/pretty-format/src/__tests__/expect-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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';
Expand All @@ -13,41 +13,42 @@ 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;
getPrettyPrint: plugins =>
function(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') +
const message = pass
? () =>
this.utils.matcherHint('.not.toBe') +
'\n\n' +
`Expected value to be:\n` +
`Expected value to not be:\n` +
` ${this.utils.printExpected(expected)}\n` +
`Received:\n` +
` ${this.utils.printReceived(prettyFormatted)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
);
};
` ${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};
},
return {actual: prettyFormatted, message, pass};
},
};

0 comments on commit 2047cd1

Please sign in to comment.