From 2047cd1b93a4865e9cf1cb2e1fd3a0c2ce69d4d0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 23 Apr 2017 10:43:01 +0200 Subject: [PATCH] [WIP] Support sibling elements in html pretty printing --- .../src/__tests__/HTMLElementPlugin-test.js | 32 ++++++++- .../src/__tests__/expect-util.js | 67 ++++++++++--------- 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js b/packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js index 8e99254683b3..2535c3f85bdb 100644 --- a/packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js +++ b/packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js @@ -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 */ @@ -93,4 +93,34 @@ describe('HTMLElement Plugin', () => { '
\n \n texty texty\n \n
', ); }); + + it('supports siblings', () => { + const parent = document.createElement('div'); + parent.innerHTML = 'some text'; + + expect(parent).toPrettyPrintTo([ + '
', + ' ', + ' some ', + ' ', + ' ', + ' text', + ' ', + '
', + ].join('\n')); + }); + + it('supports text siblings', () => { + const parent = document.createElement('div'); + parent.innerHTML = 'some text'; + + expect(parent).toPrettyPrintTo([ + '
', + ' some ', + ' ', + ' text', + ' ', + '
', + ].join('\n')); + }); }); diff --git a/packages/pretty-format/src/__tests__/expect-util.js b/packages/pretty-format/src/__tests__/expect-util.js index 1586a812aa96..31a474b64348 100644 --- a/packages/pretty-format/src/__tests__/expect-util.js +++ b/packages/pretty-format/src/__tests__/expect-util.js @@ -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'; @@ -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}; + }, };