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 Immutable pretty-format plugins for Immutable.OrderedSet and Immu… #2899

Merged
merged 16 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"react-test-renderer": "^15.4.1",
"rimraf": "^2.5.4",
"strip-ansi": "^3.0.1",
"typescript": "^2.1.4"
"typescript": "^2.1.4",
"immutable": "^3.8.1"
Copy link
Member

Choose a reason for hiding this comment

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

can you insert this in the right alphabetical location please?

},
"scripts": {
"build-clean": "rm -rf ./packages/*/build",
Expand Down
18 changes: 16 additions & 2 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@
const chalk = require('chalk');
const prettyFormat = require('pretty-format');
const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher');

const PLUGINS = [AsymmetricMatcherPlugin];
const ImmutableOrderedSetPlugin = require('pretty-format/build/plugins/ImmutableOrderedSet');
const ImmutableListPlugin = require('pretty-format/build/plugins/ImmutableList');
const ImmutableMapPlugin = require('pretty-format/build/plugins/ImmutableMap');
const ImmutableOrderedMapPlugin = require('pretty-format/build/plugins/ImmutableOrderedMap');
const ImmutableSetPlugin = require('pretty-format/build/plugins/ImmutableSet');
const ImmutableStackPlugin = require('pretty-format/build/plugins/ImmutableStack');

const PLUGINS = [
AsymmetricMatcherPlugin,
ImmutableOrderedSetPlugin,
ImmutableListPlugin,
ImmutableOrderedMapPlugin,
ImmutableMapPlugin,
ImmutableSetPlugin,
ImmutableStackPlugin,
];
Copy link
Collaborator

Choose a reason for hiding this comment

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

We also use plugins in jest-diff and jest-snapshot.
Alos, maybe we could create ImmutablePlugins.js which exposes an array of all Immutable plugins?


export type ValueType =
| 'array'
Expand Down
319 changes: 319 additions & 0 deletions packages/pretty-format/src/__tests__/ImmutablePlugins-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
/**
* 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.
*/
/* eslint-disable max-len */

'use strict';

const React = require('react');
const prettyFormat = require('../');
const Immutable = require('immutable');
const ImmutableOrderedSet = require('../plugins/ImmutableOrderedSet');
const ImmutableList = require('../plugins/ImmutableList');
const ImmutableStack = require('../plugins/ImmutableStack');
const ImmutableSet = require('../plugins/ImmutableSet');
const ImmutableMap = require('../plugins/ImmutableMap');
const ImmutableOrderedMap = require('../plugins/ImmutableOrderedMap');

function assertImmutableObject(actual, expected, opts) {
expect(
Copy link
Member

Choose a reason for hiding this comment

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

Could we use expect.extend instead?

See http://facebook.github.io/jest/docs/expect.html#expectextendmatchers

expect(immutableValue).toPrettyPrintTo('Immutable.OrderedSet')

or something like this?

prettyFormat(actual, Object.assign({
plugins: [
ImmutableOrderedSet,
ImmutableList,
ImmutableOrderedMap,
ImmutableMap,
ImmutableSet,
ImmutableStack,
],
}, opts))
).toEqual(expected);
}

describe('Immutable.OrderedSet plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.OrderedSet([]),
'Immutable.OrderedSet []'
);
});

it('supports a single string element', () => {
assertImmutableObject(
Immutable.OrderedSet(['foo']),
'Immutable.OrderedSet [ "foo" ]'
);
});

it('supports a single integer element', () => {
assertImmutableObject(
Immutable.OrderedSet([1]),
'Immutable.OrderedSet [ 1 ]'
);
});

it('supports multiple string elements', () => {
assertImmutableObject(
Immutable.OrderedSet(['jhon', 'mike', 'cristian']),
'Immutable.OrderedSet [ "jhon", "mike", "cristian" ]'
Copy link
Collaborator

Choose a reason for hiding this comment

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

For {min: true} I'd rather print:

Immutable.OrderedSet ["jhon", "mike", "cristian"]

and for {min: false}:

Immutable.OrderedSet [
  "jhon", 
  "mike", 
  "cristian",
]

Just like it works for arrays and objects now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The same applies to all other Immutable entities

);
});

it('supports multiple integer elements', () => {
assertImmutableObject(
Immutable.OrderedSet([1, 2, 3]),
'Immutable.OrderedSet [ 1, 2, 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.OrderedSet([{a: 1, b: 2, c: 3}]),
'Immutable.OrderedSet [ Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.OrderedSet([reactComponent, reactComponent]),
'Immutable.OrderedSet [ <Mouse>\n Hello World\n</Mouse> ]'
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case you should include ReactElement and ReactTestComponent in plugins

);
});
});

describe('Immutable.List plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.List([]),
'Immutable.List []'
);
});

it('supports a single string element', () => {
assertImmutableObject(
Immutable.List(['foo']),
'Immutable.List [ "foo" ]'
);
});

it('supports a single integer element', () => {
assertImmutableObject(
Immutable.List([1]),
'Immutable.List [ 1 ]'
);
});

it('supports multiple string elements', () => {
assertImmutableObject(
Immutable.List(['jhon', 'mike', 'cristian']),
'Immutable.List [ "jhon", "mike", "cristian" ]'
);
});

it('supports multiple integer elements', () => {
assertImmutableObject(
Immutable.List([1, 2, 3]),
'Immutable.List [ 1, 2, 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.List([{a: 1, b: 2, c: 3}]),
'Immutable.List [ Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.List([reactComponent, reactComponent]),
'Immutable.List [ <Mouse>\n Hello World\n</Mouse>, <Mouse>\n Hello World\n</Mouse> ]'
);
});
});

describe('Immutable.Stack plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.Stack([]),
'Immutable.Stack []'
);
});

it('supports a single string element', () => {
assertImmutableObject(
Immutable.Stack(['foo']),
'Immutable.Stack [ "foo" ]'
);
});

it('supports a single integer element', () => {
assertImmutableObject(
Immutable.Stack([1]),
'Immutable.Stack [ 1 ]'
);
});

it('supports multiple string elements', () => {
assertImmutableObject(
Immutable.Stack(['jhon', 'mike', 'cristian']),
'Immutable.Stack [ "jhon", "mike", "cristian" ]'
);
});

it('supports multiple integer elements', () => {
assertImmutableObject(
Immutable.Stack([1, 2, 3]),
'Immutable.Stack [ 1, 2, 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.Stack([{a: 1, b: 2, c: 3}]),
'Immutable.Stack [ Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Stack([reactComponent, reactComponent]),
'Immutable.Stack [ <Mouse>\n Hello World\n</Mouse>, <Mouse>\n Hello World\n</Mouse> ]'
);
});
});

describe('Immutable.Set plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.Set([]),
'Immutable.Set []'
);
});

it('supports a single string element', () => {
assertImmutableObject(
Immutable.Set(['foo']),
'Immutable.Set [ "foo" ]'
);
});

it('supports a single integer element', () => {
assertImmutableObject(
Immutable.Set([1]),
'Immutable.Set [ 1 ]'
);
});

it('supports multiple string elements', () => {
assertImmutableObject(
Immutable.Set(['jhon', 'mike', 'cristian']),
'Immutable.Set [ "jhon", "mike", "cristian" ]'
);
});

it('supports multiple integer elements', () => {
assertImmutableObject(
Immutable.Set([1, 2, 3]),
'Immutable.Set [ 1, 2, 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.Set([{a: 1, b: 2, c: 3}]),
'Immutable.Set [ Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Set([reactComponent, reactComponent]),
'Immutable.Set [ <Mouse>\n Hello World\n</Mouse> ]'
);
});
});

describe('Immutable.Map plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.Map({}),
'Immutable.Map []'
);
});

it('supports an object with single key', () => {
assertImmutableObject(
Immutable.Map({a: 1}),
'Immutable.Map [ a: 1 ]'
);
});

it('supports an object with multiple keys', () => {
assertImmutableObject(
Immutable.Map({a: 1, b: 2, c: 3}),
'Immutable.Map [ a: 1, b: 2, c: 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.Map({key: {a: 1, b: 2, c: 3}}),
'Immutable.Map [ key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.Map({a: reactComponent, b: reactComponent}),
'Immutable.Map [ a: <Mouse>\n Hello World\n</Mouse>, b: <Mouse>\n Hello World\n</Mouse> ]'
);
});
});

describe('Immutable.OrderedMap plugin', () => {
it('supports an empty set', () => {
assertImmutableObject(
Immutable.OrderedMap({}),
'Immutable.OrderedMap []'
);
});

it('supports an object with single key', () => {
assertImmutableObject(
Immutable.OrderedMap({a: 1}),
'Immutable.OrderedMap [ a: 1 ]'
);
});

it('supports an object with multiple keys', () => {
assertImmutableObject(
Immutable.OrderedMap({a: 1, b: 2, c: 3}),
'Immutable.OrderedMap [ a: 1, b: 2, c: 3 ]'
);
});

it('supports object elements', () => {
assertImmutableObject(
Immutable.OrderedMap({key: {a: 1, b: 2, c: 3}}),
'Immutable.OrderedMap [ key: Object {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n} ]'
);
});

it('supports React components', () => {
const reactComponent = React.createElement('Mouse', null, 'Hello World');
assertImmutableObject(
Immutable.OrderedMap({a: reactComponent, b: reactComponent}),
'Immutable.OrderedMap [ a: <Mouse>\n Hello World\n</Mouse>, b: <Mouse>\n Hello World\n</Mouse> ]'
);
});
});
32 changes: 32 additions & 0 deletions packages/pretty-format/src/plugins/ImmutableList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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
*/

'use strict';

const printImmutable = require('./printImmutable');
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';

const isList = (maybeList: Object) => {
return !!(maybeList && maybeList[IS_LIST_SENTINEL]);
};

const printImmutableList = (
val: Object,
print: Function,
indent: Function,
opts: Object,
colors: Object
Copy link
Member

Choose a reason for hiding this comment

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

the flow typing here could be a lot stronger. @cpenarrieta we just added flow types to pretty-format itself – mind sending a second, follow-up PR, where you use the types from the main module? You can extract it into a types.js file like we do in other packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll work on it
thanks

) => {
return printImmutable(val, print, indent, opts, colors, 'List', false);
};

module.exports = {
print: printImmutableList,
test: (object: Object) => object && isList(object),
};
Loading