/
map-test.js
688 lines (546 loc) · 15.1 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
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
"use strict";
var QUnit = require("steal-qunit");
var DefineMap = require("can-define/map/map");
var define = require("can-define");
var Observation = require("can-observation");
var canTypes = require("can-types");
var each = require("can-util/js/each/each");
var compute = require("can-compute");
var assign = require("can-util/js/assign/assign");
var sealWorks = (function() {
try {
var o = {};
Object.seal(o);
o.prop = true;
return false;
} catch(e) {
return true;
}
})();
QUnit.module("can-define/map/map");
QUnit.test("Map is an event emitter", function (assert) {
var Base = DefineMap.extend({});
assert.ok(Base.on, 'Base has event methods.');
var Map = Base.extend({});
assert.ok(Map.on, 'Map has event methods.');
});
QUnit.test("creating an instance", function(){
var map = new DefineMap({prop: "foo"});
map.on("prop", function(ev, newVal, oldVal){
QUnit.equal(newVal, "BAR");
QUnit.equal(oldVal, "foo");
});
map.prop = "BAR";
});
QUnit.test("creating an instance with nested prop", function(){
var map = new DefineMap({name: {first: "Justin"}});
map.name.on("first", function(ev, newVal, oldVal){
QUnit.equal(newVal, "David");
QUnit.equal(oldVal, "Justin");
});
map.name.first = "David";
});
QUnit.test("extending", function(){
var MyMap = DefineMap.extend({
prop: {}
});
var map = new MyMap();
map.on("prop", function(ev, newVal, oldVal){
QUnit.equal(newVal, "BAR");
QUnit.equal(oldVal, undefined);
});
map.prop = "BAR";
});
QUnit.test("loop only through defined serializable props", function(){
var MyMap = DefineMap.extend({
propA: {},
propB: {serialize: false},
propC: {
get: function(){
return this.propA;
}
}
});
var inst = new MyMap({propA: 1, propB: 2});
QUnit.deepEqual(Object.keys(inst.get()), ["propA"]);
});
QUnit.test("get and set can setup expandos", function(){
var map = new DefineMap();
var oi = new Observation(function(){
return map.get("foo");
},null,{
updater: function(newVal){
QUnit.equal(newVal, "bar", "updated to bar");
}
});
oi.start();
map.set("foo","bar");
});
QUnit.test("default settings", function(){
var MyMap = DefineMap.extend({
"*": "string",
foo: {}
});
var m = new MyMap();
m.set("foo",123);
QUnit.ok(m.get("foo") === "123");
});
QUnit.test("default settings on unsealed", function(){
var MyMap = DefineMap.extend({
seal: false
},{
"*": "string"
});
var m = new MyMap();
m.set("foo",123);
QUnit.ok(m.get("foo") === "123");
});
if (!System.isEnv('production')) {
QUnit.test("extends sealed objects (#48)", function(){
var Map1 = DefineMap.extend({ seal: true }, {
name: {
get: function(curVal){
return "computed " + curVal;
}
}
});
var Map2 = Map1.extend({ seal: false }, {});
var Map3 = Map2.extend({ seal: true }, {});
var map1 = new Map1({ name: "Justin" });
try {
map1.foo = "bar";
if (map1.foo) {
QUnit.ok(false, "map1 not sealed");
} else {
QUnit.ok(true, "map1 sealed - silent failure");
}
} catch(ex) {
QUnit.ok(true, "map1 sealed");
}
QUnit.equal(map1.name, "computed Justin", "map1.name property is computed");
var map2 = new Map2({ name: "Brian" });
try {
map2.foo = "bar";
if (map2.foo) {
QUnit.ok(true, "map2 not sealed");
} else {
QUnit.ok(false, "map2 sealed");
}
} catch (ex) {
QUnit.ok(false, "map2 sealed");
}
QUnit.equal(map2.name, "computed Brian", "map2.name property is computed");
var map3 = new Map3({ name: "Curtis" });
try {
map3.foo = "bar";
if (map3.foo) {
QUnit.ok(false, "map3 not sealed");
} else {
QUnit.ok(true, "map3 sealed");
}
} catch (ex) {
QUnit.ok(true, "map3 sealed");
}
QUnit.equal(map3.name, "computed Curtis", "map3.name property is computed");
});
}
QUnit.test("get with dynamically added properties", function(){
var map = new DefineMap();
map.set("a",1);
map.set("b",2);
QUnit.deepEqual(map.get(), {a: 1, b: 2});
});
QUnit.test("set multiple props", function(){
var map = new DefineMap();
map.set({a: 0, b: 2});
QUnit.deepEqual(map.get(), {a: 0, b: 2});
map.set({a: 2}, true);
QUnit.deepEqual(map.get(), {a: 2});
map.set({foo: {bar: "VALUE"}});
QUnit.deepEqual(map.get(), {foo: {bar: "VALUE"}, a: 2});
});
QUnit.test("serialize responds to added props", function(){
var map = new DefineMap();
var oi = new Observation(function(){
return map.serialize();
},null,{
updater: function(newVal){
QUnit.deepEqual(newVal, {a: 1, b: 2}, "updated right");
}
});
oi.start();
map.set({a: 1, b: 2});
});
QUnit.test("initialize an undefined property", function(){
var MyMap = DefineMap.extend({seal: false},{});
var instance = new MyMap({foo: "bar"});
equal(instance.foo, "bar");
});
QUnit.test("set an already initialized null property", function(){
var map = new DefineMap({ foo: null });
map.set({ foo: null });
equal(map.foo, null);
});
QUnit.test("creating a new key doesn't cause two changes", 1, function(){
var map = new DefineMap();
var oi = new Observation(function(){
return map.serialize();
},null,{
updater: function(newVal){
QUnit.deepEqual(newVal, {a: 1}, "updated right");
}
});
oi.start();
map.set("a", 1);
});
QUnit.test("setting nested object", function(){
var m = new DefineMap({});
m.set({foo: {}});
m.set({foo: {}});
QUnit.deepEqual(m.get(), {foo: {}});
});
QUnit.test("passing a DefineMap to DefineMap (#33)", function(){
var MyMap = DefineMap.extend({foo: "observable"});
var m = new MyMap({foo: {}, bar: {}});
var m2 = new MyMap(m);
QUnit.deepEqual(m.get(), m2.get());
QUnit.ok(m.foo === m2.foo, "defined props the same");
QUnit.ok(m.bar === m2.bar, "expando props the same");
});
QUnit.test("serialize: function works (#38)", function(){
var Something = DefineMap.extend({});
var MyMap = DefineMap.extend({
somethingRef: {
type: function(val){
return new Something({id: val});
},
serialize: function(val){
return val.id;
}
},
somethingElseRef: {
type: function(val){
return new Something({id: val});
},
serialize: false
}
});
var myMap = new MyMap({somethingRef: 2, somethingElseRef: 3});
QUnit.ok(myMap.somethingRef instanceof Something);
QUnit.deepEqual( myMap.serialize(), {somethingRef: 2}, "serialize: function and serialize: false works");
var MyMap2 = DefineMap.extend({
"*": {
serialize: function(value){
return "" + value;
}
}
});
var myMap2 = new MyMap2({foo: 1, bar: 2});
QUnit.deepEqual( myMap2.serialize(), {foo: "1", bar: "2"}, "serialize: function on default works");
});
QUnit.test("isMapLike", function(){
var map = new DefineMap({});
ok(canTypes.isMapLike(map), "is map like");
});
QUnit.test("get will not create properties", function(){
var method = function(){};
var MyMap = DefineMap.extend({
method: method
});
var m = new MyMap();
m.get("foo");
QUnit.equal(m.get("method"), method);
});
QUnit.test("Properties are enumerable", function(){
QUnit.expect(4);
var VM = DefineMap.extend({
foo: "string"
});
var vm = new VM({ foo: "bar", baz: "qux" });
var i = 0;
each(vm, function(value, key){
if(i === 0) {
QUnit.equal(key, "foo");
QUnit.equal(value, "bar");
} else {
QUnit.equal(key, "baz");
QUnit.equal(value, "qux");
}
i++;
});
});
QUnit.test("Getters are not enumerable", function(){
QUnit.expect(2);
var MyMap = DefineMap.extend({
foo: "string",
baz: {
get: function(){
return this.foo;
}
}
});
var map = new MyMap({ foo: "bar" });
each(map, function(value, key){
QUnit.equal(key, "foo");
QUnit.equal(value, "bar");
});
});
QUnit.test("extending DefineMap constructor functions (#18)", function(){
var AType = DefineMap.extend("AType", { aProp: {}, aMethod: function(){} });
var BType = AType.extend("BType", { bProp: {}, bMethod: function(){} });
var CType = BType.extend("CType", { cProp: {}, cMethod: function(){} });
var map = new CType();
map.on("aProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "PROP");
QUnit.equal(oldVal, undefined);
});
map.on("bProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "FOO");
QUnit.equal(oldVal, undefined);
});
map.on("cProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "BAR");
QUnit.equal(oldVal, undefined);
});
map.aProp = "PROP";
map.bProp = 'FOO';
map.cProp = 'BAR';
QUnit.ok(map.aMethod);
QUnit.ok(map.bMethod);
QUnit.ok(map.cMethod);
});
QUnit.test("extending DefineMap constructor functions more than once (#18)", function(){
var AType = DefineMap.extend("AType", { aProp: {}, aMethod: function(){} });
var BType = AType.extend("BType", { bProp: {}, bMethod: function(){} });
var CType = AType.extend("CType", { cProp: {}, cMethod: function(){} });
var map1 = new BType();
var map2 = new CType();
map1.on("aProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "PROP", "aProp newVal on map1");
QUnit.equal(oldVal, undefined);
});
map1.on("bProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "FOO", "bProp newVal on map1");
QUnit.equal(oldVal, undefined);
});
map2.on("aProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "PROP", "aProp newVal on map2");
QUnit.equal(oldVal, undefined);
});
map2.on("cProp", function(ev, newVal, oldVal){
QUnit.equal(newVal, "BAR", "cProp newVal on map2");
QUnit.equal(oldVal, undefined);
});
map1.aProp = "PROP";
map1.bProp = 'FOO';
map2.aProp = "PROP";
map2.cProp = 'BAR';
QUnit.ok(map1.aMethod, "map1 aMethod");
QUnit.ok(map1.bMethod);
QUnit.ok(map2.aMethod);
QUnit.ok(map2.cMethod, "map2 cMethod");
});
QUnit.test("extending DefineMap constructor functions - value (#18)", function(){
var AType = DefineMap.extend("AType", { aProp: {value: 1} });
var BType = AType.extend("BType", { });
var CType = BType.extend("CType",{ });
var c = new CType();
QUnit.equal( c.aProp , 1 ,"got initial value" );
});
QUnit.test("copying DefineMap excludes constructor", function() {
var AType = DefineMap.extend("AType", { aProp: {value: 1} });
var a = new AType();
var b = assign({}, a);
QUnit.notEqual(a.constructor, b.constructor, "Constructor prop not copied");
QUnit.equal(a.aProp, b.aProp, "Other values are unaffected");
});
QUnit.test("cloning from non-defined map excludes special keys on setup", function() {
var a = {
_data: {},
constructor: function() {},
_bindEvents: {},
_cid: "object0",
"foo": "bar"
};
var b = new DefineMap(a);
QUnit.notEqual(a.constructor, b.constructor, "Constructor prop not copied");
QUnit.notEqual(a._data, b._data, "_data prop not copied");
QUnit.notEqual(a._bindEvents, b._bindEvents, "_bindEvents prop not copied");
QUnit.notEqual(a._cid, b._cid, "_cid prop not copied");
QUnit.equal(a.foo, b.foo, "Other props copied");
});
QUnit.test("copying from .set() excludes special keys", function() {
var a = {
_data: {},
constructor: function() {},
_bindEvents: {},
_cid: "object0",
"foo": "bar",
"existing": "newVal"
};
var b = new DefineMap({
"existing": "oldVal"
});
b.set(a);
QUnit.notEqual(a.constructor, b.constructor, "Constructor prop not copied");
QUnit.notEqual(a._data, b._data, "_data prop not copied");
QUnit.notEqual(a._bindEvents, b._bindEvents, "_bindEvents prop not copied");
QUnit.notEqual(a._cid, b._cid, "_cid prop not copied");
QUnit.equal(a.foo, b.foo, "NEw props copied");
});
QUnit.test("copying with assign() excludes special keys", function() {
var a = {
_data: {},
constructor: function() {},
__bindEvents: {},
_cid: "object0",
"foo": "bar",
"existing": "newVal"
};
var b = new DefineMap({
"existing": "oldVal"
});
assign(b, a);
QUnit.notEqual(a.constructor, b.constructor, "Constructor prop not copied");
QUnit.notEqual(a._data, b._data, "_data prop not copied");
QUnit.notEqual(a.__bindEvents, b.__bindEvents, "_bindEvents prop not copied");
QUnit.notEqual(a._cid, b._cid, "_cid prop not copied");
QUnit.equal(a.foo, b.foo, "New props copied");
QUnit.equal(a.existing, b.existing, "Existing props copied");
});
QUnit.test("shorthand getter setter (#56)", function(){
var Person = DefineMap.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 Person({first: "Mohamed", last: "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('compute props can be set to null or undefined (#2372)', function() {
var VM = DefineMap.extend({
computeProp: {
type: 'compute'
}
});
var vmNull = new VM({computeProp: null});
QUnit.equal(vmNull.get('computeProp'), null, 'computeProp is null, no error thrown');
var vmUndef = new VM({computeProp: undefined});
QUnit.equal(vmUndef.get('computeProp'), undefined, 'computeProp is undefined, no error thrown');
});
QUnit.test("Inheriting DefineMap .set doesn't work if prop is on base map (#74)", function(){
var Base = DefineMap.extend({
baseProp: "string"
});
var Inheriting = Base.extend();
var inherting = new Inheriting();
inherting.set("baseProp", "value");
QUnit.equal(inherting.baseProp,"value", "set prop");
});
if(sealWorks && System.env.indexOf('production') < 0) {
QUnit.test("setting not defined property", function(){
var MyMap = DefineMap.extend({
prop: {}
});
var mymap = new MyMap();
try {
mymap.notdefined = "value";
ok(false, "no error");
} catch(e) {
ok(true, "error thrown");
}
});
}
QUnit.test(".extend errors when re-defining a property (#117)", function(){
var A = DefineMap.extend("A", {
foo: {
type: "string",
value: "blah"
}
});
A.extend("B", {
foo: {
type: "string",
value: "flub"
}
});
var C = DefineMap.extend("C", {
foo: {
get: function() {
return "blah";
}
}
});
C.extend("D", {
foo: {
get: function() {
return "flub";
}
}
});
QUnit.ok(true, "extended without errors");
});
QUnit.test(".value functions should not be observable", function(){
var outer = new DefineMap({
bam: "baz"
});
var ItemsVM = DefineMap.extend({
item: {
value: function(){
(function(){})(this.zed, outer.bam);
return new DefineMap({ foo: "bar" });
}
},
zed: "string"
});
var items = new ItemsVM();
var count = 0;
var itemsList = compute(function(){
count++;
return items.item;
});
itemsList.on('change', function() {});
items.item.foo = "changed";
items.zed = "changed";
equal(count, 1);
});
QUnit.test(".value values are overwritten by props in DefineMap construction", function() {
var Foo = DefineMap.extend({
bar: {
value: "baz"
}
});
var foo = new Foo({
bar: "quux"
});
equal(foo.bar, "quux", "Value set properly");
});
QUnit.test("Does not attempt to redefine _data if already defined", function() {
var Bar = DefineMap.extend({seal: false}, {
baz: { value : "thud" }
});
var baz = new Bar();
define(baz, {
quux: { value: "jeek" },
plonk: {
get: function() {
return "waldo";
}
}
}, baz._define);
QUnit.equal(baz.quux, "jeek", "New definitions successful");
QUnit.equal(baz.plonk, "waldo", "New computed definitions successful");
QUnit.equal(baz.baz, "thud", "Old definitions still available");
});