forked from VoliJS/NestedTypes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.all.js
More file actions
680 lines (560 loc) · 20.2 KB
/
test.all.js
File metadata and controls
680 lines (560 loc) · 20.2 KB
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
define( function( require, exports, module ){
var Nested = require( 'nestedtypes' );
var NestedModel = Nested.Model.extend({
defaults:{
time: Date,
text: '',
number: 1
},
url: '/',
save: function(){}
});
var MainModel = Nested.Model.extend({
defaults:{
first: NestedModel,
second: NestedModel,
count: 0,
sum: 0
}
});
describe( 'Extended defaults functionality', function(){
var M, M1;
it( 'create native properties for defaults', function( done ){
M = Nested.Model.extend({
defaults: {
a: 1,
b: 'ds'
},
some: 4
});
var m = new M();
m.a.should.eql( 1 );
m.b.should.eql( 'ds' );
m.some.should.eql( 4 );
m.once( 'change:b', function(){
m.b.should.eql( '5' );
done();
});
m.b = 5;
});
it( 'inherit defaults from the base class', function(){
M1 = M.extend({
defaults: {
d: 1,
a: 'e'
}
});
var m = new M1();
m.a.should.eql( 'e' );
m.d.should.eql( 1 );
m.b.should.eql( 'ds' );
m.some.should.eql( 4 );
});
});
describe( 'constructors in defaults', function(){
var user, User = Nested.Model.extend({
attributes:{
created: Date,
name: String,
loginCount: Integer
}
});
before( function(){
user = new User();
});
it( 'support Integer primitive type', function(){
user.loginCount = 0.5;
user.loginCount.should.eql( 1 );
});
it( 'create default values for constructor attributes', function(){
user.created.should.be.instanceof( Date );
user.name.should.eql( '' );
});
it( 'cast attribute values to defined types on assignment', function(){
var type;
user.loginCount = "5";
user.loginCount.should.be.number;
type = typeof user.loginCount;
type.should.eql( 'number' );
user.loginCount = "djkjkj";
user.loginCount.should.be.NaN;
// parse Date from string
user.created = "2012-12-12T10:00";
user.created.should.be.instanceof( Date );
user.created.toISOString().should.be.eql( '2012-12-12T10:00:00.000Z' );
// parse Date from timestamp
user.created = 1234567890123;
user.created.should.be.instanceof( Date );
user.created.toISOString().should.be.eql( '2009-02-13T23:31:30.123Z' );
user.name = 34;
user.name.should.be.string;
user.name.should.be.eql( '34' );
user.name = 'Joe';
type = typeof user.name;
type.should.eql( 'string' );
user.name.should.be.eql( 'Joe' );
});
describe( 'JSON', function(){
var comment, Comment = Nested.Model.extend({
defaults: {
created: Date,
author: User,
text: String
}
});
before( function(){
comment = new Comment({
created: '2012-11-10T13:14:15.123Z',
text: 'bla-bla-bla',
author: {
created: '2012-11-10T13:14:15.123Z',
name: 'you'
}
});
});
it( 'initialize model with JSON defaults ', function(){
var M = Nested.Model.extend({
defaults : {
a : [ 1, 2 ],
b : { a: 1, b : [ 2 ] },
f : Date.value( "2012-12-12T12:12" ),
g : Date.options({
value : "2012-12-12T12:12"
})
}
});
var m1 = new M(), m2 = new M();
m1.a.push( 3 );
m2.a.should.not.include( 3 );
m1.b.b.push( 3 );
m2.b.b.should.not.include( 3 );
m1.f.should.be.instanceOf( Date );
m1.g.should.be.instanceOf( Date );
m1.f.getTime().should.eql( 1355314320000 );
m1.g.getTime().should.eql( 1355314320000 );
});
it( 'create nested models from JSON', function(){
comment.created.should.eql( comment.author.created );
comment.created.should.be.instanceof( Date );
comment.author.created.should.be.instanceof( Date );
});
it( 'serialize nested models to JSON', function(){
var json = comment.toJSON();
json.created.should.be.string;
json.author.created.should.be.string;
json.created.should.eql( json.author.created );
});
});
});
describe( 'event bubbling', function(){
function shouldFireChangeOnce( model, attr, todo ){
var change = sinon.spy(),
attrs = attr.split( ' ' );
model.on( 'change', change );
var changeAttrs = _.map( attrs, function( name ){
var changeName = sinon.spy();
model.on( 'change:' + name, changeName );
return changeName;
});
todo( model );
change.should.be.calledOnce;
model.off( change );
_.each( changeAttrs, function( spy ){
spy.should.be.calledOnce;
model.off( spy );
});
}
describe( 'model with nested model', function(){
it( 'bubble single attribute local change event', function(){
shouldFireChangeOnce( new MainModel(), 'first', function( model ){
model.first.text = 'bubble';
});
});
it( "shouldn't bubble events if not required", function(){
var M = Nested.Model.extend({
defaults : {
first : MainModel,
second : MainModel.options({
triggerWhenChanged : false
})
}
});
shouldFireChangeOnce( new M(), 'first', function( model ){
model.first.first.text = "bubble";
model.second.first.text = 'not bubble';
});
});
it( 'emit single change event with local event handlers', function(){
shouldFireChangeOnce( new MainModel(), 'first', function( model ){
model.on( 'change:first', function(){
model.count += 1;
});
model.on( 'change:first', function(){
model.sum += 1;
});
model.first.text = 'bubble';
});
});
it( 'emit single change event in case of bulk change', function(){
shouldFireChangeOnce( new MainModel(), 'first second', function( model ){
model.set({
count: 1,
first: {
time: '2012-12-12 12:12',
text: 'hi'
},
second: {
time: '2012-12-12 12:12',
text: 'hi'
}
});
});
});
});
describe( 'model with nested collection', function(){
var Coll = Nested.Collection.extend({
model: NestedModel
});
var Compound = MainModel.extend({
defaults:{
items: Coll
}
});
var compd = new Compound();
it( 'trigger change when models are added to nested collection', function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.create({
time: "2012-12-12 12:12"
});
});
shouldFireChangeOnce( compd, 'items', function(){
compd.items.add([{
time: "2012-12-12 12:12"
},{
time: "2012-12-12 12:12"
}]);
});
});
it( 'trigger change when model is changed in nested collection', function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.first().text = 'Hi there!';
});
});
it( 'trigger change when models are removed from nested collection', function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.remove( compd.items.first() );
});
shouldFireChangeOnce( compd, 'items', function(){
compd.items.remove([ compd.items.first(), compd.items.last() ]);
});
});
it( "trigger change on nested collection's reset", function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.reset([{
id: 1,
time: "2012-12-12 12:12"
},{
id: 2,
time: "2012-12-12 12:13"
},{
id: 3,
time: "2012-12-12 12:14"
}]);
compd.items.length.should.eql( 3 );
});
});
it( 'trigger change when nested collection is sorted', function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.comparator = 'time';
compd.items.sort();
});
});
it( "trigger single change on nested collection's bulk change operation", function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.items.set([{
id: 3,
time: "2012-12-12 12:12"
},{
id: 4,
time: "2012-12-12 12:13"
},{
id: 5,
time: "2012-12-12 12:14"
}]);
compd.items.length.should.eql( 3 );
});
});
it( 'trigger single change in case of bulk update', function(){
shouldFireChangeOnce( compd, 'items', function(){
compd.set({
items:[{
id: 4,
time: "2012-12-12 12:12"
},{
id: 5,
time: "2012-12-12 12:13"
},{
id: 6,
time: "2012-12-12 12:14"
}],
first: {
text: 'Hi'
},
second: {
text: 'Lo'
}
});
});
});
});
});
describe( 'automatic event subscription', function(){
it( 'manage subscriptions automatically', function(){
var M = Nested.Model.extend({
defaults:{
left: NestedModel,
right: NestedModel
},
listening: {
left: {
'change:number' : function(){
}
},
right: {
'change:time change:text' : 'onRight'
}
},
onRight: function(){
}
});
});
});
describe( 'custom properties', function(){
it( 'generate read-only properties if function specified', function(){
var M = Nested.Model.extend({
properties: {
a: function(){ return 5; }
}
});
var m = new M();
m.a.should.eql( 5 );
});
it( 'generate custom properties if standard spec provided', function(){
var M = Nested.Model.extend({
state: 0,
properties: {
a: {
get: function(){ return this.state; },
set: function( x ){ this.state = x; return x; }
}
}
});
var m = new M();
m.a = 5;
m.a.should.eql( 5 );
m.state.should.eql( 5 );
});
it( 'override properties for defaults', function(){
var M = Nested.Model.extend({
defaults: {
a: 10
},
properties: {
a: function(){ return 5; }
}
});
var m = new M();
m.a.should.eql( 5 );
});
});
describe( 'pass options to nested', function(){
before( function(){
this.Book = Nested.Model.extend({
defaults: {
title: String,
published: Date
},
parse: function(){
this.parsed = true;
}
});
this.Books = Nested.Collection.extend({
model: this.Book
});
this.Author = Nested.Model.extend({
defaults: {
name: String,
biography: this.Book,
books: this.Books
}
});
});
it( 'model with nested model', function(){
var author = new this.Author({
name: 'Jules Verne',
biography: {
name: 'My Bio',
published: '1990-01-02 13:14:15'
}
}, {
parse: true
});
author.biography.parsed.should.be.true;
});
it( 'model with nested collection', function(){
var author = new this.Author({
name: 'Jules Verne',
books: [
{
name: 'Five Weeks in a Balloon',
published: '1990-01-02 13:14:15'
},
{
name: 'Around the Moon',
published: '1980-01-02 13:14:15'
}
]
}, {
parse: true
});
author.books.at(0).parsed.should.be.true;
author.books.at(1).parsed.should.be.true;
});
});
describe( 'Attributes definition options', function(){
it( 'must override native properties', function(){
var M = Nested.Model.extend({
attributes : {
attr : Number.value( 5 ).options({
set : function( value ){
return value * 2;
}
})
}
});
var m = new M();
m.attr.should.eql( 10 );
m.attr = 4;
m.attr.should.eql( 8 );
var M1 = Nested.Model.extend({
attributes : {
attr : Number.value( 5 ).options({
set : function( value ){
return value * 2;
},
get : function( value ){
return value + 1;
}
})
}
});
var m1 = new M1();
m1.attr.should.eql( 11 );
m1.attr = 4;
m1.attr.should.eql( 9 );
});
});
describe( 'Relations defined by model id references', function(){
var C = Nested.Collection.extend({
model : Nested.Model.extend({
defaults : {
name : String
}
})
});
var models = new C( _.map( [ 1, 2, 3 ], function( id ){
return {
id : id,
name : id
};
}));
describe( 'Model.From', function(){
var M = Nested.Model.extend({
defaults : {
ref : Nested.Model.From( models )
}
});
var m;
it( 'should parse references', function(){
m = new M({ id : 1, ref : 1 }, { parse: true });
m.ref.name.should.eql( '1' );
});
it( 'should accept assignments with models', function(){
m.ref = models.get( 2 );
m.ref.name.should.eql( '2' );
});
it( 'should be serialized to id', function(){
var json = m.toJSON();
json.ref.should.eql( 2 );
});
});
describe( 'Collection.SubsetOf', function(){
var M = Nested.Model.extend({
defaults : {
refs : Nested.Collection.SubsetOf( models )
}
});
var m;
it( 'should parse references', function(){
m = new M({ id : 1, refs : [ 1, 2 ]}, { parse: true });
m.refs.get( 1 ).name.should.eql( '1' );
m.refs.get( 2 ).name.should.eql( '2' );
});
it( 'should accept assignments with models', function(){
m.refs = [ models.get( 2 ) ];
m.refs.first().name.should.eql( '2' );
});
it( 'should be serialized to id', function(){
var json = m.toJSON();
json.refs.should.eql( [ 2 ] );
});
});
describe( 'Model.Collection type', function(){
var M, M2, M3;
it( 'is defined for base Model type', function(){
Nested.Model.Collection.should.eql( Nested.Collection );
});
it( 'is generated for Model subclasses', function(){
M = Nested.Model.extend({
urlBase : '',
save : function(){},
defaults : {
a : 1,
b : 2
}
});
var c = new M.Collection();
c.create({ a: 7 });
c.first().b.should.eql( 2 );
});
it( 'can be customized by defining Model.collection property', function(){
M2 = M.extend({
defaults : {
c : 3
},
collection : {
something : 'useless',
initialize : function(){
this.create({ c : 0 });
}
}
});
var c = new M2.Collection();
c.something.should.eql( 'useless' );
c.first().a.should.eql( 1 );
c.first().c.should.eql( 0 );
});
it( 'is inherited from base Model.Collection', function(){
M3 = M2.extend({
defaults : {
d : 8
}
});
var c = new M3.Collection();
c.something.should.eql( 'useless' );
c.first().a.should.eql( 1 );
c.first().d.should.eql( 8 );
});
});
});
});