From e782cbc9d173a62dad7092df0fc9d5fcf7388e61 Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Sun, 28 May 2017 23:32:03 +0200 Subject: [PATCH 1/2] Add support for Immutable.Record in pretty-format Fixes #3677 --- .../src/__tests__/plugins-test.js | 2 +- .../src/__tests__/Immutable-test.js | 79 +++++++++++++++++++ .../src/plugins/ImmutablePlugins.js | 1 + .../src/plugins/ImmutableRecord.js | 26 ++++++ .../src/plugins/lib/printImmutable.js | 26 ++++-- 5 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 packages/pretty-format/src/plugins/ImmutableRecord.js diff --git a/packages/jest-snapshot/src/__tests__/plugins-test.js b/packages/jest-snapshot/src/__tests__/plugins-test.js index 8aafb163f0ae..ed463a0aa5f5 100644 --- a/packages/jest-snapshot/src/__tests__/plugins-test.js +++ b/packages/jest-snapshot/src/__tests__/plugins-test.js @@ -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([])); diff --git a/packages/pretty-format/src/__tests__/Immutable-test.js b/packages/pretty-format/src/__tests__/Immutable-test.js index 6e30c7f16c3b..0820c505f978 100644 --- a/packages/pretty-format/src/__tests__/Immutable-test.js +++ b/packages/pretty-format/src/__tests__/Immutable-test.js @@ -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}', + ); + }); +}); diff --git a/packages/pretty-format/src/plugins/ImmutablePlugins.js b/packages/pretty-format/src/plugins/ImmutablePlugins.js index 7e9ab3bba89c..35af5baafa42 100644 --- a/packages/pretty-format/src/plugins/ImmutablePlugins.js +++ b/packages/pretty-format/src/plugins/ImmutablePlugins.js @@ -15,4 +15,5 @@ module.exports = [ require('./ImmutableStack'), require('./ImmutableOrderedSet'), require('./ImmutableOrderedMap'), + require('./ImmutableRecord'), ]; diff --git a/packages/pretty-format/src/plugins/ImmutableRecord.js b/packages/pretty-format/src/plugins/ImmutableRecord.js new file mode 100644 index 000000000000..f57cae9d21fa --- /dev/null +++ b/packages/pretty-format/src/plugins/ImmutableRecord.js @@ -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); diff --git a/packages/pretty-format/src/plugins/lib/printImmutable.js b/packages/pretty-format/src/plugins/lib/printImmutable.js index 64a75be6ce20..74a61a046fae 100644 --- a/packages/pretty-format/src/plugins/lib/printImmutable.js +++ b/packages/pretty-format/src/plugins/lib/printImmutable.js @@ -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)), + ), + ); + } result += immutableArray.join(',' + opts.spacing); if (!opts.min && immutableArray.length > 0) { From 854738fe423658357f225b8651b9d1957dc862aa Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Mon, 29 May 2017 10:55:30 +0200 Subject: [PATCH 2/2] Extract pushToImmutableArray function --- .../src/plugins/lib/printImmutable.js | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/pretty-format/src/plugins/lib/printImmutable.js b/packages/pretty-format/src/plugins/lib/printImmutable.js index 74a61a046fae..5b335fea9c38 100644 --- a/packages/pretty-format/src/plugins/lib/printImmutable.js +++ b/packages/pretty-format/src/plugins/lib/printImmutable.js @@ -39,21 +39,17 @@ const printImmutable = ( const immutableArray = []; + 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 => - immutableArray.push( - indent( - addKey(isMap, key) + print(val.get(key), print, indent, opts, colors), - ), - ), - ); + val._keys.forEach(key => pushToImmutableArray(val.get(key), key)); } else { - val.forEach((item, key) => - immutableArray.push( - indent(addKey(isMap, key) + print(item, print, indent, opts, colors)), - ), - ); + val.forEach((item, key) => pushToImmutableArray(item, key)); } result += immutableArray.join(',' + opts.spacing);