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 support for Immutable.Record in pretty-format #3678

Merged
merged 2 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/__tests__/plugins-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const testPath = names => {
it('gets plugins', () => {
const {getSerializers} = require('../plugins');
const plugins = getSerializers();
expect(plugins.length).toBe(9);
expect(plugins.length).toBe(10);
});

it('adds plugins from an empty array', () => testPath([]));
Expand Down
79 changes: 79 additions & 0 deletions packages/pretty-format/src/__tests__/Immutable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,82 @@ describe('Immutable.OrderedMap plugin', () => {
);
});
});

describe('Immutable.Record plugin', () => {
it('supports an empty record', () => {
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');

expect(new ABRecord()).toPrettyPrintTo('Immutable.ABRecord {a: 1, b: 2}', {
min: true,
});
});

it('supports a record without descriptive name', () => {
const ABRecord = Immutable.Record({a: 1, b: 2});

expect(new ABRecord()).toPrettyPrintTo('Immutable.Record {a: 1, b: 2}', {
min: true,
});
});

it('supports a record with values {min: true}', () => {
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');

expect(
new ABRecord({a: 3, b: 4}),
).toPrettyPrintTo('Immutable.ABRecord {a: 3, b: 4}', {min: true});
});

it('supports a record with values {min: false}', () => {
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');

expect(new ABRecord({a: 3, b: 4})).toPrettyPrintTo(
'Immutable.ABRecord {\n a: 3,\n b: 4,\n}',
);
});

it('supports a record with Map value {min: true}', () => {
const ABRecord = Immutable.Record(
{a: Immutable.Map({c: 1}), b: 2},
'ABRecord',
);

expect(
new ABRecord(),
).toPrettyPrintTo('Immutable.ABRecord {a: Immutable.Map {c: 1}, b: 2}', {
min: true,
});
});

it('supports a record with Map value {min: false}', () => {
const ABRecord = Immutable.Record(
{a: Immutable.Map({c: 1}), b: 2},
'ABRecord',
);

expect(new ABRecord()).toPrettyPrintTo(
'Immutable.ABRecord {\n a: Immutable.Map {\n c: 1,\n },\n b: 2,\n}',
);
});

it('supports imbricated Record {min: true}', () => {
const CDRecord = Immutable.Record({c: 3, d: 4}, 'CDRecord');
const ABRecord = Immutable.Record({a: new CDRecord(), b: 2}, 'ABRecord');

expect(
new ABRecord(),
).toPrettyPrintTo(
'Immutable.ABRecord {a: Immutable.CDRecord {c: 3, d: 4}, b: 2}',
{min: true},
);
});

it('supports imbricated Record {min: false}', () => {
const CDRecord = Immutable.Record({c: 3, d: 4}, 'CDRecord');
const ABRecord = Immutable.Record({a: new CDRecord(), b: 2}, 'ABRecord');

expect(new ABRecord()).toPrettyPrintTo(
'Immutable.ABRecord {\n a: Immutable.CDRecord {\n c: 3,\n d: 4,\n },\n b: 2,\n}',
);
});
});
1 change: 1 addition & 0 deletions packages/pretty-format/src/plugins/ImmutablePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ module.exports = [
require('./ImmutableStack'),
require('./ImmutableOrderedSet'),
require('./ImmutableOrderedMap'),
require('./ImmutableRecord'),
];
26 changes: 26 additions & 0 deletions packages/pretty-format/src/plugins/ImmutableRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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
*/

import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat';

const printImmutable = require('./lib/printImmutable');

const IS_RECORD = '@@__IMMUTABLE_RECORD__@@';
const test = (maybeRecord: any) => !!(maybeRecord && maybeRecord[IS_RECORD]);

const print = (
val: any,
print: Print,
indent: Indent,
opts: Options,
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'Record', true);

module.exports = ({print, test}: Plugin);
26 changes: 20 additions & 6 deletions packages/pretty-format/src/plugins/lib/printImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ const printImmutable = (
isMap: boolean,
): string => {
const [openTag, closeTag] = isMap ? ['{', '}'] : ['[', ']'];
const fullStructureName = val._name || immutableDataStructureName;

let result =
IMMUTABLE_NAMESPACE +
immutableDataStructureName +
fullStructureName +
SPACE +
openTag +
opts.edgeSpacing;

const immutableArray = [];
val.forEach((item, key) =>
immutableArray.push(
indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
),
);

if (Array.isArray(val._keys)) {
// if we have a record, we can not iterate on it directly
val._keys.forEach(key =>
immutableArray.push(
indent(
addKey(isMap, key) + print(val.get(key), print, indent, opts, colors),
),
),
);
} else {
val.forEach((item, key) =>
immutableArray.push(
indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract a function and reuse it? Something like:

const pushToImmutableArray = (item: any, key: string) => {
  immutableArray.push(
    indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
  )
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

result += immutableArray.join(',' + opts.spacing);
if (!opts.min && immutableArray.length > 0) {
Expand Down