-
Notifications
You must be signed in to change notification settings - Fork 45
/
Collection-spec.js
1140 lines (980 loc) · 45.1 KB
/
Collection-spec.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
/* globals FieldDB*/
"use strict";
var Collection;
var FieldDBObject;
try {
if (FieldDB) {
Collection = FieldDB.Collection;
FieldDBObject = FieldDB.FieldDBObject;
}
} catch (e) {}
Collection = Collection || require("../api/Collection").Collection;
FieldDBObject = FieldDBObject || require("../api/FieldDBObject").FieldDBObject;
var DEFAULT_DATUM_VALIDATION_STATI = require("./../api/datum/validation-status.json");
var specIsRunningTooLong = 5000;
/*
======== A Handy Little Jasmine Reference ========
https://github.com/pivotal/jasmine/wiki/Matchers
Spec matchers:
expect(x).toEqual(y); compares objects or primitives x and y and passes if they are equivalent
expect(x).toBe(y); compares objects or primitives x and y and passes if they are the same object
expect(x).toMatch(pattern); compares x to string or regular expression pattern and passes if they match
expect(x).toBeDefined(); passes if x is not undefined
expect(x).toBeUndefined(); passes if x is undefined
expect(x).toBeNull(); passes if x is null
expect(x).toBeTruthy(); passes if x evaluates to true
expect(x).toBeFalsy(); passes if x evaluates to false
expect(x).toContain(y); passes if array or string x contains y
expect(x).toBeLessThan(y); passes if x is less than y
expect(x).toBeGreaterThan(y); passes if x is greater than y
expect(function(){fn();}).toThrow(e); passes if function fn throws exception e when executed
Every matcher's criteria can be inverted by prepending .not:
expect(x).not.toEqual(y); compares objects or primitives x and y and passes if they are not equivalent
Custom matchers help to document the intent of your specs, and can help to remove code duplication in your specs.
beforeEach(function() {
this.addMatchers({
toBeLessThan: function(expected) {
var actual = this.actual;
var notText = this.isNot ? " not" : "";
this.message = function () {
return "Expected " + actual + notText + " to be less than " + expected;
}
return actual < expected;
}
});
});
*/
var useDefaults = function() {
return JSON.parse(JSON.stringify(DEFAULT_DATUM_VALIDATION_STATI));
};
var removeFieldDBFields = function(item) {
delete item.fieldDBtype;
delete item.version;
delete item.dateCreated;
return item;
};
describe("lib/Collection", function() {
it("should load", function() {
expect(Collection).toBeDefined();
});
describe("construction options", function() {
var collection;
beforeEach(function() {
collection = new Collection({
inverted: true,
primaryKey: "validationStatus",
capitalizeFirstCharacterOfPrimaryKeys: true
});
collection.add(useDefaults()[0]);
collection.add(useDefaults()[1]);
});
it("should accept a primary key", function() {
expect(collection.primaryKey).toEqual("validationStatus");
});
it("should use the primary key to get members using dot notation", function() {
expect(collection.Published.validationStatus).toEqual(useDefaults()[1].validationStatus);
expect(collection.published.validationStatus).toEqual(useDefaults()[1].validationStatus);
});
it("should accept inverted", function() {
expect(collection.collection[0].validationStatus).toEqual(useDefaults()[1].validationStatus);
});
it("should permit push to add to the bottom", function() {
collection.push(useDefaults()[2]);
expect(collection.collection[2].validationStatus).toEqual(useDefaults()[2].validationStatus);
});
it("should permit unshift to add to the top", function() {
collection.unshift(useDefaults()[2]);
expect(collection.collection[0].validationStatus).toEqual(useDefaults()[2].validationStatus);
});
it("should permit pop to remove from the bottom", function() {
expect(collection.collection.length).toEqual(2);
expect(collection.collection[1].validationStatus).toEqual("Checked*");
var removed = collection.pop();
expect(collection.collection.length).toEqual(1);
expect(removed.validationStatus).toEqual("Checked*");
});
it("should permit shift to remove from the top", function() {
expect(collection.collection.length).toEqual(2);
expect(collection.collection[0].validationStatus).toEqual("Published*");
var removed = collection.shift();
expect(collection.collection.length).toEqual(1);
expect(removed.validationStatus).toEqual("Published*");
});
it("should return reference to the added item", function() {
expect(collection.length).toEqual(2);
var addedItem = collection.add({
validationStatus: "ToBeChecked"
});
expect(collection.length).toEqual(3);
expect(collection._collection.length).toEqual(3);
expect(addedItem).toBe(collection.tobechecked);
});
it("should permit add of an array", function() {
expect(collection.length).toEqual(2);
var addedItems = collection.add([{
validationStatus: "CheckedWithSam"
}, {
validationStatus: "ToBeCheckedWithSam"
}]);
expect(collection.length).toEqual(4);
expect(collection._collection.length).toEqual(4);
expect(addedItems[0]).toBe(collection.checkedwithsam);
expect(addedItems[1]).toBe(collection.tobecheckedwithsam);
});
it("should permit concat of an array", function() {
expect(collection.length).toEqual(2);
expect(collection.concat([])).toEqual(collection);
expect(collection.concat()).toEqual(collection);
collection.concat([{
validationStatus: "CheckedWithSam"
}, {
validationStatus: "ToBeCheckedWithSam"
}]).concat([{
validationStatus: "CheckedWithJo"
}]);
expect(collection.length).toEqual(5);
});
it("should permit concat of a collection", function() {
expect(collection.length).toEqual(2);
collection.concat(new Collection({
collection: [{
validationStatus: "CheckedWithSam"
}, {
validationStatus: "ToBeCheckedWithSam"
}, {
validationStatus: "ToBeCheckedWithPhylis"
}],
primaryKey: "validationStatus"
}));
expect(collection.length).toEqual(5);
});
it("should permit constrution with just an array", function() {
var newcollection = new Collection([{
id: "a",
type: "tags"
}, {
id: "z",
value: "somethign\n with a line break",
type: "wiki"
}, {
id: "c",
type: "date"
}]);
expect(newcollection.length).toEqual(3);
});
it("should sanitize primary keys", function() {
var newcollection = new Collection();
newcollection.add({
id: " A",
tipa: "llamda"
});
expect(newcollection.primaryKey).toEqual("id");
expect(newcollection.length).toEqual(1);
expect(newcollection.warnMessage).toBeUndefined();
expect(removeFieldDBFields(newcollection.A.toJSON())).toEqual({
id: "A",
tipa: "llamda"
});
});
it("should be able to change the primaryKey", function() {
var newcollection = new Collection();
newcollection.primaryKey = "tempId";
newcollection.add({
tempId: " A",
tipa: "llamda"
});
expect(newcollection.primaryKey).toEqual("tempId");
expect(newcollection.length).toEqual(1);
expect(newcollection.warnMessage).toBeUndefined();
});
});
describe("acessing contents", function() {
var collection;
beforeEach(function() {
collection = new Collection({
primaryKey: "validationStatus",
collection: useDefaults(),
capitalizeFirstCharacterOfPrimaryKeys: true
});
// collection.debugMode = true;
});
it("should seem like an object by providing dot notation for primaryKeys ", function() {
expect(collection.Checked.validationStatus).toEqual(useDefaults()[0].validationStatus);
});
it("should seem like an object by providing case insensitive cleaned dot notation for primaryKeys ", function() {
expect(collection.checked.validationStatus).toEqual(useDefaults()[0].validationStatus);
});
it("should return undefined for items which are not in the collection", function() {
expect(collection.Removed).toBeUndefined();
});
it("should be able to find items by primary key", function() {
expect(collection.find("Checked*").length).toEqual(1);
expect(removeFieldDBFields(collection.find("Checked*")[0].toJSON())).toEqual(useDefaults()[0]);
});
it("should be return an empty array if nothing was searched for", function() {
expect(collection.find("")).toEqual([]);
});
it("should be return an empty array if nothing was searched for", function() {
expect(collection.find({})).toEqual([]);
});
it("should be able to find items by primary key using a regex", function() {
collection.add({
validationStatus: "CheckedBySeberina",
color: "green"
});
expect(collection.length).toEqual(8);
// expect(collection).toEqual();
var checkedStati = collection.find(/^Checked/).map(function(item) {
return {
validationStatus: item.validationStatus,
color: item.color,
default: item.default
};
});
expect(checkedStati.length).toEqual(2);
expect(checkedStati[0].validationStatus).toEqual("Checked*");
expect(checkedStati[0].color).toEqual("green");
expect(checkedStati[1].validationStatus).toEqual("CheckedBySeberina");
expect(checkedStati[1].color).toEqual("green");
});
it("should be able to find items by any attribute", function() {
expect(collection.find("color", "green").map(function(item) {
return {
validationStatus: item.validationStatus,
color: item.color
};
})).toEqual([{
validationStatus: "Checked*",
color: "green"
}, {
validationStatus: "ApprovedLanguageLearningContent*",
color: "green"
}]);
});
it("should accpet a RegExp to find items", function() {
expect(collection.find("color", /(red|black)/i).map(function(item) {
return {
validationStatus: item.validationStatus,
color: item.color
};
})).toEqual([{
validationStatus: "Deleted*",
color: "red"
}, {
validationStatus: "Duplicate*",
color: "red"
}]);
});
/*TODO could change this to "rVn" once we have phonological fuzzy find */
it("should be able to fuzzy find items by any attribute", function() {
expect(collection.fuzzyFind("color", "r.*n").map(function(item) {
return item.color;
})).toEqual(["green", "orange", "green", "orange"]);
});
it("should be able to fuzzy find best match items by any attribute", function() {
collection.push({
validationStatus: "CheckedByRanran",
color: "ran"
});
var results = collection.fuzzyFind("color", "ran");
expect(results[0].color).toEqual("ran");
expect(results.map(function(item) {
return item.color;
})).toEqual(["ran", "orange", "orange"]);
});
it("should provide map on its internal collection", function() {
expect(collection.map).toBeDefined();
expect(collection.map(function(item) {
return item.validationStatus;
})).toEqual(["Checked*", "Published*", "ToBeChecked*", "ApprovedLanguageLearningContent*", "ContributedLanguageLearningContent*", "Deleted*", "Duplicate*"]);
});
});
describe("cloning and minimal pairs", function() {
it("should be able to clone an existing collection", function() {
var collection = new Collection({
primaryKey: "validationStatus",
collection: useDefaults(),
capitalizeFirstCharacterOfPrimaryKeys: true
});
var newcollection = collection.clone();
expect(newcollection.length).toEqual(collection.length);
newcollection.map(function(item){
expect(item.toJSON()).toEqual(collection.find([item.validationStatus])[0].toJSON());
});
newcollection.checked = {
validationStatus: "Checked",
color: "blue"
};
expect(removeFieldDBFields(collection.checked.toJSON())).toEqual(useDefaults()[0]);
expect(newcollection.checked).not.toEqual(useDefaults()[0]);
expect(collection.checked).not.toEqual(newcollection.checked);
});
it("should be able to clone a collection run convertToDocType and still be a collection", function() {
var corpora = new Collection([{
id: "first item"
}, {
id: "second item"
}, {
id: "third item"
}, {
id: "fourth item"
}, {
id: "fifth item"
}]);
expect(corpora.length).toEqual(5);
var shouldbethesamecollection = FieldDBObject.convertDocIntoItsType(corpora);
expect(shouldbethesamecollection.length).toEqual(5);
expect(corpora.length).toEqual(5);
expect(corpora.constructor).toBe(Collection);
expect(shouldbethesamecollection).toBe(corpora);
expect(shouldbethesamecollection._collection[2]).toBe(corpora._collection[2]);
});
it("should not effect clone if original object is changed", function() {
var adatum = new FieldDBObject({
"tags": "apositive"
});
adatum.fields = new Collection([{
id: "judgement",
value: "#"
}, {
id: "utterance",
value: "noqata tusunayawanmi"
}]);
var aminimalPair = adatum.clone();
console.log(" aminimalPair.fields1 ", aminimalPair.fields[1]);
aminimalPair.fields[1].value = "noqata tusunayami";
aminimalPair.fields[0].value = "*";
expect(adatum.fields.judgement.value).toEqual("#");
expect(aminimalPair.fields[0].value).toEqual("*");
adatum.fields.utterance.value = "noqata tusunayawaanmi";
expect(adatum.fields.utterance.value).toEqual("noqata tusunayawaanmi");
expect(aminimalPair.fields[1].value).toEqual("noqata tusunayami");
});
});
describe("customized primary key", function() {
it("should provide map on its internal collection", function() {
// var HasInternalCollection = function HasInternalCollection() {
// FieldDBObject.apply(this, arguments);
// };
// HasInternalCollection.prototype = Object.create(FieldDBObject.prototype, {
// constructor: {
// value: HasInternalCollection
// },
// datalist: {
// get: function() {
// if (!this._datalist) {
// this.debug("creating a default data list");
// this._datalist = new FieldDBObject({
// title: {
// default: "Imported Data"
// },
// docs: {
// collection: [],
// primaryKey: "tempId"
// },
// // confidential: self.corpus.confidential,
// });
// }
// return this._datalist;
// },
// set: function(value) {
// if (value === this._datalist) {
// return;
// }
// this._datalist = value;
// }
// }
// });
var customizedWithTempId = new Collection({
// debugMode: true,
collection: [],
primaryKey: "tempId"
});
expect(customizedWithTempId).toBeDefined();
expect(customizedWithTempId.primaryKey).toEqual("tempId");
expect(customizedWithTempId._primaryKey).toEqual("tempId");
expect(customizedWithTempId._collection).toBeDefined();
customizedWithTempId.add({
tempId: "123",
some: "contents"
});
expect(customizedWithTempId["123"].toJSON()).toEqual({
fieldDBtype: "FieldDBObject",
tempId: "123",
some: "contents",
dateCreated: customizedWithTempId["123"].dateCreated,
version: customizedWithTempId["123"].version
});
});
});
describe("cleaning contents", function() {
var collection,
item,
item2,
item3;
beforeEach(function() {
collection = new Collection({
// debugMode: true,
primaryKey: "name",
collection: [new FieldDBObject({
name: "chicken",
difference: "lowercase"
}), new FieldDBObject({
name: "chicken",
difference: "lowercase"
}), new FieldDBObject({
name: "_chicken_",
difference: "underscores"
}), new FieldDBObject({
name: "CHICKEN",
difference: "uppercase and after chicken"
}), new FieldDBObject({
name: "duck"
}), new FieldDBObject({
name: "pigeon"
}), new FieldDBObject({
name: "turkey"
})],
capitalizeFirstCharacterOfPrimaryKeys: true
});
// collection.debugMode = true;
item = collection.CHICKEN;
item2 = collection.chicken;
item3 = collection.pigeon;
});
it("should not set/complain about setting an object to itself.", function() {
var duck = collection._collection[3];
expect(duck).toBeDefined();
expect(collection.duck).toEqual(duck);
collection.set("duck", duck);
expect(collection.duck).toEqual(duck);
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("Overwriting an existing collection member duck (they have the same key but are not equal nor the same object)");
}
});
it("should not set/complain about setting an equivalent object to itself.", function() {
var duck = collection._collection[3];
expect(duck).toBeDefined();
expect(collection.duck).toEqual(duck);
collection.set("duck", new FieldDBObject({
name: "duck",
}));
expect(collection.duck).toEqual(duck);
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("Overwriting an existing collection member duck (they have the same key but are not equal nor the same object)");
}
});
it("should ask the user if they want to merge non equivalent object to itself.", function() {
var duck = collection._collection[3];
expect(duck).toBeDefined();
expect(collection.duck).toEqual(duck);
collection.set("duck", new FieldDBObject({
name: "duck",
feet: "yellow"
}));
expect(collection.duck).toEqual(duck);
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("Overwriting an existing collection member duck (they have the same key but are not equal nor the same object)");
}
});
it("should work for collections with primary key clashes", function() {
expect(collection).toBeDefined();
if (collection.warnMessage) {
// expect(collection.warnMessage).toContain("The sanitized the dot notation key of this object is not the same as its primaryKey: chicken -> Chicken");
expect(collection.warnMessage).not.toContain("Not setting Chicken, it already the same in the collection");
// expect(collection.warnMessage).toContain("The sanitized the dot notation key of this object is not the same as its primaryKey: _chicken_ -> Chicken");
// expect(collection.warnMessage).toContain("The sanitized the dot notation key of this object is not the same as its primaryKey: duck -> Duck");
// expect(collection.warnMessage).toContain("The sanitized the dot notation key of this object is not the same as its primaryKey: pigeon -> Pigeon");
// expect(collection.warnMessage).toContain("The sanitized the dot notation key of this object is not the same as its primaryKey: turkey -> Turkey");
}
expect(collection.find("difference", "lowercase")[0].difference).toEqual("lowercase");
expect(collection.find("difference", "underscores")[0].difference).toEqual("underscores");
expect(collection.length).toBe(6);
expect(item).toBeDefined();
expect(item.difference).toBe("uppercase and after chicken");
expect(item2).toBeDefined();
expect(item2.difference).toBe("uppercase and after chicken");
expect(collection.collection[0].difference).toEqual("lowercase");
expect(collection.collection[1].difference).toEqual("underscores");
expect(collection.collection[2].difference).toEqual("uppercase and after chicken");
expect(item3).toBeDefined();
});
it("should give the index of a item", function() {
expect(collection.indexOf).toBeDefined();
expect(item).toBeDefined();
expect(collection.indexOf(item)).toBe(2);
expect(collection.collection[collection.indexOf(item)]).toBe(item);
expect(collection.indexOf(item2)).toBe(2);
expect(collection.collection[collection.indexOf(item2)]).toBe(item2);
expect(collection.indexOf(item3)).toBe(4);
expect(collection.collection[collection.indexOf(item3)]).toBe(item3);
});
it("should give the index of a simple object", function() {
expect(collection.indexOf(item.toJSON())).toBe(2);
expect(collection.indexOf(item.toJSON())).toBe(2);
expect(collection.collection[collection.indexOf(item.toJSON())]).toBe(item);
expect(collection.indexOf(item2.toJSON())).toBe(2);
expect(collection.collection[collection.indexOf(item2.toJSON())]).toBe(item2);
expect(collection.indexOf(item3.toJSON())).toBe(4);
expect(collection.collection[collection.indexOf(item3.toJSON())]).toBe(item3);
});
it("should give the index using only the primary key", function() {
expect(collection.indexOf("chicken")).toBe(2);
expect(collection.indexOf("chicken")).toBe(2);
expect(collection.collection[collection.indexOf("chicken")]).toBe(item);
expect(collection.indexOf("duck")).toBe(3);
expect(collection.indexOf("pigeon")).toBe(4);
expect(collection.collection[collection.indexOf("pigeon")]).toBe(item3);
});
it("should be possible to reorder items by index", function() {
expect(collection.reorder).toBeDefined();
expect(collection.collection[2].name).toBe("CHICKEN");
expect(collection.collection[3].name).toBe("duck");
expect(collection.collection[4].name).toBe("pigeon");
collection.reorder(4, 2);
expect(collection.collection[0].name).toBe("chicken");
expect(collection.collection[1].name).toBe("_chicken_");
expect(collection.collection[2].name).toBe("pigeon");
expect(collection.collection[3].name).toBe("CHICKEN");
expect(collection.collection[4].name).toBe("duck");
expect(collection.collection[5].name).toBe("turkey");
});
it("should be possible to reorder items using a bare object", function() {
expect(collection.collection[0].name).toBe("chicken");
expect(collection.collection[1].name).toBe("_chicken_");
expect(collection.collection[2].name).toBe("CHICKEN");
expect(collection.collection[3].name).toBe("duck");
expect(collection.collection[4].name).toBe("pigeon");
expect(collection.collection[5].name).toBe("turkey");
collection.reorder(collection.collection[5].toJSON(), 0);
expect(collection.collection[0].name).toBe("turkey");
expect(collection.collection[1].name).toBe("chicken");
expect(collection.collection[2].name).toBe("_chicken_");
expect(collection.collection[3].name).toBe("CHICKEN");
expect(collection.collection[4].name).toBe("duck");
expect(collection.collection[5].name).toBe("pigeon");
});
it("should be possible to reorder items using an object", function() {
expect(collection.collection[0].name).toBe("chicken");
expect(collection.collection[1].name).toBe("_chicken_");
expect(collection.collection[2].name).toBe("CHICKEN");
expect(collection.collection[3].name).toBe("duck");
expect(collection.collection[4].name).toBe("pigeon");
expect(collection.collection[5].name).toBe("turkey");
collection.reorder(collection.collection[5], 0);
expect(collection.collection[0].name).toBe("turkey");
expect(collection.collection[1].name).toBe("chicken");
expect(collection.collection[2].name).toBe("_chicken_");
expect(collection.collection[3].name).toBe("CHICKEN");
expect(collection.collection[4].name).toBe("duck");
expect(collection.collection[5].name).toBe("pigeon");
});
it("should be possible to remove an item", function() {
// collection.debugMode = true;
var chicken = collection.collection[1];
expect(chicken.name).toEqual("_chicken_");
expect(collection.length).toEqual(6);
var removedOne = collection.remove(chicken);
expect(collection.length).toEqual(5);
expect(removedOne).toEqual([chicken]);
expect(collection.removedCollection).toEqual([chicken]);
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
}
var uppercasechicken = collection.collection[1];
expect(uppercasechicken.name).toEqual("CHICKEN");
expect(collection.length).toEqual(5);
removedOne = collection.remove(uppercasechicken);
expect(collection.length).toEqual(4);
expect(removedOne).toEqual([uppercasechicken]);
expect(collection.removedCollection).toEqual([chicken, uppercasechicken]);
expect(collection.length).toEqual(4);
removedOne = collection.remove(chicken);
expect(collection.length).toEqual(4);
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
// expect(chicken).toEqual( " ");
expect(collection.warnMessage).toContain("Didn't need to remove object(s) which were not in the collection.");
expect(removedOne).toEqual([]);
expect(collection.removedCollection).toEqual([chicken, uppercasechicken]);
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
collection.warnMessage = "";
expect(collection.warnMessage).not.toContain("Didn't need to remove object(s) which were not in the collection.");
expect(collection.remove({})).toEqual([]);
expect(collection.warnMessage).toContain("Didn't need to remove object(s) which were not in the collection.");
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
expect(collection.length).toEqual(4);
removedOne = collection.remove(collection.duck);
expect(collection.length).toEqual(3);
expect(removedOne.length).toEqual(1);
expect(removedOne[0].name).toEqual("duck");
});
it("should be possible to remove a simple object", function() {
// collection.debugMode = true;
var duck = collection.collection[3].toJSON();
expect(duck.fieldDBtype).toEqual("FieldDBObject");
expect(duck.name).toEqual("duck");
expect(collection.length).toEqual(6);
var removedOne = collection.remove(duck);
expect(collection.length).toEqual(5);
expect(removedOne.length).toEqual(1);
expect(removedOne[0].equals(duck)).toBeTruthy();
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
}
expect(removedOne[0].name).toEqual(duck.name);
expect(removedOne[0].difference).toEqual(duck.difference);
var pigeon = collection.collection[3].toJSON();
collection.warnMessage = "";
expect(pigeon.name).toEqual("pigeon");
expect(collection.length).toEqual(5);
removedOne = collection.remove(pigeon);
expect(collection.length).toEqual(4);
expect(removedOne[0].equals(pigeon)).toBeTruthy();
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
});
it("should be possible to remove objects matching a regular expression", function() {
var removedAFew = collection.remove({
name: /.*chicken.*/i
});
expect(removedAFew.length).toEqual(3);
expect(removedAFew[0].name).toEqual("CHICKEN");
expect(removedAFew[1].name).toEqual("_chicken_");
expect(removedAFew[2].name).toEqual("chicken");
expect(collection.length).toEqual(3);
expect(collection.collection[0].name).toEqual("duck");
expect(collection.collection[1].name).toEqual("pigeon");
expect(collection.collection[2].name).toEqual("turkey");
});
it("should be possible to remove a primary key", function() {
expect(collection.remove).toBeDefined();
expect(collection.collection[0].name).toBe("chicken");
expect(collection.collection[1].name).toBe("_chicken_");
expect(collection.collection[2].name).toBe("CHICKEN");
expect(collection.collection[3].name).toBe("duck");
expect(collection.collection[4].name).toBe("pigeon");
expect(collection.collection[5].name).toBe("turkey");
var removedOne = collection.remove("chicken");
expect(removedOne.length).toEqual(2);
expect(removedOne[0].name).toEqual("CHICKEN");
expect(removedOne[1].name).toEqual("chicken");
removedOne = collection.remove("duck");
expect(removedOne.length).toEqual(1);
expect(removedOne[0].name).toEqual("duck");
expect(collection.duck).toBeUndefined();
});
it("should be possible to remove multiple primary keys", function() {
var removedOne = collection.remove(["duck", "pigeon"]);
expect(removedOne.length).toEqual(2);
expect(removedOne[0].name).toEqual("pigeon");
expect(removedOne[1].name).toEqual("duck");
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
}
});
it("should be possible to remove multiple items", function() {
var removedOne = collection.remove([collection.duck, collection.pigeon]);
expect(removedOne.length).toEqual(2);
expect(removedOne[0].name).toEqual("pigeon");
expect(removedOne[1].name).toEqual("duck");
if (collection.warnMessage) {
expect(collection.warnMessage).not.toContain("One of the requested removal items dont match what was removed");
}
});
});
describe("non-lossy persistance", function() {
var collection,
collectionToLoad = [{
_id: "a",
type: "datum"
}, {
_id: "z",
type: "datum"
}, {
_id: "c",
type: "datum"
}];
beforeEach(function() {
collection = new Collection({
collection: collectionToLoad,
aHellperFunction: function() {
this.debug("called");
}
});
collection.anotherHelperFunction = function() {
this.debug("called");
};
});
it("should have a type of Collection", function() {
expect(collection.fieldDBtype).toEqual("Collection");
});
it("should seem like an array when serialized", function() {
expect(collection.toJSON().map(removeFieldDBFields)).toEqual(collectionToLoad);
expect(Object.prototype.toString.call(collection.toJSON())).toEqual("[object Array]");
});
it("should seem not loose information when serialized and reloaded", function() {
var collectionFromDB = JSON.stringify(collection);
collection = new Collection({
collection: JSON.parse(collectionFromDB)
});
expect(collection.toJSON().map(removeFieldDBFields)).toEqual(collectionToLoad);
// expect(collection.map(removeFieldDBFields)).toEqual(collectionToLoad);
});
it("should seem not loose information when toJSONed and reloaded", function() {
var collectionFromDB = collection.toJSON();
collection = new Collection({
collection: collectionFromDB
});
expect(collection.toJSON().map(removeFieldDBFields)).toEqual(collectionToLoad);
// expect(collection.map(removeFieldDBFields)).toEqual(collectionToLoad);
});
});
describe("confidentiality", function() {
var collection;
beforeEach(function() {
collection = new Collection({
primaryKey: "validationStatus",
collection: useDefaults(),
capitalizeFirstCharacterOfPrimaryKeys: true
});
// collection.debugMode = true;
});
it("should be able to set encrypted on members of a collection", function() {
collection.encrypted = true;
expect(collection.checked.encrypted).toEqual(true);
});
it("should be able to set confidential on members of a collection", function() {
collection.confidential = {
secretkey: "secretkey"
};
expect(collection.checked.confidential.secretkey).toEqual("secretkey");
});
});
describe("equals", function() {
it("should be able to calculate if two collections are equal", function() {
var collectionOfBirds = new Collection([{
id: "penguin",
wings: "black"
}, {
id: "duck",
wings: "brown"
}, {
id: "robin",
wings: "beige"
}]);
var sameCollectionButDifferent = new Collection(collectionOfBirds.toJSON());
expect(collectionOfBirds.equals(sameCollectionButDifferent)).toBeTruthy();
sameCollectionButDifferent.penguin.wings = "black white";
expect(collectionOfBirds._collection[0].wings).not.toEqual(sameCollectionButDifferent._collection[0].wings);
expect(collectionOfBirds.equals(sameCollectionButDifferent)).toBeFalsy();
});
it("should consider empty collections as equal", function() {
expect(new Collection().equals(new Collection())).toBeTruthy();
expect(new Collection([{
id: "hie"
}]).equals(new Collection())).toBeFalsy();
});
it("should consider not collections with different lengths as equal", function() {
var aCollectionWithOneItem = new Collection([{
id: "one"
}]);
var aCheatedCollectionWithTwoItems = new Collection(aCollectionWithOneItem.toJSON());
expect(aCollectionWithOneItem.equals(aCheatedCollectionWithTwoItems)).toBeTruthy();
// now a cheated collection will have 2 ites, but it is identical to the first collection, normally this is impossible because the collection is uniqued
// this is just to prove it will exit early.
aCheatedCollectionWithTwoItems._collection.push(aCollectionWithOneItem._collection[0]);
expect(aCollectionWithOneItem.equals(aCheatedCollectionWithTwoItems)).toBeFalsy();
});
});
describe("merging", function() {
var aBaseCollection;
var atriviallyDifferentCollection;
beforeEach(function() {
aBaseCollection = new Collection([new FieldDBObject({
id: "penguin",
missingInNew: "hi"
}), new FieldDBObject({
id: "cuckoo"
}), new FieldDBObject({
id: "robin",
externalObject: new FieldDBObject({
internalString: "internal",
internalTrue: true,
internalEmptyString: "",
internalFalse: false,
internalNumber: 2,
internalEqualString: "i'm a old property",
// debugMode: true
})
}), new FieldDBObject({
id: "cardinal"
}), {
id: "onlyinTarget"
}, {
id: "willBeOverwritten",
internalObject: {
missingInNew: "this isnt a FieldDBObject so it will be undefined after merge."
}
},
new FieldDBObject({
id: "conflictingContents",
conflicting: "in first collection"
})
]);
atriviallyDifferentCollection = new Collection([new FieldDBObject({
id: "penguin"
}), new FieldDBObject({
id: "cuckoo",
missingInOriginal: "hi there"
}), {
id: "willBeOverwritten",
internalObject: {
missingInOriginal: "this isnt a FieldDBObject so it will be fully overwritten."
}
},
new FieldDBObject({
id: "robin",
externalObject: new FieldDBObject({
internalString: "internal is different",
internalTrue: true,
internalEmptyString: "",
internalFalse: false,
internalNumber: 2,
internalEqualString: "i'm a old property",
// debugMode: true
})
}), aBaseCollection.cardinal, {
id: "onlyinNew"
},
new FieldDBObject({
id: "conflictingContents",
conflicting: "in second collection"
})
]);
});
it("should be able to merge items in collections using their primary key", function() {
expect(aBaseCollection.fieldDBtype).toEqual("Collection");
expect(aBaseCollection.robin.fieldDBtype).toEqual("FieldDBObject");
expect(aBaseCollection.onlyintarget).toBeDefined();
expect(aBaseCollection._collection.length).toEqual(7);
expect(aBaseCollection._collection.map(function(item) {
return item.id;
})).toEqual(["penguin", "cuckoo", "robin", "cardinal", "onlyinTarget", "willBeOverwritten", "conflictingContents"]);