Skip to content

Commit

Permalink
refactor printImmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
cpenarrieta committed Mar 3, 2017
1 parent 1402e2d commit 81aebeb
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 70 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"flow-bin": "^0.39.0",
"glob": "^7.1.1",
"graceful-fs": "^4.1.11",
"immutable": "^3.8.1",
"istanbul-api": "^1.1.0",
"istanbul-lib-coverage": "^1.0.0",
"jasmine-reporters": "^2.2.0",
Expand All @@ -30,8 +31,7 @@
"react-test-renderer": "^15.4.1",
"rimraf": "^2.5.4",
"strip-ansi": "^3.0.1",
"typescript": "^2.1.4",
"immutable": "^3.8.1"
"typescript": "^2.1.4"
},
"scripts": {
"build-clean": "rm -rf ./packages/*/build",
Expand Down
44 changes: 22 additions & 22 deletions packages/pretty-format/src/__tests__/ImmutablePlugins-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Immutable.OrderedSet plugin', () => {
it('supports multiple string elements {min: false}', () => {
assertImmutableObject(
Immutable.OrderedSet(['jhon', 'mike', 'cristian']),
'Immutable.OrderedSet [\n "jhon",\n "mike",\n "cristian",\n]'
'Immutable.OrderedSet [\n "jhon",\n "mike",\n "cristian"\n]'

This comment has been minimized.

Copy link
@thymikee

thymikee Mar 3, 2017

Collaborator

This trailing comma is intentional, that's how we print arrays and object by default, see: https://github.com/facebook/jest/blob/master/packages/pretty-format/src/__tests__/pretty-format-test.js#L69

This comment has been minimized.

Copy link
@cpenarrieta

cpenarrieta Mar 3, 2017

Author Contributor

done. thanks

);
});

Expand All @@ -78,7 +78,7 @@ describe('Immutable.OrderedSet plugin', () => {
it('supports multiple integer elements {min: false}', () => {
assertImmutableObject(
Immutable.OrderedSet([1, 2, 3]),
'Immutable.OrderedSet [\n 1,\n 2,\n 3,\n]'
'Immutable.OrderedSet [\n 1,\n 2,\n 3\n]'
);
});

Expand All @@ -93,7 +93,7 @@ describe('Immutable.OrderedSet plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.OrderedSet([{a: 1, b: 2, c: 3}]),
'Immutable.OrderedSet [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n]'
'Immutable.OrderedSet [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n]'
);
});

Expand All @@ -110,7 +110,7 @@ describe('Immutable.OrderedSet plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.OrderedSet([reactComponent, reactComponent]),
'Immutable.OrderedSet [\n <Mouse>\n Hello World\n </Mouse>,\n]'
'Immutable.OrderedSet [\n <Mouse>\n Hello World\n </Mouse>\n]'
);
});
});
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Immutable.List plugin', () => {
it('supports multiple string elements {min: false}', () => {
assertImmutableObject(
Immutable.List(['jhon', 'mike', 'cristian']),
'Immutable.List [\n "jhon",\n "mike",\n "cristian",\n]'
'Immutable.List [\n "jhon",\n "mike",\n "cristian"\n]'
);
});

Expand All @@ -166,7 +166,7 @@ describe('Immutable.List plugin', () => {
it('supports multiple integer elements {min: false}', () => {
assertImmutableObject(
Immutable.List([1, 2, 3]),
'Immutable.List [\n 1,\n 2,\n 3,\n]'
'Immutable.List [\n 1,\n 2,\n 3\n]'
);
});

Expand All @@ -181,7 +181,7 @@ describe('Immutable.List plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.List([{a: 1, b: 2, c: 3}]),
'Immutable.List [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n]'
'Immutable.List [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n]'
);
});

Expand All @@ -198,7 +198,7 @@ describe('Immutable.List plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.List([reactComponent, reactComponent]),
'Immutable.List [\n <Mouse>\n Hello World\n </Mouse>,\n <Mouse>\n Hello World\n </Mouse>,\n]'
'Immutable.List [\n <Mouse>\n Hello World\n </Mouse>,\n <Mouse>\n Hello World\n </Mouse>\n]'
);
});
});
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Immutable.Stack plugin', () => {
it('supports multiple string elements {min: false}', () => {
assertImmutableObject(
Immutable.Stack(['jhon', 'mike', 'cristian']),
'Immutable.Stack [\n "jhon",\n "mike",\n "cristian",\n]'
'Immutable.Stack [\n "jhon",\n "mike",\n "cristian"\n]'
);
});

Expand All @@ -254,7 +254,7 @@ describe('Immutable.Stack plugin', () => {
it('supports multiple integer elements {min: false}', () => {
assertImmutableObject(
Immutable.Stack([1, 2, 3]),
'Immutable.Stack [\n 1,\n 2,\n 3,\n]'
'Immutable.Stack [\n 1,\n 2,\n 3\n]'
);
});

Expand All @@ -269,7 +269,7 @@ describe('Immutable.Stack plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.Stack([{a: 1, b: 2, c: 3}]),
'Immutable.Stack [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n]'
'Immutable.Stack [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n]'
);
});

Expand All @@ -286,7 +286,7 @@ describe('Immutable.Stack plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Stack([reactComponent, reactComponent]),
'Immutable.Stack [\n <Mouse>\n Hello World\n </Mouse>,\n <Mouse>\n Hello World\n </Mouse>,\n]'
'Immutable.Stack [\n <Mouse>\n Hello World\n </Mouse>,\n <Mouse>\n Hello World\n </Mouse>\n]'
);
});
});
Expand Down Expand Up @@ -327,7 +327,7 @@ describe('Immutable.Set plugin', () => {
it('supports multiple string elements {min: false}', () => {
assertImmutableObject(
Immutable.Set(['jhon', 'mike', 'cristian']),
'Immutable.Set [\n "jhon",\n "mike",\n "cristian",\n]'
'Immutable.Set [\n "jhon",\n "mike",\n "cristian"\n]'
);
});

Expand All @@ -342,7 +342,7 @@ describe('Immutable.Set plugin', () => {
it('supports multiple integer elements {min: false}', () => {
assertImmutableObject(
Immutable.Set([1, 2, 3]),
'Immutable.Set [\n 1,\n 2,\n 3,\n]'
'Immutable.Set [\n 1,\n 2,\n 3\n]'
);
});

Expand All @@ -357,7 +357,7 @@ describe('Immutable.Set plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.Set([{a: 1, b: 2, c: 3}]),
'Immutable.Set [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n]'
'Immutable.Set [\n Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n]'
);
});

Expand All @@ -374,7 +374,7 @@ describe('Immutable.Set plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Set([reactComponent, reactComponent]),
'Immutable.Set [\n <Mouse>\n Hello World\n </Mouse>,\n]'
'Immutable.Set [\n <Mouse>\n Hello World\n </Mouse>\n]'
);
});
});
Expand Down Expand Up @@ -407,7 +407,7 @@ describe('Immutable.Map plugin', () => {
it('supports an object with multiple keys {min: false}', () => {
assertImmutableObject(
Immutable.Map({a: 1, b: 2, c: 3}),
'Immutable.Map {\n a: 1,\n b: 2,\n c: 3,\n}'
'Immutable.Map {\n a: 1,\n b: 2,\n c: 3\n}'
);
});

Expand All @@ -422,7 +422,7 @@ describe('Immutable.Map plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.Map({key: {a: 1, b: 2, c: 3}}),
'Immutable.Map {\n key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n}'
'Immutable.Map {\n key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n}'
);
});

Expand All @@ -439,7 +439,7 @@ describe('Immutable.Map plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Map({a: reactComponent, b: reactComponent}),
'Immutable.Map {\n a: <Mouse>\n Hello World\n </Mouse>,\n b: <Mouse>\n Hello World\n </Mouse>,\n}'
'Immutable.Map {\n a: <Mouse>\n Hello World\n </Mouse>,\n b: <Mouse>\n Hello World\n </Mouse>\n}'
);
});
});
Expand Down Expand Up @@ -472,7 +472,7 @@ describe('Immutable.OrderedMap plugin', () => {
it('supports an object with multiple keys {min: false}', () => {
assertImmutableObject(
Immutable.OrderedMap({a: 1, b: 2, c: 3}),
'Immutable.OrderedMap {\n a: 1,\n b: 2,\n c: 3,\n}'
'Immutable.OrderedMap {\n a: 1,\n b: 2,\n c: 3\n}'
);
});

Expand All @@ -487,7 +487,7 @@ describe('Immutable.OrderedMap plugin', () => {
it('supports object elements {min: false}', () => {
assertImmutableObject(
Immutable.OrderedMap({key: {a: 1, b: 2, c: 3}}),
'Immutable.OrderedMap {\n key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n },\n}'
'Immutable.OrderedMap {\n key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n }\n}'
);
});

Expand All @@ -504,7 +504,7 @@ describe('Immutable.OrderedMap plugin', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.OrderedMap({a: reactComponent, b: reactComponent}),
'Immutable.OrderedMap {\n a: <Mouse>\n Hello World\n </Mouse>,\n b: <Mouse>\n Hello World\n </Mouse>,\n}'
'Immutable.OrderedMap {\n a: <Mouse>\n Hello World\n </Mouse>,\n b: <Mouse>\n Hello World\n </Mouse>\n}'
);
});
});
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const isList = (maybeList: Object) => {
return !!(maybeList && maybeList[IS_LIST_SENTINEL]);
};

const printImmutableList = (
const test = (object: Object) => object && isList(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -26,7 +28,4 @@ const printImmutableList = (
return printImmutable(val, print, indent, opts, colors, 'List', false);
};

module.exports = {
print: printImmutableList,
test: (object: Object) => object && isList(object),
};
module.exports = {print, test};
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const isMap = (maybeMap: Object) => {
return !!(maybeMap && maybeMap[IS_MAP_SENTINEL]);
};

const printImmutableMap = (
const test = (object: Object) => object && isMap(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -26,7 +28,4 @@ const printImmutableMap = (
return printImmutable(val, print, indent, opts, colors, 'Map', true);
};

module.exports = {
print: printImmutableMap,
test: (object: Object) => object && isMap(object),
};
module.exports = {print, test};
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableOrderedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const isOrderedMap = (maybeOrderedMap: Object) => {
return isMap(maybeOrderedMap) && isOrdered(maybeOrderedMap);
};

const printImmutableOrderedMap = (
const test = (object: Object) => object && isOrderedMap(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -35,7 +37,4 @@ const printImmutableOrderedMap = (
return printImmutable(val, print, indent, opts, colors, 'OrderedMap', true);
};

module.exports = {
print: printImmutableOrderedMap,
test: (object: Object) => object && isOrderedMap(object),
};
module.exports = {print, test};
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableOrderedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const isOrderedSet = (maybeOrderedSet: Object) => {
return isSet(maybeOrderedSet) && isOrdered(maybeOrderedSet);
};

const printImmutableOrderedSet = (
const test = (object: Object) => object && isOrderedSet(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -35,7 +37,4 @@ const printImmutableOrderedSet = (
return printImmutable(val, print, indent, opts, colors, 'OrderedSet', false);
};

module.exports = {
print: printImmutableOrderedSet,
test: (object: Object) => object && isOrderedSet(object),
};
module.exports = {print, test};
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const isSet = (maybeSet: Object) => {
return !!(maybeSet && maybeSet[IS_SET_SENTINEL]);
};

const printImmutableSet = (
const test = (object: Object) => object && isSet(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -26,7 +28,4 @@ const printImmutableSet = (
return printImmutable(val, print, indent, opts, colors, 'Set', false);
};

module.exports = {
print: printImmutableSet,
test: (object: Object) => object && isSet(object),
};
module.exports = {print, test};
9 changes: 4 additions & 5 deletions packages/pretty-format/src/plugins/ImmutableStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const isStack = (maybeStack: Object) => {
return !!(maybeStack && maybeStack[IS_STACK_SENTINEL]);
};

const printImmutableStack = (
const test = (object: Object) => object && isStack(object);

const print = (
val: Object,
print: Function,
indent: Function,
Expand All @@ -26,7 +28,4 @@ const printImmutableStack = (
return printImmutable(val, print, indent, opts, colors, 'Stack', false);
};

module.exports = {
print: printImmutableStack,
test: (object: Object) => object && isStack(object),
};
module.exports = {print, test};
25 changes: 9 additions & 16 deletions packages/pretty-format/src/plugins/printImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,21 @@ const printImmutable = (
immutableDataStructureName: string,
isMap: boolean,
) : string => {
let result = IMMUTABLE_NAMESPACE + immutableDataStructureName;
const openTag = isMap ? '{' : '[';
const closeTag = isMap ? '}' : ']';

if (val.isEmpty()) {
return result + SPACE + openTag + closeTag;
}
let result = IMMUTABLE_NAMESPACE + immutableDataStructureName +
SPACE + openTag + opts.edgeSpacing;

result += SPACE + openTag + opts.edgeSpacing;
const immutableArray = [];
val.forEach((item: any, key: any) => {
result += indent(
addKey(isMap, key) + print(item, print, indent, opts, colors)
) + ',' + opts.spacing;
immutableArray.push(
indent(addKey(isMap, key) + print(item, print, indent, opts, colors))
);
});

if (opts.min) {
//remove last comma and last spacing
return result.slice(0, -2) + opts.edgeSpacing + closeTag;
} else {
//remove last spacing
return result.slice(0, -1) + opts.edgeSpacing + closeTag;
}
result += immutableArray.join(',' + opts.spacing);

return result + opts.edgeSpacing + closeTag;
};

module.exports = printImmutable;

0 comments on commit 81aebeb

Please sign in to comment.