/
list-test.js
1078 lines (889 loc) · 25.2 KB
/
list-test.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
"use strict";
var QUnit = require("steal-qunit");
var DefineList = require("can-define/list/list");
var DefineMap = require("can-define/map/map");
var Observation = require("can-observation");
var define = require("can-define");
var assign = require("can-util/js/assign/assign");
var CID = require("can-cid");
var types = require("can-types");
var stache = require("can-stache");
QUnit.module("can-define/list/list");
QUnit.test("List is an event emitter", function(assert) {
var Base = DefineList.extend({});
assert.ok(Base.on, 'Base has event methods.');
var List = Base.extend({});
assert.ok(List.on, 'List has event methods.');
});
QUnit.test("creating an instance", function() {
var list = new DefineList([ "a", "b", "c" ]);
list.on("add", function(ev, newVals, index) {
QUnit.deepEqual(newVals, [ "d" ]);
QUnit.equal(index, 3);
});
list.push("d");
});
test('list attr changes length', function() {
var l = new DefineList([
0,
1,
2
]);
l.set(3, 3);
equal(l.length, 4);
});
test('remove on pop', function() {
var l = new DefineList([ 0, 1, 2 ]);
l.pop();
equal(l.length, 2);
deepEqual(l.get(), [ 0, 1 ]);
});
test('list splice', function() {
var l = new DefineList([
0,
1,
2,
3
]);
l.on('add', function(ev, newVals, index) {
deepEqual(newVals, [
'a',
'b'
], 'got the right newVals');
equal(index, 1, 'adding items');
});
l.on('remove', function(ev, oldVals, index) {
deepEqual(oldVals, [
1,
2
], 'got the right oldVals');
equal(index, 1, 'no new Vals');
});
l.splice(1, 2, 'a', 'b');
deepEqual(l.get(), [
0,
'a',
'b',
3
], 'serialized');
});
test('Array accessor methods', 11, function() {
var l = new DefineList([
'a',
'b',
'c'
]),
sliced = l.slice(2),
joined = l.join(' | '),
concatenated = l.concat([
2,
1
], new DefineList([ 0 ]));
ok(sliced instanceof DefineList, 'Slice is an Observable list');
equal(sliced.length, 1, 'Sliced off two elements');
equal(sliced[0], 'c', 'Single element as expected');
equal(joined, 'a | b | c', 'Joined list properly');
ok(concatenated instanceof DefineList, 'Concatenated is an Observable list');
deepEqual(concatenated.serialize(), [
'a',
'b',
'c',
2,
1,
0
], 'DefineList concatenated properly');
l.forEach(function(letter, index) {
ok(true, 'Iteration');
if (index === 0) {
equal(letter, 'a', 'First letter right');
}
if (index === 2) {
equal(letter, 'c', 'Last letter right');
}
});
});
test('Concatenated list items Equal original', function() {
var l = new DefineList([
{ firstProp: "Some data" },
{ secondProp: "Next data" }
]),
concatenated = l.concat([
{ hello: "World" },
{ foo: "Bar" }
]);
ok(l[0] === concatenated[0], "They are Equal");
ok(l[1] === concatenated[1], "They are Equal");
});
test('Lists with maps concatenate properly', function() {
var Person = DefineMap.extend();
var People = DefineList.extend({
'#': Person
});
var Genius = Person.extend();
var Animal = DefineMap.extend();
var me = new Person({ name: "John" });
var animal = new Animal({ name: "Tak" });
var genius = new Genius({ name: "Einstein" });
var hero = { name: "Ghandi" };
var people = new People([]);
var specialPeople = new People([
genius,
hero
]);
people = people.concat([ me, animal, specialPeople ], specialPeople, [ 1, 2 ], 3);
ok(people.length === 8, "List length is right");
ok(people[0] === me, "Map in list === vars created before concat");
ok(people[1] instanceof Person, "Animal got serialized to Person");
});
test('splice removes items in IE (#562)', function() {
var l = new DefineList([ 'a' ]);
l.splice(0, 1);
ok(!l.get(0), 'all props are removed');
});
test('reverse triggers add/remove events (#851)', function() {
expect(4);
var l = new DefineList([ 1, 2, 3 ]);
l.on('add', function() {
ok(true, 'add called');
});
l.on('remove', function() {
ok(true, 'remove called');
});
l.on('length', function() {
ok(true, 'length should be called');
});
l.reverse();
deepEqual(l.get(), [ 3, 2, 1 ], "reversed");
});
test('filter', function() {
var l = new DefineList([ { id: 1, name: "John" }, { id: 2, name: "Mary" } ]);
var filtered = l.filter(function(item) {
return item.name === "Mary";
});
notEqual(filtered._cid, l._cid, "not same object");
equal(filtered.length, 1, "one item");
equal(filtered[0].name, "Mary", "filter works");
});
test('No Add Events if DefineList Splice adds the same items that it is removing. (#1277, #1399)', function() {
var list = new DefineList([ "a", "b" ]);
list.bind('add', function() {
ok(false, 'Add callback should not be called.');
});
list.bind('remove', function() {
ok(false, 'Remove callback should not be called.');
});
var result = list.splice(0, 2, "a", "b");
deepEqual(result, [ "a", "b" ]);
});
test("add event always returns an array as the value (#998)", function() {
var list = new DefineList([]),
msg;
list.bind("add", function(ev, newElements, index) {
deepEqual(newElements, [ 4 ], msg);
});
msg = "works on push";
list.push(4);
list.pop();
msg = "works on attr()";
list.set(0, 4);
list.pop();
msg = "works on replace()";
list.replace([ 4 ]);
});
test("Setting with .set() out of bounds of length triggers add event with leading undefineds", function() {
var list = new DefineList([ 1 ]);
list.bind("add", function(ev, newElements, index) {
deepEqual(newElements, [ undefined, undefined, 4 ],
"Leading undefineds are included");
equal(index, 1, "Index takes into account the leading undefineds from a .set()");
});
list.set(3, 4);
});
test("No events should fire if removals happened on empty arrays", function() {
var list = new DefineList([]),
msg;
list.bind("remove", function(ev, removed, index) {
ok(false, msg);
});
msg = "works on pop";
list.pop();
msg = "works on shift";
list.shift();
ok(true, "No events were fired.");
});
test('setting an index out of bounds does not create an array', function() {
expect(1);
var l = new DefineList();
l.set('1', 'foo');
equal(l.get('1'), 'foo');
});
test('splice with similar but less items works (#1606)', function() {
var list = new DefineList([ 'aa', 'bb', 'cc' ]);
list.splice(0, list.length, 'aa', 'cc', 'dd');
deepEqual(list.get(), [ 'aa', 'cc', 'dd' ]);
list.splice(0, list.length, 'aa', 'cc');
deepEqual(list.get(), [ 'aa', 'cc' ]);
});
test('filter returns same list type (#1744)', function() {
var ParentList = DefineList.extend();
var ChildList = ParentList.extend();
var children = new ChildList([ 1, 2, 3 ]);
ok(children.filter(function() {}) instanceof ChildList);
});
test('reverse returns the same list instance (#1744)', function() {
var ParentList = DefineList.extend();
var ChildList = ParentList.extend();
var children = new ChildList([ 1, 2, 3 ]);
ok(children.reverse() === children);
});
test("slice and join are observable by a compute (#1884)", function() {
expect(2);
var list = new DefineList([ 1, 2, 3 ]);
var sliced = new Observation(function() {
return list.slice(0, 1);
}, null, {
updater: function(newVal) {
deepEqual(newVal.get(), [ 2 ], "got a new DefineList");
}
});
sliced.start();
var joined = new Observation(function() {
return list.join(",");
}, null, {
updater: function(newVal) {
equal(newVal, "2,3", "joined is observable");
}
});
joined.start();
list.shift();
});
test('list.replace', function() {
var firstArray = [
{ id: 1, name: "Marshall" },
{ id: 2, name: "Austin" },
{ id: 3, name: "Hyrum" }
];
var myList = new DefineList(firstArray);
var newArray = [
{ id: 4, name: "Aubree" },
{ id: 5, name: "Leah" },
{ id: 6, name: 'Lily' }
];
myList.replace(newArray);
equal(myList.length, 3);
equal(myList[0].name, "Aubree");
equal(myList[1].name, "Leah");
equal(myList[2].name, "Lily", "Can replace a List with an Array.");
myList.replace(firstArray);
equal(myList.length, 3);
equal(myList[0].name, "Marshall");
equal(myList[1].name, "Austin");
equal(myList[2].name, "Hyrum", "Can replace a List with another List.");
});
test('list.map', function() {
var myArray = [
{ id: 1, name: "Marshall" },
{ id: 2, name: "Austin" },
{ id: 3, name: "Hyrum" }
];
var myList = new DefineList(myArray);
var newList = myList.map(function(person) {
person.lastName = "Thompson";
return person;
});
equal(newList.length, 3);
equal(newList[0].name, "Marshall");
equal(newList[0].lastName, "Thompson");
equal(newList[1].name, "Austin");
equal(newList[1].lastName, "Thompson");
equal(newList[2].name, "Hyrum");
equal(newList[2].lastName, "Thompson");
var ExtendedList = DefineList.extend({
testMe: function() {
return "It Worked!";
}
});
var myExtendedList = new ExtendedList(myArray);
var newExtendedList = myExtendedList.map(function(person) {
person.lastName = "Thompson";
return person;
});
try {
newExtendedList.testMe();
} catch(err) {
QUnit.ok(err.message.match(/testMe/), 'Does not return the same type of list.');
}
});
test('list.sort a simple list', function() {
var myList = new DefineList([
"Marshall",
"Austin",
"Hyrum"
]);
myList.sort();
equal(myList.length, 3);
equal(myList[0], "Austin");
equal(myList[1], "Hyrum");
equal(myList[2], "Marshall", "Basic list was properly sorted.");
});
test('list.sort a list of objects', function() {
var objList = new DefineList([
{ id: 1, name: "Marshall" },
{ id: 2, name: "Austin" },
{ id: 3, name: "Hyrum" }
]);
objList.sort(function(a, b) {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
} else {
return 0;
}
});
equal(objList.length, 3);
equal(objList[0].name, "Austin");
equal(objList[1].name, "Hyrum");
equal(objList[2].name, "Marshall", "List of objects was properly sorted.");
});
test('list.sort a list of DefineMaps', function() {
var Account = DefineMap.extend({
name: "string",
amount: "number",
slug: {
serialize: true,
get: function() {
return this.name.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '');
}
}
});
Account.List = DefineList.extend({
"*": Account,
limit: "number",
skip: "number",
total: "number"
});
var accounts = new Account.List([
{
name: "Savings",
amount: 20.00
},
{
name: "Checking",
amount: 103.24
},
{
name: "Kids Savings",
amount: 48155.13
}
]);
accounts.limit = 3;
var template = stache('{{#each accounts}}{{name}},{{/each}}')({ accounts: accounts });
equal(template.textContent, "Savings,Checking,Kids Savings,", "template rendered properly.");
accounts.sort(function(a, b) {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
} else {
return 0;
}
});
equal(accounts.length, 3);
equal(template.textContent, "Checking,Kids Savings,Savings,", "template updated properly.");
// Try sorting in reverse on the dynamic `slug` property
accounts.sort(function(a, b) {
if (a.slug < b.slug) {
return 1;
} else if (a.slug > b.slug) {
return -1;
} else {
return 0;
}
});
equal(accounts.length, 3);
equal(accounts.limit, 3, "expandos still present after sorting/replacing.");
equal(template.textContent, "Savings,Kids Savings,Checking,", "template updated properly.");
});
test('list.sort a list of objects without losing reference (#137)', function() {
var unSorted = new DefineList([ { id: 3 }, { id: 2 }, { id: 1 } ]);
var sorted = unSorted.slice(0).sort(function(a, b) {
return a.id > b.id ? 1 : (a.id < b.id ? -1 : 0);
});
equal(unSorted[0], sorted[2], 'items should be equal');
});
test("list defines", 6, function() {
var Todo = function(props) {
assign(this, props);
CID(this);
};
define(Todo.prototype, {
completed: "boolean",
destroyed: {
value: false
}
});
Todo.prototype.destroy = function() {
this.destroyed = true;
};
var TodoList = DefineList.extend({
"*": Todo,
remaining: {
get: function() {
return this.filter({
completed: false
});
}
},
completed: {
get: function() {
return this.filter({
completed: true
});
}
},
destroyCompleted: function() {
this.completed.forEach(function(todo) {
todo.destroy();
});
},
setCompletedTo: function(value) {
this.forEach(function(todo) {
todo.completed = value;
});
}
});
var todos = new TodoList([ { completed: true }, { completed: false } ]);
ok(todos.item(0) instanceof Todo, "correct instance");
equal(todos.completed.length, 1, "only one todo");
todos.on("completed", function(ev, newVal, oldVal) {
ok(newVal instanceof TodoList, "right type");
equal(newVal.length, 2, "all items");
ok(oldVal instanceof TodoList, "right type");
equal(oldVal.length, 1, "all items");
});
todos.setCompletedTo(true);
});
QUnit.test("extending the base supports overwriting _eventSetup", function() {
var L = DefineList.extend({});
Object.getOwnPropertyDescriptor(DefineMap.prototype, "_eventSetup");
L.prototype.arbitraryProp = true;
ok(true, "set arbitraryProp");
L.prototype._eventSetup = function() {};
ok(true, "worked");
});
QUnit.test("setting expandos on a DefineList", function() {
var DL = DefineList.extend({
count: "number"
});
var dl = new DL();
dl.set({ count: 5, skip: 2 });
QUnit.equal(dl.get("count"), 5, "read with .get defined"); //-> 5
QUnit.equal(dl.count, 5, "read with . defined");
QUnit.equal(dl.get("skip"), 2, "read with .get expando");
QUnit.equal(dl.skip, 2, "read with . expando");
QUnit.equal(dl.get("limit"), undefined, "read with .get undefined");
});
QUnit.test("passing a DefineList to DefineList (#33)", function() {
var m = new DefineList([ {}, {} ]);
var m2 = new DefineList(m);
QUnit.deepEqual(m.get(), m2.get());
QUnit.ok(m[0] === m2[0], "index the same");
QUnit.ok(m[1] === m2[1], "index the same");
});
QUnit.test("reading and setting expandos", function() {
var list = new DefineList();
var countObservation = new Observation(function() {
return list.get("count");
}, null, function(newValue) {
QUnit.equal(newValue, 1000, "got new value");
});
countObservation.start();
list.set("count", 1000);
QUnit.equal(countObservation.value, 1000);
var list2 = new DefineList();
list2.on("count", function(ev, newVal) {
QUnit.equal(newVal, 5);
});
list2.set("count", 5);
});
QUnit.test("is list like", function() {
var list = new DefineList();
QUnit.ok(types.isListLike(list));
});
QUnit.test("extending DefineList constructor functions (#61)", function() {
var AList = DefineList.extend('AList', { aProp: {}, aMethod: function() {} });
var BList = AList.extend('BList', { bProp: {}, bMethod: function() {} });
var CList = BList.extend('CList', { cProp: {}, cMethod: function() {} });
var list = new CList([ {}, {} ]);
list.on("aProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "PROP");
QUnit.equal(oldVal, undefined);
});
list.on("bProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "FOO");
QUnit.equal(oldVal, undefined);
});
list.on("cProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "BAR");
QUnit.equal(oldVal, undefined);
});
list.aProp = "PROP";
list.bProp = 'FOO';
list.cProp = 'BAR';
QUnit.ok(list.aMethod);
QUnit.ok(list.bMethod);
QUnit.ok(list.cMethod);
});
QUnit.test("extending DefineList constructor functions more than once (#61)", function() {
var AList = DefineList.extend("AList", { aProp: {}, aMethod: function() {} });
var BList = AList.extend("BList", { bProp: {}, bMethod: function() {} });
var CList = AList.extend("CList", { cProp: {}, cMethod: function() {} });
var list1 = new BList([ {}, {} ]);
var list2 = new CList([ {}, {}, {} ]);
list1.on("aProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "PROP", "aProp newVal on list1");
QUnit.equal(oldVal, undefined);
});
list1.on("bProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "FOO", "bProp newVal on list1");
QUnit.equal(oldVal, undefined);
});
list2.on("aProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "PROP", "aProp newVal on list2");
QUnit.equal(oldVal, undefined);
});
list2.on("cProp", function(ev, newVal, oldVal) {
QUnit.equal(newVal, "BAR", "cProp newVal on list2");
QUnit.equal(oldVal, undefined);
});
list1.aProp = "PROP";
list1.bProp = 'FOO';
list2.aProp = "PROP";
list2.cProp = 'BAR';
QUnit.ok(list1.aMethod, "list1 aMethod");
QUnit.ok(list1.bMethod);
QUnit.ok(list2.aMethod);
QUnit.ok(list2.cMethod, "list2 cMethod");
});
QUnit.test("extending DefineList constructor functions - value (#61)", function() {
var AList = DefineList.extend("AList", { aProp: { value: 1 } });
var BList = AList.extend("BList", { });
var CList = BList.extend("CList", { });
var c = new CList([]);
QUnit.equal(c.aProp, 1, "got initial value");
});
QUnit.test("'*' inheritance works (#61)", function() {
var Account = DefineMap.extend({
name: "string",
amount: "number",
slug: {
serialize: true,
get: function() {
return this.name.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '');
}
}
});
var BaseList = DefineList.extend({
"*": Account
});
var ExtendedList = BaseList.extend({});
var xl = new ExtendedList([ {} ]);
QUnit.ok(xl[0] instanceof Account);
});
QUnit.test("shorthand getter setter (#56)", function() {
var People = DefineList.extend({
first: "*",
last: "*",
get fullName() {
return this.first + " " + this.last;
},
set fullName(newVal) {
var parts = newVal.split(" ");
this.first = parts[0];
this.last = parts[1];
}
});
var p = new People([]);
p.fullName = "Mohamed Cherif";
p.on("fullName", function(ev, newVal, oldVal) {
QUnit.equal(oldVal, "Mohamed Cherif");
QUnit.equal(newVal, "Justin Meyer");
});
equal(p.fullName, "Mohamed Cherif", "fullName initialized right");
p.fullName = "Justin Meyer";
});
QUnit.test("added and removed are called after items are added/removed (#14)", function() {
var Person = DefineMap.extend({
id: "number",
name: "string"
});
var addedFuncCalled, removedFuncCalled, theList;
var People = DefineList.extend({
"#": {
added: function(items, index) {
addedFuncCalled = true;
ok(items, "items added got passed to added");
ok(typeof index === 'number', "index of items was passed to added and is a number");
ok(items[0].name === 'John', "Name was correct");
theList = this;
},
removed: function(items, index) {
removedFuncCalled = true;
ok(items, "items added got passed to removed");
ok(typeof index === 'number', "index of items was passed to removed and is a number");
theList = this;
},
Type: Person
},
outsideProp: {
type: "boolean",
value: true
}
});
var people = new People([]);
var me = new Person();
me.name = "John";
me.id = "1234";
ok(!addedFuncCalled, "added function has not been called yet");
people.push(me);
ok(addedFuncCalled, "added function was called");
ok(theList.outsideProp === true && theList instanceof People,
"the list was passed correctly as this to added");
theList = null;
ok(!removedFuncCalled, "removed function has not been called yet");
people.splice(people.indexOf(me), 1);
ok(removedFuncCalled, "removed function was called");
ok(theList.outsideProp === true && theList instanceof People,
"the list was passed correctly as this to removed");
});
QUnit.test("* vs # (#78)", function() {
var MyList = DefineList.extend({
"*": "number",
"#": {
added: function() {
ok(true, "called on init");
},
removed: function() {},
type: "string"
}
});
var list = new MyList([ 1, 2, 3 ]);
QUnit.ok(list[0] === "1", "converted to string");
list.set("prop", "4");
QUnit.ok(list.prop === 4, "type converted");
});
QUnit.test("Array shorthand uses #", function() {
var MyMap = DefineMap.extend({
"numbers": [ "number" ]
});
var map = new MyMap({ numbers: [ "1", "2" ] });
QUnit.ok(map.numbers[0] === 1, "converted to number");
map.numbers.set("prop", "4");
QUnit.ok(map.numbers.prop === "4", "type left alone");
});
QUnit.test("replace-with-self lists are diffed properly (can-view-live#10)", function() {
var a = new DefineMap({ name: "A" });
var b = new DefineMap({ name: "B" });
var c = new DefineMap({ name: "C" });
var d = new DefineMap({ name: "D" });
expect(4);
var list1 = new DefineList([ a, b ]);
list1.on("add", function(ev, newVals, where) {
throw new Error("list1 should not add.");
});
list1.on("remove", function(ev, oldVals, where) {
throw new Error("list1 should not remove.");
});
list1.replace([ a, b ]);
var list2 = new DefineList([ a, b, c ]);
list2.on("add", function(ev, newVals, where) {
equal(newVals.length, 1, "list2 added length");
equal(where, 2, "list2 added location");
});
list2.on("remove", function(ev, oldVals, where) {
equal(oldVals.length, 1, "list2 removed length");
equal(where, 2, "list2 removed location");
});
list2.replace([ a, b, d ]);
});
QUnit.test("set >= length - triggers length event (#152)", function() {
var l = new DefineList([ 1, 2, 3 ]);
var batchNum = null;
l.on("add", function(e) {
ok(true, "add called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
l.on("remove", function(e) {
ok(false, "remove called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
l.on("length", function(e) {
ok(true, "length called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
expect(4);
l.set(3, 5);
deepEqual(l.get(), [ 1, 2, 3, 5 ], "updated list");
});
QUnit.test("set < length - triggers length event (#150)", function() {
var l = new DefineList([ 1, 2, 3 ]);
var batchNum = null;
l.on("add", function(e) {
ok(true, "add called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
l.on("remove", function(e) {
ok(true, "remove called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
l.on("length", function(e) {
ok(true, "length called");
if (batchNum === null) {
batchNum = e.batchNum;
}
else {
equal(batchNum, e.batchNum, "batch numbers match");
}
});
expect(6);
l.set(2, 4);
deepEqual(l.get(), [ 1, 2, 4 ], "updated list");
});
QUnit.test("set/splice are observable", function() {
var list = new DefineList([ 1, 2, 3, 4, 5 ]);
var count = new Observation(function() {
var count = 0;
for (var i = 0; i < list.length; i++) {
count += (list[i] % 2) ? 1 : 0;
}
return count;
}, null, {
updater: function() {
ok(true);
}
});
count.start();
expect(3);
list.set(3, 5);
list.set(2, 4);
list.splice(1, 1, 1);
});
QUnit.test("setting length > current (#147)", function() {
var list = new DefineList([ 1, 2 ]);
list.length = 5;
equal(list.length, 5);
equal(list.hasOwnProperty(0), true);
equal(list.hasOwnProperty(1), true);
equal(list.hasOwnProperty(2), true);
equal(list.hasOwnProperty(3), true);
equal(list.hasOwnProperty(4), true);
equal(list.hasOwnProperty(5), false);
});
QUnit.test("setting length < current (#147)", function() {
var list = new DefineList([ 1, 2, 3, 4, 5 ]);
list.length = 3;
equal(list.length, 3);
equal(list.hasOwnProperty(0), true);
equal(list.hasOwnProperty(1), true);
equal(list.hasOwnProperty(2), true);
equal(list.hasOwnProperty(3), false);
equal(list.hasOwnProperty(4), false);
equal(list.hasOwnProperty(5), false);
});
test('every', function() {
var l = new DefineList([ { id: 1, name: "Bob" }, { id: 2, name: "Bob" } ]);
var allBobs = l.every(function(item) {
return item.name === "Bob";
});
ok(allBobs, "Every works in true case");
var idOne = l.every(function(item) {
return item.id === 1;
});
ok(!idOne, "Every works in false case");