Skip to content

Commit 17f47f7

Browse files
pedrottimarkcpojer
authored andcommitted
Improve Seq and remove newline from non-min empty in Immutable plugin (jestjs#4241)
* Improve Seq and remove newline from non-min empty in Immutable plugin * Edit comment * Add tests for arguments and iterator * Add assertions size toBeUndefined for lazy Seq
1 parent 8b1ee5e commit 17f47f7

2 files changed

Lines changed: 216 additions & 60 deletions

File tree

packages/pretty-format/src/__tests__/immutable.test.js

Lines changed: 139 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Immutable.OrderedSet', () => {
3434
it('supports an empty collection {min: false}', () => {
3535
expect(
3636
Immutable.OrderedSet([]),
37-
).toPrettyPrintTo('Immutable.OrderedSet [\n]', {min: false});
37+
).toPrettyPrintTo('Immutable.OrderedSet []', {min: false});
3838
});
3939

4040
it('supports a single string element', () => {
@@ -125,7 +125,7 @@ describe('Immutable.List', () => {
125125
});
126126

127127
it('supports an empty collection {min: false}', () => {
128-
expect(Immutable.List([])).toPrettyPrintTo('Immutable.List [\n]', {
128+
expect(Immutable.List([])).toPrettyPrintTo('Immutable.List []', {
129129
min: false,
130130
});
131131
});
@@ -206,7 +206,7 @@ describe('Immutable.Stack', () => {
206206
});
207207

208208
it('supports an empty collection {min: false}', () => {
209-
expect(Immutable.Stack([])).toPrettyPrintTo('Immutable.Stack [\n]', {
209+
expect(Immutable.Stack([])).toPrettyPrintTo('Immutable.Stack []', {
210210
min: false,
211211
});
212212
});
@@ -287,7 +287,7 @@ describe('Immutable.Set', () => {
287287
});
288288

289289
it('supports an empty collection {min: false}', () => {
290-
expect(Immutable.Set([])).toPrettyPrintTo('Immutable.Set [\n]', {
290+
expect(Immutable.Set([])).toPrettyPrintTo('Immutable.Set []', {
291291
min: false,
292292
});
293293
});
@@ -365,7 +365,7 @@ describe('Immutable.Map', () => {
365365
});
366366

367367
it('supports an empty collection {min: false}', () => {
368-
expect(Immutable.Map({})).toPrettyPrintTo('Immutable.Map {\n}', {
368+
expect(Immutable.Map({})).toPrettyPrintTo('Immutable.Map {}', {
369369
min: false,
370370
});
371371
});
@@ -430,7 +430,7 @@ describe('Immutable.OrderedMap', () => {
430430
it('supports an empty collection {min: false}', () => {
431431
expect(
432432
Immutable.OrderedMap({}),
433-
).toPrettyPrintTo('Immutable.OrderedMap {\n}', {min: false});
433+
).toPrettyPrintTo('Immutable.OrderedMap {}', {min: false});
434434
});
435435

436436
it('supports an object with single key', () => {
@@ -545,7 +545,7 @@ describe('Immutable.Record', () => {
545545
it('supports an empty record {min: false}', () => {
546546
const ABRecord = Immutable.Record({}, 'ABRecord');
547547

548-
expect(ABRecord()).toPrettyPrintTo('Immutable.ABRecord {\n}', {
548+
expect(ABRecord()).toPrettyPrintTo('Immutable.ABRecord {}', {
549549
min: false,
550550
});
551551
});
@@ -643,16 +643,15 @@ describe('indentation of heterogeneous collections', () => {
643643
[
644644
'Object {',
645645
' "filter": "all",',
646-
' "todos": Immutable.List [',
647-
' ],',
646+
' "todos": Immutable.List [],',
648647
'}',
649648
].join('\n'),
650649
);
651650
});
652651
test('empty Immutable.Map as child of Array', () => {
653652
const val = [Immutable.Map({})];
654653
expect(val).toPrettyPrintTo(
655-
['Array [', ' Immutable.Map {', ' },', ']'].join('\n'),
654+
['Array [', ' Immutable.Map {},', ']'].join('\n'),
656655
);
657656
});
658657

@@ -785,6 +784,27 @@ describe('maxDepth option', () => {
785784
expect(val).toPrettyPrintTo(expected, {maxDepth: 1});
786785
});
787786

787+
test('Immutable.Seq as child of Immutable.Map', () => {
788+
const val = {
789+
// ++depth === 1
790+
filter: 'all',
791+
todos: Immutable.Seq(
792+
Immutable.List([
793+
Immutable.Map({
794+
completed: true,
795+
text: 'Return if depth exceeds max',
796+
}),
797+
]),
798+
),
799+
};
800+
const expected = [
801+
'Object {',
802+
' "filter": "all",',
803+
' "todos": [Immutable.Seq],',
804+
'}',
805+
].join('\n');
806+
expect(val).toPrettyPrintTo(expected, {maxDepth: 1});
807+
});
788808
test('Immutable.Map as descendants in immutable collection', () => {
789809
const val = Immutable.Map({
790810
// ++depth === 1
@@ -817,47 +837,139 @@ describe('maxDepth option', () => {
817837
});
818838

819839
describe('Immutable.Seq', () => {
820-
const expected = '[Immutable.Seq]';
821840
it('supports an empty sequence from array {min: true}', () => {
822-
expect(Immutable.Seq([])).toPrettyPrintTo(expected, {min: true});
841+
expect(Immutable.Seq([])).toPrettyPrintTo('Immutable.Seq []', {min: true});
823842
});
824843
it('supports an empty sequence from array {min: false}', () => {
825-
expect(Immutable.Seq([])).toPrettyPrintTo(expected, {min: false});
844+
expect(Immutable.Seq([])).toPrettyPrintTo('Immutable.Seq []', {min: false});
826845
});
827846
it('supports a non-empty sequence from array {min: true}', () => {
828-
expect(Immutable.Seq([0, 1, 2])).toPrettyPrintTo(expected, {min: true});
847+
expect(
848+
Immutable.Seq([0, 1, 2]),
849+
).toPrettyPrintTo('Immutable.Seq [0, 1, 2]', {min: true});
829850
});
830851
it('supports a non-empty sequence from array {min: false}', () => {
831-
expect(Immutable.Seq([0, 1, 2])).toPrettyPrintTo(expected, {min: false});
852+
expect(
853+
Immutable.Seq([0, 1, 2]),
854+
).toPrettyPrintTo('Immutable.Seq [\n 0,\n 1,\n 2,\n]', {min: false});
855+
});
856+
857+
it('supports a non-empty sequence from arguments', () => {
858+
function returnArguments(...args) {
859+
return arguments;
860+
}
861+
expect(Immutable.Seq(returnArguments(0, 1, 2))).toPrettyPrintTo(
862+
'Immutable.Seq [\n 0,\n 1,\n 2,\n]',
863+
);
832864
});
865+
833866
it('supports an empty sequence from object {min: true}', () => {
834-
expect(Immutable.Seq({})).toPrettyPrintTo(expected, {min: true});
867+
expect(Immutable.Seq({})).toPrettyPrintTo('Immutable.Seq {}', {min: true});
835868
});
836869
it('supports an empty sequence from object {min: false}', () => {
837-
expect(Immutable.Seq({})).toPrettyPrintTo(expected, {min: false});
870+
expect(Immutable.Seq({})).toPrettyPrintTo('Immutable.Seq {}', {min: false});
838871
});
839872
it('supports a non-empty sequence from object {min: true}', () => {
840-
expect(Immutable.Seq({key: 'value'})).toPrettyPrintTo(expected, {
873+
expect(
874+
Immutable.Seq({key: 'value'}),
875+
).toPrettyPrintTo('Immutable.Seq {"key": "value"}', {
841876
min: true,
842877
});
843878
});
844879
it('supports a non-empty sequence from object {min: false}', () => {
845-
expect(Immutable.Seq({key: 'value'})).toPrettyPrintTo(expected, {
880+
expect(
881+
Immutable.Seq({key: 'value'}),
882+
).toPrettyPrintTo('Immutable.Seq {\n "key": "value",\n}', {
846883
min: false,
847884
});
848885
});
849-
it('supports a sequence from Immutable.Map', () => {
886+
887+
it('supports a sequence of entries from Immutable.Map', () => {
850888
expect(Immutable.Seq(Immutable.Map({key: 'value'}))).toPrettyPrintTo(
851-
expected,
889+
'Immutable.Seq {\n "key": "value",\n}',
852890
);
853891
});
854-
it('supports a sequence from Immutable.List', () => {
855-
expect(Immutable.Seq(Immutable.List([0, 1, 2]))).toPrettyPrintTo(expected);
892+
893+
it('supports a sequence of values from ECMAScript Set', () => {
894+
expect(Immutable.Seq(new Set([0, 1, 2]))).toPrettyPrintTo(
895+
'Immutable.Seq [\n 0,\n 1,\n 2,\n]',
896+
);
856897
});
857-
it('supports a sequence from Immutable.Set', () => {
858-
expect(Immutable.Seq(Immutable.Set([0, 1, 2]))).toPrettyPrintTo(expected);
898+
it('supports a sequence of values from Immutable.List', () => {
899+
expect(Immutable.Seq(Immutable.List([0, 1, 2]))).toPrettyPrintTo(
900+
'Immutable.Seq [\n 0,\n 1,\n 2,\n]',
901+
);
902+
});
903+
it('supports a sequence of values from Immutable.Set', () => {
904+
expect(Immutable.Seq(Immutable.Set([0, 1, 2]))).toPrettyPrintTo(
905+
'Immutable.Seq [\n 0,\n 1,\n 2,\n]',
906+
);
859907
});
860-
it('supports a sequence from Immutable.Stack', () => {
861-
expect(Immutable.Seq(Immutable.Stack([0, 1, 2]))).toPrettyPrintTo(expected);
908+
it('supports a sequence of values from Immutable.Stack', () => {
909+
expect(Immutable.Seq(Immutable.Stack([0, 1, 2]))).toPrettyPrintTo(
910+
'Immutable.Seq [\n 0,\n 1,\n 2,\n]',
911+
);
912+
});
913+
});
914+
915+
describe('Immutable.Seq lazy entries', () => {
916+
const expected = 'Immutable.Seq {…}';
917+
const object = {key0: '', key1: '1'};
918+
const filterer = value => value.length !== 0;
919+
920+
// undefined size confirms correct criteria for lazy Seq
921+
test('from object properties', () => {
922+
const val = Immutable.Seq(object).filter(filterer);
923+
expect(val.size).toBeUndefined();
924+
expect(val).toPrettyPrintTo(expected);
925+
});
926+
test('from Immutable.Map entries', () => {
927+
const val = Immutable.Seq(Immutable.Map(object)).filter(filterer);
928+
expect(val.size).toBeUndefined();
929+
expect(val).toPrettyPrintTo(expected);
930+
});
931+
});
932+
933+
describe('Immutable.Seq lazy values', () => {
934+
const expected = 'Immutable.Seq […]';
935+
const array = ['', '1', '22'];
936+
const filterer = item => item.length !== 0;
937+
938+
test('from Immutable.Range', () => {
939+
const val = Immutable.Range(1, Infinity);
940+
expect(val.size).toBe(Infinity);
941+
expect(val).toPrettyPrintTo(expected);
942+
});
943+
944+
// undefined size confirms correct criteria for lazy Seq
945+
test('from iterator', () => {
946+
function returnIterator(values) {
947+
let i = 0;
948+
return {
949+
next() {
950+
return i < values.length
951+
? {done: false, value: values[i++]}
952+
: {done: true};
953+
},
954+
};
955+
}
956+
const val = Immutable.Seq(returnIterator(array));
957+
expect(val.size).toBeUndefined();
958+
expect(val).toPrettyPrintTo(expected);
959+
});
960+
test('from array items', () => {
961+
const val = Immutable.Seq(array).filter(filterer);
962+
expect(val.size).toBeUndefined();
963+
expect(val).toPrettyPrintTo(expected);
964+
});
965+
test('from Immutable.List values', () => {
966+
const val = Immutable.Seq(Immutable.List(array)).filter(filterer);
967+
expect(val.size).toBeUndefined();
968+
expect(val).toPrettyPrintTo(expected);
969+
});
970+
test('from ECMAScript Set values', () => {
971+
const val = Immutable.Seq(new Set(array)).filter(filterer);
972+
expect(val.size).toBeUndefined();
973+
expect(val).toPrettyPrintTo(expected);
862974
});
863975
});

0 commit comments

Comments
 (0)