forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.js
291 lines (229 loc) · 6.71 KB
/
map_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
/* jshint asi:true*/
steal("can/map", "can/compute", "can/test", "can/list", function(){
module('can/map');
test("Basic Map", 4, function () {
var state = new can.Map({
category: 5,
productType: 4
});
state.bind("change", function (ev, attr, how, val, old) {
equal(attr, "category", "correct change name");
equal(how, "set");
equal(val, 6, "correct");
equal(old, 5, "correct");
});
state.attr("category", 6);
state.unbind("change");
});
test("Nested Map", 5, function () {
var me = new can.Map({
name: {
first: "Justin",
last: "Meyer"
}
});
ok(me.attr("name") instanceof can.Map);
me.bind("change", function (ev, attr, how, val, old) {
equal(attr, "name.first", "correct change name")
equal(how, "set")
equal(val, "Brian", "correct")
equal(old, "Justin", "correct")
})
me.attr("name.first", "Brian");
me.unbind("change")
})
test("remove attr", function () {
var state = new can.Map({
category: 5,
productType: 4
});
state.removeAttr("category");
deepEqual(can.Map.keys(state), ["productType"], "one property");
});
test("remove attr on key with dot", function () {
var state = new can.Map({
"key.with.dots": 12,
productType: 4
});
var state2 = new can.Map({
"key.with.dots": 4,
key: {
"with": {
someValue: 20
}
}
});
state.removeAttr("key.with.dots");
state2.removeAttr("key.with.someValue");
deepEqual( can.Map.keys(state), ["productType"], "one property");
deepEqual( can.Map.keys(state2), ["key.with.dots", "key"], "two properties");
deepEqual( can.Map.keys( state2.key["with"] ) , [], "zero properties");
});
test("nested event handlers are not run by changing the parent property (#280)", function () {
var person = new can.Map({
name: {
first: "Justin"
}
})
person.bind("name.first", function (ev, newName) {
ok(false, "name.first should never be called")
//equal(newName, "hank", "name.first handler called back with correct new name")
});
person.bind("name", function () {
ok(true, "name event triggered")
})
person.attr("name", {
first: "Hank"
});
});
test("cyclical objects (#521)", function () {
var foo = {};
foo.foo = foo;
var fooed = new can.Map(foo);
ok(true, "did not cause infinate recursion");
ok(fooed.attr('foo') === fooed, "map points to itself")
var me = {
name: "Justin"
}
var references = {
husband: me,
friend: me
}
var ref = new can.Map(references)
ok(ref.attr('husband') === ref.attr('friend'), "multiple properties point to the same thing")
})
test('Getting attribute that is a can.compute should return the compute and not the value of the compute (#530)', function () {
var compute = can.compute('before');
var map = new can.Map({
time: compute
});
equal(map.time, compute, 'dot notation call of time is compute');
equal(map.attr('time'), compute, '.attr() call of time is compute');
})
test('_cid add to original object', function () {
var map = new can.Map(),
obj = {
'name': 'thecountofzero'
};
map.attr('myObj', obj);
ok(!obj._cid, '_cid not added to original object');
})
test("can.each used with maps", function () {
can.each(new can.Map({
foo: "bar"
}), function (val, attr) {
if (attr === "foo") {
equal(val, "bar")
} else {
ok(false, "no properties other should be called " + attr)
}
})
})
test("can.Map serialize triggers reading (#626)", function () {
var old = can.__reading;
var attributesRead = [];
var readingTriggeredForKeys = false;
can.__reading = function (object, attribute) {
if (attribute === "__keys") {
readingTriggeredForKeys = true;
} else {
attributesRead.push(attribute);
}
};
var testMap = new can.Map({
cats: "meow",
dogs: "bark"
});
testMap.serialize();
ok(can.inArray("cats", attributesRead ) !== -1 && can.inArray( "dogs", attributesRead ) !== -1, "map serialization triggered __reading on all attributes");
ok(readingTriggeredForKeys, "map serialization triggered __reading for __keys");
can.__reading = old;
})
test("Test top level attributes", 7, function () {
var test = new can.Map({
'my.enable': false,
'my.item': true,
'my.count': 0,
'my.newCount': 1,
'my': {
'value': true,
'nested': {
'value': 100
}
}
});
equal(test.attr('my.value'), true, 'correct');
equal(test.attr('my.nested.value'), 100, 'correct');
ok(test.attr("my.nested") instanceof can.Map);
equal(test.attr('my.enable'), false, 'falsey (false) value accessed correctly');
equal(test.attr('my.item'), true, 'truthey (true) value accessed correctly');
equal(test.attr('my.count'), 0, 'falsey (0) value accessed correctly');
equal(test.attr('my.newCount'), 1, 'falsey (1) value accessed correctly');
});
test("computed properties don't cause memory leaks", function () {
var computeMap = can.Map.extend({
'name': can.compute(function(){
return this.attr('first') + this.attr('last')
})
}),
handler = function(){},
map = new computeMap({
first: 'Mickey',
last: 'Mouse'
});
map.bind('name', handler);
map.bind('name', handler);
equal(map._computedBindings.name.count, 2, '2 handlers listening to computed property');
map.unbind('name', handler);
map.unbind('name', handler);
equal(map._computedBindings.name.count, 0, '0 handlers listening to computed property');
ok(!map._computedBindings.name.handler, 'computed property handler removed');
});
test("serializing cycles", function(){
var map1 = new can.Map({name: "map1"});
var map2 = new can.Map({name: "map2"});
map1.attr("map2", map2);
map2.attr("map1", map1);
var res = map1.serialize();
equal(res.name, "map1");
equal(res.map2.name, "map2");
});
test("Unbinding from a map with no bindings doesn't throw an error (#1015)", function() {
expect(0);
var test = new can.Map({});
try {
test.unbind('change');
} catch(e) {
ok(false, 'No error should be thrown');
}
});
test("Fast dispatch event still has target and type (#1082)", 4, function() {
var data = new can.Map({
name: 'CanJS'
});
data.bind('change', function(ev){
equal(ev.type, 'change');
equal(ev.target, data);
});
data.bind('name', function(ev){
equal(ev.type, 'name');
equal(ev.target, data);
});
data.attr('name', 'David');
});
test("map passed to Map constructor (#1166)", function(){
var map = new can.Map({x: 1});
var res = new can.Map(map);
deepEqual(res.attr(), {
x: 1
}, "has the same properties");
});
test("constructor passed to scope is threated as a property (#1261)", function(){
var Constructor = can.Construct.extend({});
var Map = can.Map.extend({
Todo: Constructor
});
var m = new Map();
equal(m.attr("Todo"), Constructor);
});
});