/
full.js
1815 lines (1587 loc) · 46.6 KB
/
full.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
var tape = require('tape');
var State = require('../ampersand-state');
var AmpersandRegistry = require('ampersand-registry');
var Collection = require('ampersand-collection');
var definition, Foo, registry;
// wrap test so we always run reset first
var test = function () {
reset();
tape.apply(tape, arguments);
};
test.only = function () {
reset();
tape.only.apply(tape, arguments);
};
function reset() {
registry = new AmpersandRegistry();
definition = {
type: 'foo',
props: {
id: 'number',
firstName: ['string', true, 'defaults'],
lastName: ['string', true],
thing: {
type: 'string',
required: true,
default: 'hi'
},
num: ['number', true],
today: ['date'],
hash: ['object'],
list: ['array'],
myBool: ['boolean', true, false],
someNumber: {type: 'number', allowNull: true},
someNull: {type: 'object', default: null},
good: {
type: 'string',
test: function (newVal) {
if (newVal !== 'good') {
return 'Value not good';
}
}
}
},
session: {
active: ['boolean', true, true]
},
derived: {
name: {
deps: ['firstName', 'lastName'],
fn: function () {
return this.firstName + ' ' + this.lastName;
}
},
initials: {
deps: ['firstName', 'lastName'],
cache: false,
fn: function () {
// This currently breaks without both deps being set
if (this.firstName && this.lastName) {
return (this.firstName.charAt(0) + this.lastName.charAt(0)).toUpperCase();
}
return '';
}
},
isCrazy: {
deps: ['crazyPerson'],
fn: function () {
return !!this.crazyPerson;
}
}
},
// add a reference to the registry
registry: registry
};
Foo = State.extend(definition);
}
test('should get the derived value', function (t) {
var foo = new Foo({
firstName: 'jim',
lastName: 'tom'
});
foo.firstName = 'jim';
foo.lastName = 'tom';
t.strictEqual(foo.name, 'jim tom');
t.strictEqual(foo.initials, 'JT');
t.end();
});
test('should have default values for properties', function (t) {
var foo = new Foo({
firstName: 'jim',
lastName: 'tom'
});
t.strictEqual(foo.myBool, false);
t.strictEqual(foo.someNull, null);
t.end();
});
test('should have default array/object properties', function (t) {
var Bar = State.extend({
props: {
list: ['array', true],
hash: ['object', true]
}
});
var bar = new Bar();
var otherBar = new Bar();
t.ok(bar.list !== undefined);
t.ok(bar.hash !== undefined);
//Should create unique instances of the defaults
otherBar.list.push('foo');
otherBar.hash.foo = 'bar';
t.ok(bar.list.length === 0);
t.ok(bar.hash.foo === undefined);
t.end();
});
test('should throw a useful error setting a default value to an array', function (t) {
t.plan(2);
try {
State.extend({
props: { list: ['array', true, []] }
});
} catch (err) {
t.ok(err instanceof TypeError);
t.ok(err.message.match(/value for list cannot be an object\/array/));
}
});
test('should throw a useful error setting a default value to an object', function (t) {
t.plan(2);
try {
State.extend({
props: { list: ['array', true, []] }
});
} catch (err) {
t.ok(err instanceof TypeError);
t.ok(err.message.match(/value for list cannot be an object\/array/));
}
});
test('a default should be settable as a function which returns a value', function (t) {
var Foo = State.extend({
props: {
anObject: ['object', true, function () { return {foo: 'bar'}; }]
}
});
var foo = new Foo();
t.deepEqual(foo.anObject, {foo: 'bar'});
t.end();
});
test('should throw an error setting a derived prop', function (t) {
t.plan(1);
var foo = new Foo();
try { foo.name = 'bob'; }
catch (err) { t.ok(err instanceof TypeError); }
});
test('Error when setting derived property should be helpful', function (t) {
var foo = new Foo();
try { foo.name = 'bob'; }
catch (err) {
t.equal(err.message, '`name` is a derived property, it can\'t be set directly.');
}
t.end();
});
test('should get correct defaults', function (t) {
var foo = new Foo({});
t.strictEqual(foo.firstName, 'defaults');
t.strictEqual(foo.thing, 'hi');
t.end();
});
test('Setting other properties when `extraProperties: "reject"` throws error', function (t) {
var Foo = State.extend({
extraProperties: 'reject'
});
var foo = new Foo();
t.throws(function () {
foo.set({
craziness: 'new'
});
}, Error, 'Throws exception if set to reject');
t.end();
});
test('Setting other properties ignores them by default', function (t) {
var foo = new Foo();
foo.set({
craziness: 'new'
});
t.strictEqual(foo.craziness, undefined, 'property should be ignored');
t.end();
});
test('Setting other properties is ok if extraProperties = "allow"', function (t) {
var foo = new Foo();
foo.extraProperties = 'allow';
foo.set({
craziness: 'new'
});
t.equal(foo.get('craziness'), 'new');
t.end();
});
test('#11 - multiple instances of the same state class should be able to use extraProperties = "allow" as expected', function (t) {
var Foo = State.extend({
extraProperties: 'allow'
});
var one = new Foo({ a: 'one.a', b: 'one.b' });
var two = new Foo({ a: 'two.a', b: 'two.b', c: 'two.c' });
t.equal(one.a, 'one.a');
t.equal(one.b, 'one.b');
t.equal(two.a, 'two.a');
t.equal(two.b, 'two.b');
t.equal(two.c, 'two.c');
t.end();
});
test('extraProperties = "allow" properties should be defined entirely on the instance not the prototype', function (t) {
var Foo = State.extend({
extraProperties: 'allow'
});
var two = new Foo();
t.deepEqual(two._definition, {});
t.end();
});
test('should throw a type error for bad data types', function (t) {
t.throws(function () {
new Foo({firstName: 3});
}, TypeError);
t.throws(function () {
new Foo({num: 'foo'});
}, TypeError);
t.throws(function () {
new Foo({hash: 10});
}, TypeError);
t.throws(function () {
new Foo({today: 'asdfadsfa'});
}, TypeError);
t.doesNotThrow(function () {
new Foo({today: 1397631169892});
new Foo({today: '1397631169892'});
new Foo({today: '2014-11-13'});
new Foo({today: '2014-11-13T21:01Z'});
new Foo({today: '2014-11-13T21:01:28.752Z'});
});
t.throws(function () {
new Foo({list: 10});
}, TypeError);
t.end();
});
test('should validate model', function (t) {
var foo = new Foo();
t.equal(foo._verifyRequired(), false);
foo.firstName = 'a';
foo.lastName = 'b';
foo.thing = 'abc';
foo.num = 12;
t.ok(foo._verifyRequired());
t.end();
});
test('should store previous attributes', function (t) {
var foo = new Foo({
firstName: 'beau'
});
foo.firstName = 'john';
t.strictEqual(foo.firstName, 'john');
t.strictEqual(foo.previous('firstName'), 'beau');
foo.firstName = 'blah';
t.strictEqual(foo.previous('firstName'), 'john');
t.end();
});
test('should have data serialization methods', function (t) {
var foo = new Foo({
firstName: 'bob',
lastName: 'tom',
thing: 'abc'
});
t.deepEqual(foo.attributes, {
firstName: 'bob',
lastName: 'tom',
thing: 'abc',
myBool: false,
active: true,
someNull: null
});
t.deepEqual(foo.serialize(), {
firstName: 'bob',
lastName: 'tom',
thing: 'abc',
myBool: false,
someNull: null
});
t.end();
});
test('serialize should not include session properties no matter how they\'re defined.', function (t) {
var Foo = State.extend({
props: {
name: 'string'
},
session: {
// simple definition
active: 'boolean'
}
});
var Bar = State.extend({
props: {
name: 'string'
},
session: {
// fuller definition
active: ['boolean', true, false]
}
});
var foo = new Foo({name: 'hi', active: true});
var bar = new Bar({name: 'hi', active: true});
t.deepEqual(foo.serialize(), {name: 'hi'});
t.deepEqual(bar.serialize(), {name: 'hi'});
t.end();
});
test('serialize should serialize props/derived/session on request', function (t) {
var Foo = State.extend({
props: {
name: 'string'
},
derived: {
derivedAtrr: {
fn: function() {
return 'derived-attr';
}
}
},
session: {
active: 'boolean'
}
});
var foo = new Foo({name: 'hi', active: true});
t.deepEqual(foo.serialize({session: true}), {name: 'hi', active: true}, 'serializes session on request');
t.deepEqual(foo.serialize({derived: true}), {name: 'hi', derivedAtrr: 'derived-attr'}, 'serializes derived on request');
t.deepEqual(foo.serialize({props: false, session: true}), {active: true}, 'serialize ignores props on request');
t.end();
});
test('should fire events normally for properties defined on the fly', function (t) {
var foo = new Foo();
foo.extraProperties = 'allow';
foo.on('change:crazyPerson', function () {
t.ok(true);
});
foo.set({
crazyPerson: true
});
t.end();
});
test('should fire event on derived properties, even if dependent on ad hoc prop.', function (t) {
var Foo = State.extend({
extraProperties: 'allow',
derived: {
isCrazy: {
deps: ['crazyPerson'],
fn: function () {
return !!this.crazyPerson;
}
}
}
});
var foo = new Foo();
foo.on('change:isCrazy', function () {
t.ok(true);
});
foo.set({
crazyPerson: true
});
t.end();
});
test('throw error on invalid extraProperties', function (t) {
var Foo = State.extend({
extraProperties: 'qwijbo'
});
var foo = new Foo();
t.throws(function () {
foo.set('a', 'b');
}, TypeError, 'Throws TypeError on invalid extraProperties');
t.end();
});
test('should fire general change event on single attribute', function (t) {
var foo = new Foo({firstName: 'coffee'});
foo.on('change', function () {
t.ok(true);
});
foo.firstName = 'bob';
t.end();
});
test('should fire single change event for multiple attribute set', function (t) {
var foo = new Foo({firstName: 'coffee'});
foo.on('change', function () {
t.ok(true);
});
foo.set({
firstName: 'roger',
lastName: 'smells'
});
t.end();
});
test('derived properties', function (t) {
var ran = 0;
var notCachedRan = 0;
var Foo = State.extend({
props: {
name: ['string', true]
},
derived: {
greeting: {
deps: ['name'],
fn: function () {
ran++;
return 'hi, ' + this.name;
}
},
notCached: {
cache: false,
deps: ['name'],
fn: function () {
notCachedRan++;
return 'hi, ' + this.name;
}
}
}
});
var foo = new Foo({name: 'henrik'});
t.strictEqual(ran, 0, 'derived function should not have run yet.');
t.equal(foo.greeting, 'hi, henrik');
t.equal(foo.greeting, 'hi, henrik');
t.equal(ran, 1, 'cached derived should only run once');
t.equal(notCachedRan, 0, 'should not have been run yet');
foo.name = 'someone';
t.equal(foo.greeting, 'hi, someone');
t.equal(foo.greeting, 'hi, someone');
t.equal(ran, 2, 'cached derived should have been cleared and run once again');
t.equal(notCachedRan, 1, 'should have been run once because it was triggered');
t.equal(foo.notCached, 'hi, someone');
t.equal(notCachedRan, 2, 'incremented again');
t.equal(foo.notCached, 'hi, someone');
t.equal(notCachedRan, 3, 'incremented each time');
t.end();
});
test('cached, derived properties should only fire change event if they\'ve actually changed', function (t) {
var changed = 0;
var Foo = State.extend({
props: {
name: ['string', true],
other: 'string'
},
derived: {
greeting: {
deps: ['name', 'other'],
fn: function () {
return 'hi, ' + this.name;
}
}
}
});
var foo = new Foo({name: 'henrik'});
foo.on('change:greeting', function () {
changed++;
});
t.equal(changed, 0);
foo.name = 'new';
t.equal(changed, 1);
foo.other = 'new';
t.equal(changed, 1);
t.end();
});
test('derived properties with derived dependencies', function (t) {
var ran = 0;
var Foo = State.extend({
props: {
name: ['string', true]
},
derived: {
greeting: {
deps: ['name'],
fn: function () {
return 'hi, ' + this.name;
}
},
awesomeGreeting: {
deps: ['greeting'],
fn: function () {
return this.greeting + '!';
}
}
}
});
var foo = new Foo({name: 'henrik'});
foo.on('change:awesomeGreeting', function () {
ran++;
t.ok(true, 'should fire derived event');
});
foo.on('change:greeting', function () {
ran++;
t.ok(true, 'should fire derived event');
});
foo.on('change:name', function () {
ran++;
t.ok(true, 'should fire derived event');
});
foo.on('change', function () {
ran++;
t.ok(true, 'should file main event');
});
foo.name = 'something';
t.equal(ran, 4);
t.end();
});
test('derived properties triggered with multiple instances', function (t) {
var foo = new Foo({firstName: 'Silly', lastName: 'Fool'});
var bar = new Foo({firstName: 'Bar', lastName: 'Man'});
foo.on('change:name', function () {
t.ok('name changed');
});
foo.firstName = 'bob';
bar.on('change:name', function () {
t.ok('name changed');
});
bar.firstName = 'bob too';
t.end();
});
test('Calling `previous` during change of derived cached property should work', function (t) {
var foo = new Foo({firstName: 'Henrik', lastName: 'Joreteg'});
var ran = false;
foo.on('change:name', function () {
if (!ran) {
t.equal(typeof foo.previous('name'), 'undefined');
ran = true;
} else {
t.equal(foo.previous('name'), 'Crazy Joreteg');
}
});
foo.firstName = 'Crazy';
foo.firstName = 'Lance!';
t.end();
});
test('Calling `previous` during change of derived property that is not cached, should be `undefined`', function (t) {
var foo = new Foo({firstName: 'Henrik', lastName: 'Joreteg'});
// the initials property is explicitly not cached
// so you should not be able to get a previous value
// for it.
foo.on('change:initials', function () {
t.equal(typeof foo.previous('initials'), 'undefined');
});
foo.firstName = 'Crazy';
t.end();
});
test('Should be able to define and use custom data types', function (t) {
var Foo = State.extend({
props: {
silliness: 'crazyType'
},
dataTypes: {
crazyType: {
set: function (newVal) {
return {
val: newVal,
type: 'crazyType'
};
},
get: function (val) {
return val + 'crazy!';
}
}
}
});
var foo = new Foo({silliness: 'you '});
t.equal(foo.silliness, 'you crazy!');
t.end();
});
tape.skip('Throw typeError for invalid data types', function (t) {
t.throws(function () {
State.extend({
props: {
weight: 'randomType'
}
});
});
t.end();
});
test('Uses dataType compare', function (t) {
var compareRun;
var Foo = State.extend({
props: {
silliness: 'crazyType'
},
dataTypes: {
crazyType: {
compare: function () {
compareRun = true;
return false;
},
set: function (newVal) {
return {
val: newVal,
type: 'crazyType'
};
},
get: function (val) {
return val + 'crazy!';
}
}
}
});
compareRun = false;
var foo = new Foo({ silliness: 'you' });
foo.silliness = 'they';
t.assert(compareRun);
t.end();
});
test('Should only allow nulls where specified', function (t) {
var foo = new Foo({
firstName: 'bob',
lastName: 'vila',
someNumber: null
});
t.equal(foo.someNumber, null);
t.throws(function () {
foo.firstName = null;
}, TypeError, 'Throws exception when setting disallowed null');
t.end();
});
test('Attribute test function works', function (t) {
var foo = new Foo({good: 'good'});
t.equal(foo.good, 'good');
t.throws(function () {
foo.good = 'bad';
}, TypeError, 'Throws exception on invalid attribute value');
t.end();
});
test('Values attribute basic functionality', function (t) {
var Model = State.extend({
props: {
state: {
type: 'string',
values: ['CA', 'WA', 'NV']
}
}
});
var m = new Model();
t.throws(function () {
m.state = 'PR';
}, TypeError, 'Throws exception when setting something not in list');
t.equal(m.state, undefined, 'Should be undefined if no default');
m.state = 'CA';
t.equal(m.state, 'CA', 'State should be set');
t.end();
});
test('Values attribute default works and is called only once', function (t) {
var ran = 0;
var Model = State.extend({
dataTypes: {
countryType: {
default: 'Atlantis'
}
},
props: {
country: {
type: 'countryType',
required: true
},
state: {
type: 'string',
values: ['CA', 'WA', 'NV'],
default: function(){
ran++;
return 'CA';
}
}
}
});
var m = new Model();
t.equal(m.state, 'CA', 'Should have applied the default');
t.equal(ran, 1, 'Should have been invoked only once');
t.equal(m.state, 'CA', 'Should have returned the same object');
t.equal(ran, 1, 'Should have been invoked only once');
t.equal(m.country, 'Atlantis');
t.throws(function () {
m.state = 'PR';
}, TypeError, 'Throws exception when setting something not in list');
t.end();
});
test('toggle() works on boolean and values properties.', function (t) {
var Model = State.extend({
props: {
isAwesome: 'boolean',
someNumber: 'number',
state: {
type: 'string',
values: ['CA', 'WA', 'NV'],
default: 'CA'
}
}
});
var m = new Model();
t.throws(function () {
m.toggle('someNumber');
}, TypeError, 'Throws exception when toggling a non-toggleable property.');
m.toggle('state');
t.equal(m.state, 'WA', 'Should go to next');
m.toggle('state');
t.equal(m.state, 'NV', 'Should go to next');
m.toggle('state');
t.equal(m.state, 'CA', 'Should go to next with loop');
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, true, 'Should toggle even if undefined');
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, false, 'Should toggle if true.');
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, true, 'Should toggle if false.');
t.end();
});
test('property test function scope is correct.', function (t) {
var m;
var temp;
var Model = State.extend({
props: {
truth: {
type: 'boolean',
test: function () {
temp = this;
return false;
}
}
}
});
m = new Model();
m.toggle('truth');
t.equal(m, temp);
t.end();
});
test('should be able to inherit for use in other objects', function (t) {
var StateObj = State.extend({
props: {
name: 'string'
}
});
function AwesomeThing() {
StateObj.apply(this, arguments);
}
AwesomeThing.prototype = Object.create(StateObj.prototype);
AwesomeThing.prototype.hello = function () {
return this.name;
};
var awe = new AwesomeThing({name: 'cool'});
t.equal(awe.hello(), 'cool');
t.equal(awe.name, 'cool');
t.end();
});
test('extended state objects should maintain child collections of parents', function (t) {
var State1 = State.extend({
collections: {
myStuff: Collection
}
});
var State2 = State1.extend({
collections: {
myOtherCollection: Collection
}
});
var thing = new State2();
t.ok(thing.myStuff);
t.ok(thing.myOtherCollection);
t.end();
});
test('`initialize` should have access to initialized child collections', function (t) {
var StateObj = State.extend({
initialize: function () {
t.ok(this.myStuff);
t.equal(this.myStuff.parent, this);
t.end();
},
collections: {
myStuff: Collection
}
});
new StateObj();
});
test('parent collection references should be maintained when adding/removing to a collection', function (t) {
var StateObj = State.extend({
props: {
id: 'string'
}
});
var c = new Collection();
var s = new StateObj({id: '47'});
c.add(s);
t.equal(s.collection, c);
c.remove(s);
t.notOk(s.collection);
t.end();
});
test('children and collections should be instantiated', function (t) {
var GrandChild = State.extend({
props: {
id: 'string'
},
collections: {
nicknames: Collection
}
});
var FirstChild = State.extend({
props: {
id: 'string'
},
children: {
grandChild: GrandChild
}
});
var StateObj = State.extend({
props: {
id: 'string'
},
children: {
firstChild: FirstChild
}
});
var data = {
id: 'child',
firstChild: {
id: 'child',
grandChild: {
id: 'grandChild',
nicknames: [
{name: 'munchkin'},
{name: 'kiddo'}
]
}
}
};
var first = new StateObj(data);
t.ok(first.firstChild, 'child should be initted');
t.ok(first.firstChild.grandChild, 'grand child should be initted');
t.equal(first.firstChild.id, 'child');
t.equal(first.firstChild.grandChild.id, 'grandChild');
t.ok(first.firstChild.grandChild.nicknames instanceof Collection, 'should be collection');
t.equal(first.firstChild.grandChild.nicknames.length, 2);
t.deepEqual(first.serialize(), {
id: 'child',
firstChild: {
id: 'child',
grandChild: {
id: 'grandChild',
nicknames: [
{name: 'munchkin'},
{name: 'kiddo'}
]
}
}
});
t.equal(JSON.stringify(first), JSON.stringify({
id: 'child',
firstChild: {
id: 'child',
grandChild: {
id: 'grandChild',
nicknames: [
{name: 'munchkin'},
{name: 'kiddo'}
]
}
}
}), 'should be able to pass whole object to JSON.stringify()');
// using `set` should still apply to children
first.set({
firstChild: {
id: 'firstChild',
grandChild: {
nicknames: [{name: 'runt'}]
}
}
});
t.ok(first.firstChild instanceof FirstChild, 'should still be instanceof');
t.equal(first.firstChild.id, 'firstChild', 'change should have been applied');
t.equal(first.firstChild.grandChild.nicknames.length, 3, 'collection should have been updated');
t.end();
});
test('issue #82, child collections should not be cleared if they add data to themselves when instantiated', function (t) {
var Widget = State.extend({
props: {
title: 'string'
}
});
var Widgets = Collection.extend({
initialize: function () {
// some collections read from data they have immediate access to
// like localstorage, or whatnot. This should not be wiped out
// when instantiated by parent.
this.add([{title: 'hi'}]);
},
model: Widget
});
var Parent = State.extend({
collections: {
widgets: Widgets
}
});
var parent = new Parent();
t.equal(parent.widgets.length, 1, 'should contain data added by initialize method of child collection');
t.end();
});