forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_test.js
283 lines (259 loc) · 6.57 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
steal("can/util", "can/list", "can/test", "can/compute", "steal-qunit", function(){
QUnit.module('can/list');
test('list attr changes length', function () {
var l = new can.List([
0,
1,
2
]);
l.attr(3, 3);
equal(l.length, 4);
});
test('removeAttr on list', function() {
var l = new can.List([0, 1, 2]);
l.removeAttr(1);
equal(l.attr('length'), 2);
deepEqual(l.attr(), [0, 2]);
});
test('list splice', function () {
var l = new can.List([
0,
1,
2,
3
]),
first = true;
l.bind('change', function (ev, attr, how, newVals, oldVals) {
equal(attr, '1');
if (first) {
equal(how, 'remove', 'removing items');
equal(newVals, undefined, 'no new Vals');
} else {
deepEqual(newVals, [
'a',
'b'
], 'got the right newVals');
equal(how, 'add', 'adding items');
}
first = false;
});
l.splice(1, 2, 'a', 'b');
deepEqual(l.serialize(), [
0,
'a',
'b',
3
], 'serialized');
});
test('list pop', function () {
var l = new can.List([
0,
1,
2,
3
]);
l.bind('change', function (ev, attr, how, newVals, oldVals) {
equal(attr, '3');
equal(how, 'remove');
equal(newVals, undefined);
deepEqual(oldVals, [3]);
});
l.pop();
deepEqual(l.serialize(), [
0,
1,
2
]);
});
test('remove nested property in item of array map', function () {
var state = new can.List([{
nested: true
}]);
state.bind('change', function (ev, attr, how, newVal, old) {
equal(attr, '0.nested');
equal(how, 'remove');
deepEqual(old, true);
});
state.removeAttr('0.nested');
equal(undefined, state.attr('0.nested'));
});
test('pop unbinds', function () {
var l = new can.List([{
foo: 'bar'
}]);
var o = l.attr(0),
count = 0;
l.bind('change', function (ev, attr, how, newVal, oldVal) {
count++;
if (count === 1) {
equal(attr, '0.foo', 'count is set');
} else if (count === 2) {
equal(how, 'remove');
equal(attr, '0');
} else {
ok(false, 'called too many times');
}
});
equal(o.attr('foo'), 'bar');
o.attr('foo', 'car');
l.pop();
o.attr('foo', 'bad');
});
test('splice unbinds', function () {
var l = new can.List([{
foo: 'bar'
}]);
var o = l.attr(0),
count = 0;
l.bind('change', function (ev, attr, how, newVal, oldVal) {
count++;
if (count === 1) {
equal(attr, '0.foo', 'count is set');
} else if (count === 2) {
equal(how, 'remove');
equal(attr, '0');
} else {
ok(false, 'called too many times');
}
});
equal(o.attr('foo'), 'bar');
o.attr('foo', 'car');
l.splice(0, 1);
o.attr('foo', 'bad');
});
test('always gets right attr even after moving array items', function () {
var l = new can.List([{
foo: 'bar'
}]);
var o = l.attr(0);
l.unshift('A new Value');
l.bind('change', function (ev, attr, how) {
equal(attr, '1.foo');
});
o.attr('foo', 'led you');
});
test('Array accessor methods', 11, function () {
var l = new can.List([
'a',
'b',
'c'
]),
sliced = l.slice(2),
joined = l.join(' | '),
concatenated = l.concat([
2,
1
], new can.List([0]));
ok(sliced instanceof can.List, '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 can.List, 'Concatenated is an Observable list');
deepEqual(concatenated.serialize(), [
'a',
'b',
'c',
2,
1,
0
], 'List 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('splice removes items in IE (#562)', function () {
var l = new can.List(['a']);
l.splice(0, 1);
ok(!l.attr(0), 'all props are removed');
});
test('list sets up computed attributes (#790)', function() {
var List = can.List.extend({
i: can.compute(0),
a: 0
});
var l = new List([1]);
equal(l.attr('i'), 0);
var Map = can.Map.extend({
f: can.compute(0)
});
var m = new Map();
m.attr('f');
});
test('reverse triggers add/remove events (#851)', function() {
expect(6);
var l = new can.List([1,2,3]);
l.bind('change', function() { ok(true, 'change should be called'); });
l.bind('set', function() { ok(false, 'set should not be called'); });
l.bind('add', function() { ok(true, 'add called'); });
l.bind('remove', function() { ok(true, 'remove called'); });
l.bind('length', function() { ok(true, 'length should be called'); });
l.reverse();
});
test('filter', function(){
var l = new can.List([{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('removing expandos on lists', function(){
var list = new can.List(["a","b"]);
list.removeAttr("foo");
equal(list.length, 2);
});
test('No Add Events if List Splice adds the same items that it is removing. (#1277, #1399)', function() {
var list = new can.List(["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 can.List([]),
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.attr(0, 4);
list.pop();
msg = "works on replace()";
list.replace([4]);
});
test("Setting with .attr() out of bounds of length triggers add event with leading undefineds", function() {
var list = new can.List([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 .attr()");
});
list.attr(3, 4);
});
test("No events should fire if removals happened on empty arrays", function() {
var list = new can.List([]),
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.");
});
});