From ecbebd083ce58c6be81beee773da397bce0eef93 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 26 Jul 2017 11:41:49 -0400 Subject: [PATCH] Test empty Immutable collections with {min: false} option (#4121) * Test empty Immutable collections with {min: false} option * Make neutral change to code to see if it helps CI * Fix 2 prettier lint errors --- .../src/__tests__/immutable.test.js | 66 +++++++++++++++++-- .../src/plugins/lib/print_immutable.js | 4 +- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/packages/pretty-format/src/__tests__/immutable.test.js b/packages/pretty-format/src/__tests__/immutable.test.js index 3f2103283ad4..29ad7db1da29 100644 --- a/packages/pretty-format/src/__tests__/immutable.test.js +++ b/packages/pretty-format/src/__tests__/immutable.test.js @@ -22,12 +22,18 @@ const toPrettyPrintTo = expectUtil.getPrettyPrint( expect.extend({toPrettyPrintTo}); describe('Immutable.OrderedSet plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect( Immutable.OrderedSet([]), ).toPrettyPrintTo('Immutable.OrderedSet []', {min: true}); }); + it('supports an empty collection {min: false}', () => { + expect( + Immutable.OrderedSet([]), + ).toPrettyPrintTo('Immutable.OrderedSet [\n]', {min: false}); + }); + it('supports a single string element', () => { expect( Immutable.OrderedSet(['foo']), @@ -109,12 +115,18 @@ describe('Immutable.OrderedSet plugin', () => { }); describe('Immutable.List plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect(Immutable.List([])).toPrettyPrintTo('Immutable.List []', { min: true, }); }); + it('supports an empty collection {min: false}', () => { + expect(Immutable.List([])).toPrettyPrintTo('Immutable.List [\n]', { + min: false, + }); + }); + it('supports a single string element', () => { expect(Immutable.List(['foo'])).toPrettyPrintTo('Immutable.List ["foo"]', { min: true, @@ -184,12 +196,18 @@ describe('Immutable.List plugin', () => { }); describe('Immutable.Stack plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect(Immutable.Stack([])).toPrettyPrintTo('Immutable.Stack []', { min: true, }); }); + it('supports an empty collection {min: false}', () => { + expect(Immutable.Stack([])).toPrettyPrintTo('Immutable.Stack [\n]', { + min: false, + }); + }); + it('supports a single string element', () => { expect( Immutable.Stack(['foo']), @@ -261,10 +279,16 @@ describe('Immutable.Stack plugin', () => { }); describe('Immutable.Set plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect(Immutable.Set([])).toPrettyPrintTo('Immutable.Set []', {min: true}); }); + it('supports an empty collection {min: false}', () => { + expect(Immutable.Set([])).toPrettyPrintTo('Immutable.Set [\n]', { + min: false, + }); + }); + it('supports a single string element', () => { expect(Immutable.Set(['foo'])).toPrettyPrintTo('Immutable.Set ["foo"]', { min: true, @@ -333,10 +357,16 @@ describe('Immutable.Set plugin', () => { }); describe('Immutable.Map plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect(Immutable.Map({})).toPrettyPrintTo('Immutable.Map {}', {min: true}); }); + it('supports an empty collection {min: false}', () => { + expect(Immutable.Map({})).toPrettyPrintTo('Immutable.Map {\n}', { + min: false, + }); + }); + it('supports an object with single key', () => { expect(Immutable.Map({a: 1})).toPrettyPrintTo('Immutable.Map {a: 1}', { min: true, @@ -390,12 +420,18 @@ describe('Immutable.Map plugin', () => { }); describe('Immutable.OrderedMap plugin', () => { - it('supports an empty set', () => { + it('supports an empty collection {min: true}', () => { expect( Immutable.OrderedMap({}), ).toPrettyPrintTo('Immutable.OrderedMap {}', {min: true}); }); + it('supports an empty collection {min: false}', () => { + expect( + Immutable.OrderedMap({}), + ).toPrettyPrintTo('Immutable.OrderedMap {\n}', {min: false}); + }); + it('supports an object with single key', () => { expect( Immutable.OrderedMap({a: 1}), @@ -449,7 +485,23 @@ describe('Immutable.OrderedMap plugin', () => { }); describe('Immutable.Record plugin', () => { - it('supports an empty record', () => { + it('supports an empty record {min: true}', () => { + const ABRecord = Immutable.Record({}, 'ABRecord'); + + expect(new ABRecord()).toPrettyPrintTo('Immutable.ABRecord {}', { + min: true, + }); + }); + + it('supports an empty record {min: false}', () => { + const ABRecord = Immutable.Record({}, 'ABRecord'); + + expect(new ABRecord()).toPrettyPrintTo('Immutable.ABRecord {\n}', { + min: false, + }); + }); + + it('supports a record with descriptive name', () => { const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord'); expect(new ABRecord()).toPrettyPrintTo('Immutable.ABRecord {a: 1, b: 2}', { diff --git a/packages/pretty-format/src/plugins/lib/print_immutable.js b/packages/pretty-format/src/plugins/lib/print_immutable.js index 8d9e5666583d..26f62ecfb3c7 100644 --- a/packages/pretty-format/src/plugins/lib/print_immutable.js +++ b/packages/pretty-format/src/plugins/lib/print_immutable.js @@ -16,7 +16,7 @@ const SPACE = ' '; const addKey = (isMap: boolean, key: any) => (isMap ? key + ': ' : ''); const addFinalEdgeSpacing = (length: number, edgeSpacing: string) => - length > 0 ? edgeSpacing : ''; + length !== 0 ? edgeSpacing : ''; const printImmutable = ( val: any, @@ -51,7 +51,7 @@ const printImmutable = ( } result += immutableArray.join(',' + opts.spacing); - if (!opts.min && immutableArray.length > 0) { + if (!opts.min && immutableArray.length !== 0) { result += ','; }