Skip to content

Commit

Permalink
Add alternative serialize API for pretty-format plugins (#4114)
Browse files Browse the repository at this point in the history
* Add alternative serialize API for pretty-format plugins

* Fix logic in ReactElement plugin and add test for zero number child

* Add test for empty string child

* Rename 2 helper functions and use in more places

* Replace require paths to plugins with destructuring assignment

* Add any type annotation to val arg of printPlugin

* Replace overlooked occurrences of formatted with expected

* Fix pretty lint error

* Add comment and repeat to indent test

* Add comment and repeat to indent test for react also

* Fix pretty lint error in html_element.test.js
  • Loading branch information
pedrottimark authored and cpojer committed Aug 1, 2017
1 parent 31edc39 commit 9665de2
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
exports[`ReactElement plugin highlights syntax 1`] = `
"<Mouse</>
prop</>=<green>{
<div</>></>
<div></>
</>mouse</>
<span</>></>
<span></>
</>rat</>
</span></>
</div></>
Expand All @@ -24,9 +24,9 @@ exports[`ReactElement plugin highlights syntax with color from theme option 1`]
exports[`ReactTestComponent plugin highlights syntax 1`] = `
"<Mouse</>
prop</>=<green>{
<div</>></>
<div></>
</>mouse</>
<span</>></>
<span></>
</>rat</>
</span></>
</div></>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import type {OptionsReceived} from 'types/PrettyFormat';

const prettyFormat = require('../');
const AsymmetricMatcher = require('../plugins/asymmetric_matcher');
const {AsymmetricMatcher} = prettyFormat.plugins;
let options: OptionsReceived;

function fnNameFor(func) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/convert_ansi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

const ansiStyle = require('ansi-styles');
const prettyFormat = require('../');
const ConvertAnsiPlugin = require('../plugins/convert_ansi');
const {ConvertAnsi} = prettyFormat.plugins;

const prettyFormatResult = (val: string) => {
return prettyFormat(val, {
plugins: [ConvertAnsiPlugin],
plugins: [ConvertAnsi],
});
};

Expand Down
7 changes: 3 additions & 4 deletions packages/pretty-format/src/__tests__/html_element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

'use strict';

const HTMLElementPlugin = require('../plugins/html_element');
const toPrettyPrintTo = require('./expect_util').getPrettyPrint([
HTMLElementPlugin,
]);
const prettyFormat = require('../');
const {HTMLElement} = prettyFormat.plugins;
const toPrettyPrintTo = require('./expect_util').getPrettyPrint([HTMLElement]);

const expect: any = global.expect;
expect.extend({toPrettyPrintTo});
Expand Down
89 changes: 84 additions & 5 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,45 @@ describe('prettyFormat()', () => {
expect(prettyFormat('\\ \\\\')).toEqual('"\\\\ \\\\\\\\"');
});

it('prints a multiline string', () => {
const val = ['line 1', 'line 2', 'line 3'].join('\n');
expect(prettyFormat(val)).toEqual('"' + val + '"');
});

it('prints a multiline string as value of object property', () => {
const polyline = {
props: {
id: 'J',
points: ['0.5,0.460', '0.5,0.875', '0.25,0.875'].join('\n'),
},
type: 'polyline',
};
const val = {
props: {
children: polyline,
},
type: 'svg',
};
expect(prettyFormat(val)).toEqual(
[
'Object {',
' "props": Object {',
' "children": Object {',
' "props": Object {',
' "id": "J",',
' "points": "0.5,0.460',
'0.5,0.875',
'0.25,0.875",',
' },',
' "type": "polyline",',
' },',
' },',
' "type": "svg",',
'}',
].join('\n'),
);
});

it('prints a symbol', () => {
const val = Symbol('symbol');
expect(prettyFormat(val)).toEqual('Symbol(symbol)');
Expand Down Expand Up @@ -323,11 +362,51 @@ describe('prettyFormat()', () => {
);
});

it('can customize indent', () => {
const val = {prop: 'value'};
expect(prettyFormat(val, {indent: 4})).toEqual(
'Object {\n "prop": "value",\n}',
);
describe('indent option', () => {
const val = [
{
id: '8658c1d0-9eda-4a90-95e1-8001e8eb6036',
text: 'Add alternative serialize API for pretty-format plugins',
type: 'ADD_TODO',
},
{
id: '8658c1d0-9eda-4a90-95e1-8001e8eb6036',
type: 'TOGGLE_TODO',
},
];
const expected = [
'Array [',
' Object {',
' "id": "8658c1d0-9eda-4a90-95e1-8001e8eb6036",',
' "text": "Add alternative serialize API for pretty-format plugins",',
' "type": "ADD_TODO",',
' },',
' Object {',
' "id": "8658c1d0-9eda-4a90-95e1-8001e8eb6036",',
' "type": "TOGGLE_TODO",',
' },',
']',
].join('\n');
test('default implicit: 2 spaces', () => {
expect(prettyFormat(val)).toEqual(expected);
});
test('default explicit: 2 spaces', () => {
expect(prettyFormat(val, {indent: 2})).toEqual(expected);
});

// Tests assume that no strings in val contain multiple adjacent spaces!
test('non-default: 0 spaces', () => {
const indent = 0;
expect(prettyFormat(val, {indent})).toEqual(
expected.replace(/ {2}/g, ' '.repeat(indent)),
);
});
test('non-default: 4 spaces', () => {
const indent = 4;
expect(prettyFormat(val, {indent})).toEqual(
expected.replace(/ {2}/g, ' '.repeat(indent)),
);
});
});

it('can customize the max depth', () => {
Expand Down
Loading

0 comments on commit 9665de2

Please sign in to comment.