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 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
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);
18 changes: 14 additions & 4 deletions packages/pretty-format/src/plugins/lib/printImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ 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) =>

const pushToImmutableArray = (item: any, key: string) => {
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 => pushToImmutableArray(val.get(key), key));
Copy link
Member

Choose a reason for hiding this comment

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

We are looking at private fields here and with ._name above. I'm worried about changes in Immutable that will break this and therefore break snapshots.

cc @leebyron do you have any input for us on what we should do here? Do you think this is fine for pretty-printing?

Copy link
Contributor

@leebyron leebyron May 30, 2017

Choose a reason for hiding this comment

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

I think this is fine as long as someone is prepared to update this code if Immutable.js implementation details ever change, which is entirely possible. Though I think there are options for improvements

For ._name, you might want to sniff for Immutable v4.0 which offers Record.getDescriptiveName(record), otherwise use this fallback implementation, however note that your implementation differs from Immutable.js's.

I think ._keys is less safe to use. Since you just want to iterate over these, you can probably cast to a lazy Seq first. So lines 48-53 here can be replaced with:

val.toSeq().forEach(pushToImmutableArray)

} else {
val.forEach((item, key) => pushToImmutableArray(item, key));
}

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