-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from 9 commits
899ef77
0ad8b38
4c75854
2cd9b50
e981653
457d6b6
1e49f13
e7727d2
abf1523
bf7cf8a
c12b8ac
a4bdc74
1402e2d
81aebeb
0f58eb2
7d6143d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also use plugins in |
||
|
||
export type ValueType = | ||
| 'array' | ||
|
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use See http://facebook.github.io/jest/docs/expect.html#expectextendmatchers
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" ]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Immutable.OrderedSet ["jhon", "mike", "cristian"] and for Immutable.OrderedSet [
"jhon",
"mike",
"cristian",
] Just like it works for arrays and objects now. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> ]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> ]' | ||
); | ||
}); | ||
}); |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll work on it |
||
) => { | ||
return printImmutable(val, print, indent, opts, colors, 'List', false); | ||
}; | ||
|
||
module.exports = { | ||
print: printImmutableList, | ||
test: (object: Object) => object && isList(object), | ||
}; |
There was a problem hiding this comment.
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?