Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a35daa5

Browse files
renchapcpojer
authored andcommittedMay 30, 2017
Add support for Immutable.Record in pretty-format (jestjs#3678)
* Add support for Immutable.Record in pretty-format Fixes jestjs#3677 * Extract pushToImmutableArray function
1 parent 18ee091 commit a35daa5

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed
 

‎packages/jest-snapshot/src/__tests__/plugins-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const testPath = names => {
2929
it('gets plugins', () => {
3030
const {getSerializers} = require('../plugins');
3131
const plugins = getSerializers();
32-
expect(plugins.length).toBe(9);
32+
expect(plugins.length).toBe(10);
3333
});
3434

3535
it('adds plugins from an empty array', () => testPath([]));

‎packages/pretty-format/src/__tests__/Immutable-test.js

+79
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,82 @@ describe('Immutable.OrderedMap plugin', () => {
445445
);
446446
});
447447
});
448+
449+
describe('Immutable.Record plugin', () => {
450+
it('supports an empty record', () => {
451+
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');
452+
453+
expect(new ABRecord()).toPrettyPrintTo('Immutable.ABRecord {a: 1, b: 2}', {
454+
min: true,
455+
});
456+
});
457+
458+
it('supports a record without descriptive name', () => {
459+
const ABRecord = Immutable.Record({a: 1, b: 2});
460+
461+
expect(new ABRecord()).toPrettyPrintTo('Immutable.Record {a: 1, b: 2}', {
462+
min: true,
463+
});
464+
});
465+
466+
it('supports a record with values {min: true}', () => {
467+
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');
468+
469+
expect(
470+
new ABRecord({a: 3, b: 4}),
471+
).toPrettyPrintTo('Immutable.ABRecord {a: 3, b: 4}', {min: true});
472+
});
473+
474+
it('supports a record with values {min: false}', () => {
475+
const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord');
476+
477+
expect(new ABRecord({a: 3, b: 4})).toPrettyPrintTo(
478+
'Immutable.ABRecord {\n a: 3,\n b: 4,\n}',
479+
);
480+
});
481+
482+
it('supports a record with Map value {min: true}', () => {
483+
const ABRecord = Immutable.Record(
484+
{a: Immutable.Map({c: 1}), b: 2},
485+
'ABRecord',
486+
);
487+
488+
expect(
489+
new ABRecord(),
490+
).toPrettyPrintTo('Immutable.ABRecord {a: Immutable.Map {c: 1}, b: 2}', {
491+
min: true,
492+
});
493+
});
494+
495+
it('supports a record with Map value {min: false}', () => {
496+
const ABRecord = Immutable.Record(
497+
{a: Immutable.Map({c: 1}), b: 2},
498+
'ABRecord',
499+
);
500+
501+
expect(new ABRecord()).toPrettyPrintTo(
502+
'Immutable.ABRecord {\n a: Immutable.Map {\n c: 1,\n },\n b: 2,\n}',
503+
);
504+
});
505+
506+
it('supports imbricated Record {min: true}', () => {
507+
const CDRecord = Immutable.Record({c: 3, d: 4}, 'CDRecord');
508+
const ABRecord = Immutable.Record({a: new CDRecord(), b: 2}, 'ABRecord');
509+
510+
expect(
511+
new ABRecord(),
512+
).toPrettyPrintTo(
513+
'Immutable.ABRecord {a: Immutable.CDRecord {c: 3, d: 4}, b: 2}',
514+
{min: true},
515+
);
516+
});
517+
518+
it('supports imbricated Record {min: false}', () => {
519+
const CDRecord = Immutable.Record({c: 3, d: 4}, 'CDRecord');
520+
const ABRecord = Immutable.Record({a: new CDRecord(), b: 2}, 'ABRecord');
521+
522+
expect(new ABRecord()).toPrettyPrintTo(
523+
'Immutable.ABRecord {\n a: Immutable.CDRecord {\n c: 3,\n d: 4,\n },\n b: 2,\n}',
524+
);
525+
});
526+
});

‎packages/pretty-format/src/plugins/ImmutablePlugins.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ module.exports = [
1515
require('./ImmutableStack'),
1616
require('./ImmutableOrderedSet'),
1717
require('./ImmutableOrderedMap'),
18+
require('./ImmutableRecord'),
1819
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat';
12+
13+
const printImmutable = require('./lib/printImmutable');
14+
15+
const IS_RECORD = '@@__IMMUTABLE_RECORD__@@';
16+
const test = (maybeRecord: any) => !!(maybeRecord && maybeRecord[IS_RECORD]);
17+
18+
const print = (
19+
val: any,
20+
print: Print,
21+
indent: Indent,
22+
opts: Options,
23+
colors: Colors,
24+
) => printImmutable(val, print, indent, opts, colors, 'Record', true);
25+
26+
module.exports = ({print, test}: Plugin);

‎packages/pretty-format/src/plugins/lib/printImmutable.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,29 @@ const printImmutable = (
2828
isMap: boolean,
2929
): string => {
3030
const [openTag, closeTag] = isMap ? ['{', '}'] : ['[', ']'];
31+
const fullStructureName = val._name || immutableDataStructureName;
32+
3133
let result =
3234
IMMUTABLE_NAMESPACE +
33-
immutableDataStructureName +
35+
fullStructureName +
3436
SPACE +
3537
openTag +
3638
opts.edgeSpacing;
3739

3840
const immutableArray = [];
39-
val.forEach((item, key) =>
41+
42+
const pushToImmutableArray = (item: any, key: string) => {
4043
immutableArray.push(
4144
indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
42-
),
43-
);
45+
);
46+
};
47+
48+
if (Array.isArray(val._keys)) {
49+
// if we have a record, we can not iterate on it directly
50+
val._keys.forEach(key => pushToImmutableArray(val.get(key), key));
51+
} else {
52+
val.forEach((item, key) => pushToImmutableArray(item, key));
53+
}
4454

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

0 commit comments

Comments
 (0)
Please sign in to comment.