Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternative serialize API for pretty-format plugins #4114

Merged
merged 12 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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