-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
2253 lines (2110 loc) · 78.4 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var _ = require('./lodash-plus.js');
var assert = require('assert');
var falsyAndTruthy = {
'false': false,
'null': null,
'undefined': undefined,
'0': 0,
'empty string': '',
'NaN': NaN,
'1': 1,
'\'a\'': 'a',
'{}': {},
'[]': [],
'true': true
};
var falsyParams = ['a', 'b', 'c', 'd', 'e', 'f'];
var truthyParams = ['g', 'h', 'i', 'j', 'k'];
var nonExistantParams = ['l', 'm', 'n'];
describe('lodash-plus', function () {
describe('isFalsy', function() {
_.each(_.values(falsyAndTruthy), function (val) {
it('should return ' + !Boolean(val) + ' when called with ' + val, function () {
assert.equal(_.isFalsy(val), !Boolean(val));
});
})
});
describe('isTruthy', function() {
it('should return the same values as Boolean()', function () {
_.each(_.values(falsyAndTruthy), function (val) {
assert.equal(_.isTruthy(val), Boolean(val));
})
});
});
describe('isDefined', function () {
var testObject = {'a': true};
var testArray = [1, 2];
it('should return false when passed undefined', function () {
assert.equal(_.isDefined(undefined), false);
});
it('should return false when passed void 0', function () {
assert.equal(_.isDefined(void 0), false);
});
it('should return false when passed an object property that does NOT exist', function () {
assert.equal(_.isDefined(testObject.b), false);
});
it('should return false when passed an array index that does NOT exist', function () {
assert.equal(_.isDefined(testArray[2]), false);
});
it('should return true when passed !undefined', function () {
assert.equal(_.isDefined(!undefined), true);
});
it('should return true when passed null', function () {
assert.equal(_.isDefined(null), true);
});
it('should return true when passed false', function () {
assert.equal(_.isDefined(false), true);
});
it('should return true when passed an empty string', function () {
assert.equal(_.isDefined(''), true);
});
it('should return true when passed an object property that does exist', function () {
assert.equal(_.isDefined(testObject.a), true);
});
it('should return true when passed an array index that does exist', function () {
assert.equal(_.isDefined(testArray[1]), true);
});
});
describe('isNullOrUndefined', function () {
_.each(falsyAndTruthy, function (val, key) {
if (_.overSome(_.isNull, _.isUndefined)(val)) {
it('should return true when called with ' + key, function () {
assert.equal(_.isNullOrUndefined(val), true);
});
}
else {
it('should return false when called with ' + key, function () {
assert.equal(_.isNullOrUndefined(val), false);
});
}
});
});
describe('includesAny', function () {
describe('when called with two strings', function () {
var searchInA = ['abc', 'def', 'ghi', 'jkl'];
var searchInB = ['xxx', 'yyy', 'zzz'];
var searchForA = ['qqqadgjqqq', 'bqeqhqk', 'qqqlifc'];
var searchForB = ['x', 'y', 'z'];
describe('and the first string contains at least one letter from the second', function () {
it('should return true', function () {
_.each(searchInA, function (searchInString) {
_.each(searchForA, function (searchForString) {
assert.equal(_.includesAny(searchInString, searchForString), true);
});
});
});
});
describe('and the first string has length > 1 and contains no letters from the second with length of 1', function () {
it('should return false', function () {
_.each(searchInA, function (searchInString) {
_.each(searchForB, function (searchForString) {
assert.equal(_.includesAny(searchInString, searchForString), false);
});
});
});
});
describe('and the first string contains no letters from the second, which is longer than the first', function () {
it('should return false', function () {
_.each(searchInB, function (searchInString) {
_.each(searchForA, function (searchForString) {
assert.equal(_.includesAny(searchInString, searchForString), false);
});
});
});
});
});
describe('pickTruthy', function() {
var falsyAndTruthy = {
a: false,
b: null,
c: undefined,
d: 0,
e: '',
f: NaN,
g: 1,
h: 'a',
i: {},
j: [],
k: true
};
var falsyParams = ['a', 'b', 'c', 'd', 'e', 'f'];
var truthyParams = ['g', 'h', 'i', 'j', 'k'];
var nonExistantParams = ['l', 'm', 'n'];
describe('when one param passed in', function () {
it('should pick all truthy values if one param passed in', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy),
_.pick(falsyAndTruthy, truthyParams)
);
});
});
describe('when two params passed in and second is a string', function () {
it('should return an empty object if no property with the string name exists', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, ''),
{}
);
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, 'z'),
{}
);
});
_.each(falsyParams, function (prop) {
it('should return an empty object if the property with the string name is ' + falsyAndTruthy[prop], function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, prop),
{}
);
});
});
_.each(truthyParams, function (prop) {
it('should return an object containing only the property if the property with the string name is truthy', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, prop),
_.set({}, prop, falsyAndTruthy[prop])
);
});
});
});
describe('when two params passed in and second is an array', function () {
it('should return an empty object if no properties in the array exist', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, nonExistantParams),
{}
);
});
it('should return an empty object if the properties are falsy', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, falsyParams.slice(0, 3)),
{}
);
});
it('should return an object containing only the properties if they are truthy', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, truthyParams.slice(0, 3)),
_.pick(falsyAndTruthy, truthyParams.slice(0, 3))
);
});
it('should return an object containing only the truthy params', function () {
assert.deepEqual(
_.pickTruthy(falsyAndTruthy, nonExistantParams.concat(truthyParams.slice(1, 4)).concat(falsyParams.slice(2, 4))),
_.pick(falsyAndTruthy, truthyParams.slice(1, 4))
);
});
});
});
describe('when called with an array of objects and an array of strings', function () {
// TODO: is this the behaviour we want for this case?
var testObjects = [
{a: true, b: null, c: 1},
{d: false, e: NaN, f: 'a'},
{g: true, h: undefined, i: []},
{j: false, k: 0, l: _.noop}
];
describe('and at least one of the test objects has a property key matching one of the strings', function () {
var matchingTestStrings = [['x', 'b', 'q'], ['g', 'l', 'c'], ['z', 'zzz', 'h']];
it('should return true', function () {
_.each(matchingTestStrings, function (stringArray) {
assert.equal(_.includesAny(testObjects, stringArray), true);
});
});
});
describe('and none of the test objects have a property key matching one of the strings', function () {
var unMatchingTestStrings = [['x', 'y', 'z'], ['ab', 'kl'], ['ll']];
it('should return false', function () {
_.each(unMatchingTestStrings, function (stringArray) {
assert.equal(_.includesAny(testObjects, stringArray), false);
});
});
});
});
describe('when called with arrays of non-object values', function () {
_.each({
'number': {
first: [1, 2, 3],
match: [4, 2, 6],
noMatch: [7, 8, 9]
},
'string': {
first: ['a', 'b', 'c'],
match: ['d', 'b', 'f'],
noMatch: ['g', 'h', 'i']
},
'assorted': {
first: [true, NaN, undefined],
match: [false, 1, NaN],
noMatch: [null, 0, '1']
}
}, function (config, arrayType) {
describe('and the arrays are arrays of ' + arrayType + ' values and at least one element of each are equal', function () {
it('should return true', function () {
assert.equal(_.includesAny(config.first, config.match), true);
});
});
describe('and the arrays are arrays of ' + arrayType + ' values and no elements of either are equal', function () {
it('should return false', function () {
assert.equal(_.includesAny(config.first, config.noMatch), false);
});
});
});
});
describe('when searching for an array of values within an object', function () {
_.each({
'number': {
object: {a: 1, b: 2, c:3},
match: [4, 2, 6],
noMatch: [7, 8, 9]
},
'string': {
object: {a: 'a', b: 'b', c:'c'},
match: ['d', 'b', 'f'],
noMatch: ['g', 'h', 'i']
},
'assorted': {
object: {a: true, b: NaN, c:undefined},
match: [false, 1, NaN],
noMatch: [null, 0, '1']
}
}, function (config, arrayType) {
describe('and the array is an array of ' + arrayType + ' values with at least one element in the object', function () {
it('should return true', function () {
assert.equal(_.includesAny(config.object, config.match), true);
});
});
describe('and the array is an array of ' + arrayType + ' values with no elements in the object', function () {
it('should return false', function () {
assert.equal(_.includesAny(config.object, config.noMatch), false);
});
});
});
});
});
describe('disjoint', function () {
_.each({
'don\'t': {
'strings': {
A: ['a', 'b', 'c'],
B: ['d', 'e', 'f']
},
'numbers': {
A: [1, 2, 3],
B: [4, 5, 6]
},
'booleans': {
A: [false, false, false],
B: [true, true, true]
},
'mixed types': {
A: [false, null, [2], 'the'],
B: ['th', true, [null], 2]
}
},
'do': {
'strings': {
A: ['a', 'b', 'c'],
B: ['d', 'e', 'a']
},
'numbers': {
A: [1, 4, 3],
B: [4, 5, 6]
},
'booleans': {
A: [false, false, false],
B: [true, false, true]
},
'mixed types': {
A: [false, null, [2], 'the'],
B: ['th', true, null, 2]
}
}
}, function (config, doOrDont) {
var result = doOrDont !== 'do';
_.each(config, function (arrays, type) {
describe('when called with arrays of ' + type + ' that ' + doOrDont + ' intersect', function () {
it('should return ' + result, function () {
assert.equal(_.disjoint(arrays.A, arrays.B), result);
});
});
});
});
});
describe('isEvery', function () {
var testPredicates = {
moreThanFive: function (obj) {return _.size(obj) > 5;},
endsWithX: function (str) {return _.endsWith(str, 'x');}
};
_.each({
'isString': {
True: ['a', 'b', 'c', 'd'],
False: ['a', 'b', ['c'], 'd']
},
'isArray': {
True: [[1, 2], [3, 4], [true], [null, false]],
False: [[1, 2], {'0': 3, '1': 4}, [true], [null, false]]
},
'isNumber': {
True: [-1, 0, 1.5, 10000000],
False: [-1, 0, '0', 1.5, 10000000]
},
'isPlainObject': {
True: [{a: 1}, {b: {c: 'd'}}, {e: [123]}, {}],
False: [{a: 1}, {b: {c: 'd'}}, [123], {}]
},
'moreThanFive': {
True: [{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}],
False: [{a: 1, b: 2, c: 3, d: 4, e: 5}]
},
'endsWithX': {
True: ['abcx', 'xyzx', '00000x', 'x'],
False: ['abcx', 'xyzx', '00000xy', 'x']
}
}, function (expectations, funcName) {
var func = _.isEvery(_[funcName] || testPredicates[funcName]);
describe('when called with the ' + funcName + ' function', function () {
it('should return true if every array element returns true when passed to the predicate', function () {
assert.equal(func(expectations.True), true);
});
it('should return false if any array element returns false when passed to the predicate', function () {
assert.equal(func(expectations.False), false);
});
});
var stringAfterIs = _.trimStart(funcName, 'is');
describe('when called with the string ' + stringAfterIs, function () {
if (_[funcName]) {
it('should return true if every array element returns true when passed to the corresponding lodash "is" function', function () {
assert.equal(_.isEvery(stringAfterIs)(expectations.True), true);
});
it('should return false if any array element returns false when passed to the corresponding lodash "is" function', function () {
assert.equal(_.isEvery(stringAfterIs)(expectations.False), false);
});
}
// else {
// it('should throw an error if the string does not correspond to a lodash "is" function', function () {
// assert.throws(function () {_.isEvery(stringAfterIs)(expectations.True)}, Error);
// });
// }
});
});
});
describe('hasAny', function () {
var testObject = {a: 1, b: false, c: undefined, d: {}};
describe('when the object has keys matching at least one array string', function () {
var matchingArray = ['aa', 'bb', 'c', 'dd'];
it('should return true', function () {
assert.equal(_.hasAny(testObject, matchingArray), true);
});
});
describe('when the object has no keys matching any array string', function () {
var unMatchingArray = ['aa', 'bb', 'cc', 'dd'];
it('should return false', function () {
assert.equal(_.hasAny(testObject, unMatchingArray), false);
});
});
});
describe('hasAll', function () {
var testObject = {a: true, b: null, c: 1, d: false, e: NaN, f: 'a'};
var trueCases = [['a', 'b', 'd'], ['d', 'e', 'f'], ['c'], _.keys(testObject)];
var falseCases = [['a', 'b', 'm'], ['g', 'h', 'i'], ['z'], _.keys(testObject).concat('q')];
describe('when the object has a key that matches each string in the array', function () {
it('should return true', function () {
_.each(trueCases, function (array) {
assert.equal(_.hasAll(testObject, array), true);
});
});
});
describe('when the there is a string in the array that does not match an object key', function () {
it('should return false', function () {
_.each(falseCases, function (array) {
assert.equal(_.hasAll(testObject, array), false);
});
});
});
});
describe('isIntable', function () {
var intables = [-18, 0, 19, '-18', '0', '19', [2], ['2'], -0, '-0', 1.0, -1.0, '1.0', '-1.0', ['-0.0']];
var nonIntables = [-18.1, null, 19.5, {0:1, length:1}, undefined, [null], '-18.5', [], {}, 'a', NaN, false, [1, 3], ['1', '3'], {1: 1}, [[2]], ['-0.5']];
_.each(intables, function (val) {
it('should return true when passed the ' + typeof val + ' ' + val, function () {
assert.equal(_.isIntable(val), true);
});
});
_.each(nonIntables, function (val) {
it('should return false when passed the ' + typeof val + ' ' + val, function () {
assert.equal(_.isIntable(val), false);
});
});
});
describe('argsLength', function () {
describe('when callback is not a predicate', function () {
var callbacks = [
_.identity,
_.partial(_.add, 10),
_.partial(_.repeat, 'o')
];
var funcs = _.map(callbacks, _.argsLength);
it('should use the number of arguments in the callback', function () {
assert.equal(funcs[0]('a', false), 2);
assert.equal(funcs[1]('a', false, null, 1000), 14);
assert.equal(funcs[2](1, 2, 3, 4, 5), 'ooooo');
});
});
describe('when callback is a predicate', function () {
var predicates = [
_.partial(_.lt, 1),
_.partial(_.isEqual, 4),
_.isFalsy
];
var funcs = _.map(predicates, _.argsLength);
it('should return true if the number of arguments would return true when passed to the predicate', function () {
assert.equal(funcs[0]('a', false), true);
assert.equal(funcs[1]('a', false, null, 1000), true);
assert.equal(funcs[2](), true);
});
it('should return false if the number of arguments would return false when passed to the predicate', function () {
assert.equal(funcs[0]('a'), false);
assert.equal(funcs[0](), false);
assert.equal(funcs[1]('a', null, 1000), false);
assert.equal(funcs[2]({}, []), false);
});
});
describe('when no callback provided', function () {
it('should return a function that returns the number of args', function () {
assert.equal(_.argsLength()(1, 2, 3), 3);
assert.equal(_.argsLength()(true, false, null, undefined, NaN), 5);
assert.equal(_.argsLength()(), 0);
assert.equal(_.argsLength().apply(null, _.range(100)), 100);
});
});
});
describe('fullSize', function () {
var obj1 = {a: 1, b: 2, c: 3};
var obj2 = {a: {x: 1}, b: {x: 2}, c: {x: 3}};
var obj3 = {a: {b: {c: {d: {e: {f: {g: {h: 10}}}}}}}};
var obj4 = {x: obj1, y: obj2, z: obj3};
_.each({
'when an object has first level properties only': {
arg: obj1,
expectedResult: 3,
should: 'return the number of properties'
},
'when an object has first and second level properties': {
arg: obj2,
expectedResult: 6,
should: 'return the sum of first and second level properties'
},
'when an object has deeply nested properties': {
arg: obj3,
expectedResult: 8,
should: 'return a count for each propery on the path'
},
'when an object has multiple deeply nested properties': {
arg: obj4,
expectedResult: 20,
should: 'return a count for each propery on every path'
}
}, function (config, desc) {
describe(desc, function () {
it(config.should, function () {
assert.equal(_.fullSize(config.arg), config.expectedResult)
});
});
});
});
describe('extendAll', function () {
// TODO: simplify these spec and add cases for empty collection arguments
// also add separate tests for checking that collection is modified
var collection = [{a: 1}, {b: 2}, {c: 3}];
var source1 = {d: 4};
var source2 = {e: {f: 5}, g: {h: 5}};
var source3 = {a: 10, b: 10, c: 10};
var source4 = {g: {h: true}};
var source5 = {d: null, g: {z: false}};
var source6 = 123;
var extendAllClone = _.spread(_.collCloner(_.extendAll));
var mapClone = _.collCloner(_.map)
_.each({
'with one source with one non-deep property': {
collectionAndSources: [collection, source1],
resultExpectationModifier: function (obj) {
return _.set(obj, 'd', 4);
}
},
'with one source with two deep properties': {
collectionAndSources: [collection, source2],
resultExpectationModifier: function (obj) {
_.set(obj, 'e.f', 5);
_.set(obj, 'g.h', 5);
return obj;
}
},
'with one source with three non-deep properties': {
collectionAndSources: [collection, source3],
resultExpectationModifier: _.constant(source3)
},
'with two sources with a deep properties': {
collectionAndSources: [collection, source2, source4],
resultExpectationModifier: function (obj) {
_.set(obj, 'e.f', 5);
_.set(obj, 'g.h', true);
return obj;
}
},
'with three sources one non-deep and deep properties': {
collectionAndSources: [collection, source1, source2, source5],
resultExpectationModifier: function (obj) {
_.set(obj, 'd', null);
_.set(obj, 'e.f', 5);
_.set(obj, 'g.z', false);
return obj;
}
}
}, function (config, desc) {
describe('when called with ' + desc, function () {
it('should return the collection with each object modified to include the sources', function () {
assert.deepEqual(extendAllClone(config.collectionAndSources), mapClone(collection, config.resultExpectationModifier));
});
});
});
it('should throw an error if called with any value that is not a plain object', function () {
assert.throws(function () {extendAllClone([collection, source1, source2, source6])}, Error);
});
});
describe('collCloner', function () {
// TODO: rewrite
var collection = [{a: 1}, {b: 2}, {c: 3}];
var collCloneMappedCollection = _.collCloner(_.map)(collection, function (obj) {
return _.set(obj, 'd', 4);
});
var expectedMappedCollection = [{a: 1, d: 4}, {b: 2, d: 4}, {c: 3, d: 4}];
var expectedUnMappedCollection = [{a: 1}, {b: 2}, {c: 3}];
it('should return a function that has the same result as the callback', function () {
assert.deepEqual(collCloneMappedCollection, expectedMappedCollection);
});
it('should not modify the passed in collection', function () {
assert.deepEqual(collection, expectedUnMappedCollection);
});
});
describe('allPaths', function () {
var testObj1 = {a: {x: 1, y: 2, z: 3}, b: {x: 1, y: 2, z: 3}, c: {x: 1, y: 2, z: {zz: 3, zzz: {abc: 10}}}};
var testObj2 = {a: {b: {c: {d: {e: {f: {g: 1}}}}}}};
var testObj3 = {a: 1, b: 2, c: 3, d: undefined, e: undefined};
var expectedPaths1 = [['a'], ['b'], ['c'], ['a', 'x'], ['a', 'y'], ['a', 'z'], ['b', 'x'], ['b', 'y'], ['b', 'z'], ['c', 'x'], ['c', 'y'], ['c', 'z'], ['c', 'z', 'zz'], ['c', 'z', 'zzz'], ['c', 'z', 'zzz', 'abc']];
var expectedPaths2 = [['a'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f', 'g']];
var expectedPaths3 = [['a'], ['b'], ['c'], ['d'], ['e']];
_.each({
'when an object has many properties and some properties have nested properties': {
testObject: testObj1,
expectedResult: expectedPaths1
},
'when an object has a very deeply nested property': {
testObject: testObj2,
expectedResult: expectedPaths2
},
'when an object has only first level properties and some of them point to undefined values': {
testObject: testObj3,
expectedResult: expectedPaths3
}
}, function (config, desc) {
describe(desc, function () {
it('should return an array containing all exact paths to all nested properties', function () {
assert.deepEqual(_.sortBy(_.allPaths(config.testObject)), _.sortBy(config.expectedResult));
});
});
});
});
describe('getAll', function () {
var testObj1 = {a: {x: 1, y: 2, z: 3}, b: {x: 1, y: 2, z: 3}, c: {x: 1, y: 2, z: {zz: 3, zzz: {abc: 10}}}};
var testObj2 = {a: {b: {c: {d: {e: {f: {g: 1}}}}}}};
var testObj3 = {a: 1, b: 2, c: 3, d: undefined, e: undefined};
_.each({
'when an object has many properties and some properties have nested properties': {
testParams: [testObj1, ['c', 'b.x', 'a.c', 'a.z'], null],
expectedResult: [testObj1.c, testObj1.b.x, null, testObj1.a.z]
},
'when an object has a very deeply nested property': {
testParams: [testObj2, ['a', 'a.b.c', 'a.b.c.d.e.f.g', 'a.b.c.d.e.f.g.h'], 2],
expectedResult: [testObj2.a, testObj2.a.b.c, testObj2.a.b.c.d.e.f.g, 2]
},
'when an object has only first level properties and some of them point to undefined values': {
testParams: [testObj3, ['a', 'b', 'd', 'f', 'a.z']],
expectedResult: [testObj3.a, testObj3.b, testObj3.d, undefined, undefined]
}
}, function (config, desc) {
describe(desc, function () {
it('should return all objects at the requested path or default if non-existant', function () {
assert.deepEqual(_.spread(_.getAll)(config.testParams), config.expectedResult);
});
});
});
});
describe('getFirst', function () {
var testObj1 = {a: {x: 1, y: 2, z: 3}, b: {x: 1, y: 2, z: 3}, c: {x: 1, y: 2, z: {zz: 3, zzz: {abc: 10}}}};
var testObj2 = {a: {b: {c: {d: {e: {f: {g: 1}}}}}}};
var testObj3 = {a: 1, b: 2, c: 3, d: undefined, e: undefined};
_.each({
'when an object has many properties and some properties have nested properties': {
testParams: [testObj1, ['d', 'b.q', 'a.z', 'a.x'], null],
expectedResult: testObj1.a.z
},
'when an object has a very deeply nested property': {
testParams: [testObj2, ['a.c.b', 'a.d.e', 'a.b.c.d.e.f', 'a.b.c'], 2],
expectedResult: testObj2.a.b.c.d.e.f
},
'when an object has only first level properties and some of them point to undefined values': {
testParams: [testObj3, ['z', 'x'], 5],
expectedResult: 5
}
}, function (config, desc) {
describe(desc, function () {
it('should return the value of the first defined property in the array', function () {
assert.deepEqual(_.spread(_.getFirst)(config.testParams), config.expectedResult);
});
});
});
});
describe('setDefinite', function () {
// TODO: rewrite
var something = {};
var testObj = {a: 1, b: {c: 3}};
_.setDefinite(testObj, 'd', 4);
_.setDefinite(testObj, 'b.c', 2);
_.setDefinite(testObj, 'x.y.z', 10);
_.setDefinite(testObj, 'd', undefined);
_.setDefinite(testObj, 'g.h', something.orOther);
_.setDefinite(testObj, 'b.e', _.get(something, 'a'));
var expectedResult = _.merge({}, testObj, {b: {c: 2}, d: 4, x: {y: {z: 10}}});
it('should call set with defined values and ignore cases with undefined values', function () {
assert.deepEqual(testObj, expectedResult);
});
});
describe('eachUntil', function () {
// TODO: rewrite
var testArray = [2, 4, 6, 8, 10, 12, 14, 16, 18];
var testObj = {a: 'at', b: 'bat', c: 'cast', d: 'dusty', e: 'everywhere'};
var testCollection1 = [{a: 1, c: 1}, {a: 2, x: false}, {a: 3, b: 3}, {a: 4}, {b: 5, z: true}];
var testCollection2 = [{xyz: 5}, {xyz: 6}, {xyz: 7}, null, {xyz: 8}];
it('should iterate over collection until the predicate returns true', function () {
// TODO: _.push
var pushFunction = function (val) {
results.push(val);
};
// TODO: functional refactor of below
// TODO: _.each({scenarios}) approach to tests
var results = [];
_.eachUntil(testArray, pushFunction, _.partial(_.lt, 10));
assert.deepEqual(results, _.filter(testArray, _.partial(_.gt, 12)));
results = [];
_.eachUntil(testObj, pushFunction, function (val) {return val.length > 4;});
assert.deepEqual(results, _.filter(testObj, function (val) {return val.length < 5;}));
results = [];
_.eachUntil(testCollection1, pushFunction, function (val) {return _.keys(val).length < 2;});
assert.deepEqual(results, _.slice(_.filter(testCollection1, function (val) {return _.keys(val).length > 1;}), 0, 3));
results = [];
_.eachUntil(testCollection2, pushFunction, _.isFalsy);
assert.deepEqual(results, _.slice(_.filter(testCollection2), 0, 3));
});
});
describe('pathsEqual', function () {
// TODO: rewrite
var obj1 = {a: 1, b: 2, c: 3, d: {e: 5, f: 6}, g: {h: {i: {j: 100}, z: {x: true}}}, k: {l: {m: {n: 200}}}, abc: {a: {x: 11, y: null, z: 33}, b: {x: 44, y: undefined, z: 66}, c: {x: 77, y: false, z: 99}}, zzz: undefined, qwe: 22};
var obj2 = {a: 1, b: 22, d: {e: 5, f: 7}, g: {h: {z: {x: true, y: false}}}, k: {}, abc: {a: {x: '11', y: null, z: 333, qqq: {q: true}}, b: {x: '44', y: undefined, z: 666}, c: {x: '77', y: false, z: 999}}};
// should return false when the values are equal but the paths are differnt
it('should return true when the values at the paths are equal', function () {
assert.deepEqual(_.pathsEqual([obj1, 'a'], [obj2, 'a']), true);
});
it('should work for deeply nested paths', function () {
assert.deepEqual(_.pathsEqual([obj1, 'g.h.z.x'], [obj2, 'g.h.z.x']), true);
});
it('should return false when the value at one path is defined and the other is not', function () {
assert.deepEqual(_.pathsEqual([obj1, 'zzz'], [obj2, 'zzz']), false);
});
it('should return false when the values at the paths are both defined but not equal', function () {
assert.deepEqual(_.pathsEqual([obj1, 'b'], [obj2, 'b']), false);
});
it('should return true when the values at the paths are equal, but the paths are different', function () {
assert.deepEqual(_.pathsEqual([obj1, 'qwe'], [obj2, 'b']), true);
});
it('should work for deeply nested paths when the paths are different', function () {
assert.deepEqual(_.pathsEqual([obj1, 'g.h.z.x'], [obj2, 'abc.a.qqq.q']), true);
});
it('should return false when the value at one path is defined and the other is not and the paths are different', function () {
assert.deepEqual(_.pathsEqual([obj1, 'zzz'], [obj2, 'zzzzz']), false);
});
it('should return false when the values at the paths are both defined but not equal and the paths are different', function () {
assert.deepEqual(_.pathsEqual([obj1, 'd.g'], [obj2, 'g.y']), false);
});
describe('and when the same path shorthand is used', function () {
it('should return true when the values at the path are equal', function () {
assert.deepEqual(_.pathsEqual(obj1, obj2, 'a'), true);
});
it('should work for a deeply nested path', function () {
assert.deepEqual(_.pathsEqual(obj1, obj2, 'g.h.z.x'), true);
});
it('should return false when one of the values at the path is defined and the other is not', function () {
assert.deepEqual(_.pathsEqual(obj1, obj2, 'zzz'), false);
});
it('should return false when the values at the path are both defined but not equal', function () {
assert.deepEqual(_.pathsEqual(obj1, obj2, 'b'), false);
});
});
});
describe('innerJoin', function () {
// TODO: check proper handling of 'abc.b'
// TODO: rewrite
var obj1 = {a: 1, b: true, c: null, d: 'test', e: {}};
var obj2 = {a: '1', b: false, c: null, d: 'test', e: {}};
var obj3 = {a: 1, b: {c: '2'}, d: {e: {f: true, g: false}}};
var obj4 = {a: '1', b: {c: 2}, d: {e: {f: false, g: true}}};
var obj5 = {a: 1, b: 2, c: 3, d: {e: 5, f: 6}, g: {h: {i: {j: 100}, z: {x: true}}}, k: {l: {m: {n: 200}}}, zzz: undefined};
var obj6 = {a: 1, b: 22, d: {e: 5, f: 7}, g: {h: {z: {x: true, y: false}}}, k: {}};
var obj7 = {xyz: 10};
var obj8 = {xyz: 10};
var obj9 = {a: {b: {c: {d: {e: {f: {g: {h: undefined}, s: null}, r: false}, q: {ee: 1}}, p: {}}, o: 'o'}, n: true}, m: 10};
var obj10 = {a: {b: {c: {d: {e: {f: {g: {h: undefined}, s: false}, r: 0}}, p: {a: 1}}, o: 'O'}, n: 1}};
var obj11 = {};
var expected1and2Union = {c: null, d: 'test', e: {}};
var expected3and4Union = {};
var expected5and6Union = {a: 1, d: {e: 5}, g: {h: {z: {x: true}}}};
var expected9and10Union = {a: {b: {c: {d: {e: {f: {g: {h: undefined}}}}}}}};
var expectedUnionWith11 = {};
it('should return a copy of one of the objects when both are identical', function () {
assert.deepEqual(_.innerJoin(obj7, obj8), obj7);
});
it('should should work for deeply nested paths that resolve to undefined', function () {
assert.deepEqual(_.innerJoin(obj7, obj8), obj7);
});
it('should return an object of all identical path-property pairs between the two objects', function () {
assert.deepEqual(_.innerJoin(obj1, obj2), expected1and2Union);
});
it('should return an empty object if there are no identical path-property pairs between the two objects', function () {
assert.deepEqual(_.innerJoin(obj3, obj4), expected3and4Union);
});
it('should return an object of all identical path-property pairs between two complex objects with deep paths', function () {
assert.deepEqual(_.innerJoin(obj5, obj6), expected5and6Union);
});
it('should return an empty object when any object is joined with an empty object', function () {
assert.deepEqual(_.innerJoin(obj2, obj11), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj4, obj11), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj6, obj11), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj8, obj11), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj10, obj11), expectedUnionWith11);
});
it('should return an empty object when an empty object is joined with any object', function () {
assert.deepEqual(_.innerJoin(obj11, obj2), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj11, obj4), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj11, obj6), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj11, obj8), expectedUnionWith11);
assert.deepEqual(_.innerJoin(obj11, obj10), expectedUnionWith11);
});
});
describe('filtration', function () {
// TODO: rewrite
var testCollection1 = [
{a: 1, b: '_.noop', z: [true, false], second: 10, a2: 't', a3: 't'},
{a: 2, b: _.noop, z: [true, false], second: '10', a2: 't', a3: 't'},
{a: 3, b: '_.noop', z: [true, false], second: 10, a2: 't', a3: 't'},
{a: 4, b: _.noop, second: 10, a3: 't', a4: 't', a5: 't'},
{a: 5, b: '_.noop', z: [true, false], second: 10, a2: 't', a3: 't'},
{a: 6, b: _.noop, z: [true, false], second: 10, a2: 't', a3: 't'},
{a: 7, b: _.noop, second: 10, a3: 't', a4: 't', a5: 't'},
{a: 8, b: _.noop, z: [true, false], a2: 't', a3: 't', a4: 't'},
{a: 9, b: _.noop, z: [true, false], second: 10, a2: 't'},
{a: 10, b: _.noop, z: [true, false], second: 10, a2: 't', a3: 't'}
];
var testFilterArrays = [
function (obj) {
return _.some(_.values(obj), _.isFunction);
},
function (obj) {
return _.keys(obj).length > 5;
},
function (obj) {
return _.isEqual(obj.z, [true, false]);
},
function (obj) {
return _.filter(_.values(obj), _.isNumber).length >= 2;
}
];
var expectedResult = _.filter(testCollection1, function (obj) {
return obj.a === 6 || obj.a === 10;
});
it('should use all functions in the array as filters', function () {
assert.deepEqual(_.filtration(testCollection1, testFilterArrays), expectedResult)
});
});
describe('applyToNested', function () {
// TODO: rewrite
var testCollection1 = [
{a: 10, x: 'a', y: true},
{a: 20, x: 'b', y: true},
{a: 30, x: 'a', y: true},
{a: 40, x: 'a', y: false}
];
var testObj1 = {a: 10, b: {c: 20, d: {x: true, y: false}, e: 'q'}};
var applyToNestedFilter = _.applyToNested(_.filter, 'b', 1);
var applyToNestedIsFalsy = _.applyToNested(_.isFalsy, 'y');
var applyToNestedSet = _.applyToNested(_.set, 'b.d.y', 2);
var applyToNestedPick = _.applyToNested(_.pick, 'b', 0);
var expectedFilteredCollection1 = _.filter(testCollection1, function (obj) {
return (obj.a/10) % 2 === 1;
});
var expectedFilteredCollection2 = [testCollection1[3]];
var expectedSetObject = {a: '17', z: false};
var expectedPickedObject = {c: 20, e: 'q'};
it('should use the object at the specified path', function () {
var result = applyToNestedFilter(testCollection1, {a: 20, b: {x: 'a', y: true}});
assert.deepEqual(result, expectedFilteredCollection1);
result = _.filter(testCollection1, applyToNestedIsFalsy);
assert.deepEqual(result, expectedFilteredCollection2);
result = applyToNestedSet({a: '17'}, 'z', testObj1);
assert.deepEqual(result, expectedSetObject);
result = applyToNestedPick(testObj1, ['c', 'e']);
});
});
describe('setBySelf', function () {
var testObj1 = {a: 1, b: 2, c: 3};
var testObj2 = {a: 1, b: {c: {d: 2}}};
var testObj3 = {a: {b: {c: 1}}, d: {e: {f: 2}}};
var testObj4 = {a: {b: {c: 1, d: 2}}};
_.each({
'when setting an existing first level property to an existing first level property': {
object: testObj1,
atPath: 'a',
toPath: 'c',
expectedResult: {a: 3, b: 2, c: 3}
},
'when setting a non-existing first level property to an existing first level property': {
object: testObj1,
atPath: 'd',
toPath: 'c',
expectedResult: {a: 1, b: 2, c: 3, d: 3}
},
'when setting an existing first level property to a non-existing first level property': {
object: testObj1,
atPath: 'a',
toPath: 'd',
expectedResult: {a: undefined, b: 2, c: 3}