-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstructure_test.js
1541 lines (1259 loc) · 46.9 KB
/
structure_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
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 chai = require('chai');
var expect = chai.expect;
chai.should();
var Immutable = require('immutable');
var Structure = require('../src/structure');
describe('structure', function () {
describe('api', function () {
it('should be new safe to avoid mishaps of forgetting new keyword', function () {
var structure = Structure({
data: { 'foo': 'hello' }
});
structure.should.be.instanceof(Structure);
});
it('should expose immutable.js cursor', function () {
var structure = new Structure({
data: { 'foo': 'hello' }
});
var cursor = structure.cursor(['foo']);
cursor.deref().should.equal('hello');
cursor = cursor.update(function () {
return 'bar';
});
cursor.deref().should.equal('bar');
});
it('should allow values as key paths', function () {
var s = new Structure({ data: {'': 3, a: {'': 5}} });
s.cursor().cursor('').deref().should.equal(3);
s.cursor('').deref().should.equal(3);
var r = s.reference('a');
r.cursor().cursor('').deref().should.equal(5);
r.cursor('').deref().should.equal(5);
});
});
describe('existing immutable structure', function () {
it('should accept existing immutable maps', function () {
var immutableObj = Immutable.fromJS({
foo: 'hello'
});
var structure = new Structure({
data: immutableObj
});
var cursor = structure.cursor(['foo']);
cursor.deref().should.equal('hello');
cursor = cursor.update(function () {
return 'bar';
});
cursor.deref().should.equal('bar');
});
it('should accept existing immutable list', function () {
var immutableObj = Immutable.List.of('hello');
var structure = new Structure({
data: immutableObj
});
var cursor = structure.cursor(['0']);
cursor.deref().should.equal('hello');
cursor = cursor.update(function () {
return 'bar';
});
cursor.deref().should.equal('bar');
});
});
describe('events', function () {
it('should trigger swap when structure is changed with new and old data', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
structure.on('swap', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo']);
newData.toJS().should.eql({'foo': 'bar'});
oldData.toJS().should.eql({'foo': 'hello'});
structure.cursor().toJS().should.eql({ 'foo': 'bar' });
done();
});
structure.cursor(['foo']).update(function () {
return 'bar';
});
});
it('should trigger swap when structure is changed with new and old data on nested cursors', function (done) {
var structure = new Structure({
data: { 'foo': { 'bar': 'hello' } }
});
structure.on('swap', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo', 'bar']);
newData.toJS().should.eql({ 'foo': { 'bar': 'bye' } });
oldData.toJS().should.eql({ 'foo': { 'bar': 'hello' } });
done();
});
structure.cursor(['foo']).cursor(['bar']).update(function () {
return 'bye';
});
});
it('should trigger swap with keyPath', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
structure.on('swap', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo']);
done();
});
structure.cursor(['foo']).update(function () {
return 'bar';
});
});
it('should trigger swap with keyPath on forceHasSwapped', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
structure.on('swap', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo']);
done();
});
structure.forceHasSwapped(structure.current, structure.current, ['foo'])
});
it('should not emit events nor affect history when updating the structure does not actually change anything', function () {
var calls = 0;
var structure = new Structure({
data: { foo: {}, bar: 42 },
history: true
});
var original = structure.current;
structure.on('swap', function() {
calls++;
});
structure.on('change', function() {
calls++;
});
var cursor = structure.cursor();
var cursorBar = structure.cursor('bar');
cursor.set('foo', Immutable.Map());
calls.should.equal(0);
cursorBar.update(function() {
return 42;
});
calls.should.equal(0);
original.should.equal(structure.current);
// Ensure history is not affected
structure.history.size.should.equal(1);
});
it('should trigger any event on any type of add, delete and change', function (done) {
var structure = new Structure({
data: { }
});
var numberOfCalls = 0;
structure.on('any', function (newData, oldData, keyPath) {
numberOfCalls++;
if (numberOfCalls === 6) {
done();
}
});
structure.on('add', function (newData, keyPath) {
numberOfCalls++;
keyPath.should.eql(['foo']);
newData.should.eql('baz');
});
structure.cursor(['foo']).update(function (state) {
return 'baz';
});
structure.on('change', function (newData, oldData, keyPath) {
numberOfCalls++;
keyPath.should.eql(['foo']);
newData.should.equal('updated');
oldData.should.equal('baz');
});
structure.cursor('foo').update(function () { return 'updated'; });
structure.on('delete', function (oldData, keyPath) {
numberOfCalls++;
keyPath.should.eql(['foo']);
oldData.should.eql('updated');
if (numberOfCalls === 6) {
done();
}
});
structure.cursor().remove('foo');
});
it('should be able to force trigger swap', function (done) {
var structure = new Structure();
structure.on('swap', function () {
done();
});
structure.forceHasSwapped();
});
it('should trigger change with data when existing property is changed', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
structure.on('change', function (newValue, oldValue, path) {
path.should.eql(['foo']);
oldValue.should.equal('hello');
newValue.should.equal('bar');
structure.current.toJS().should.eql({
foo: 'bar'
});
done();
});
structure.cursor(['foo']).update(function () {
return 'bar';
});
});
it('should trigger change with data when existing property is changed to falsey value', function (done) {
var structure = new Structure({
data: { 'foo': true }
});
var i = 0;
structure.on('change', function (newValue, oldValue, path) {
path.should.eql(['foo']);
switch(i) {
case 0:
oldValue.should.equal(true);
expect(newValue).to.be.undefined;
break;
case 1:
expect(oldValue).to.be.undefined;
expect(newValue).to.be.false;
break;
case 2:
expect(oldValue).to.be.false;
expect(newValue).to.be.null;
done();
break;
}
i++;
});
structure.cursor(['foo']).update(function () {
return void 0;
});
structure.cursor(['foo']).update(function () {
return false;
});
structure.cursor(['foo']).update(function () {
return null;
});
});
it('should trigger add with data when a new property is added', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
structure.on('add', function (newValue, path) {
path.should.eql(['bar']);
newValue.should.equal('baz');
structure.current.toJS().should.eql({
foo: 'hello',
bar: 'baz'
});
done();
});
structure.cursor(['bar']).update(function (state) {
return 'baz';
});
});
it('should trigger add with data when a new property added is a falsey value', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
var i = 1;
structure.on('add', function (newValue, path) {
path.should.eql([i+'']);
switch(i) {
case 1:
expect(newValue).to.be.false;
break;
case 2:
expect(newValue).to.be.null;
break;
case 3:
expect(newValue).to.be.undefined;
done();
break;
}
i++;
});
structure.cursor(['1']).update(function () {
return false;
});
structure.cursor(['2']).update(function () {
return null;
});
structure.cursor().set('3', void 0);
});
it('should trigger delete with data when existing property is removed', function (done) {
var structure = new Structure({
data: { 'foo': 'hello', 'bar': 'world' }
});
structure.on('delete', function (oldValue, path) {
path.should.eql(['foo']);
oldValue.should.equal('hello');
structure.cursor().toJS().should.eql({ 'bar': 'world' });
done();
});
structure.cursor().remove('foo');
});
describe('concurrency', function () {
it('should set correct structure when modifying it during a swap event', function (done) {
var structure = new Structure({
data: { 'foo': 'hello' }
});
var i = 0;
structure.on('swap', function (newData, oldData) {
i++;
if(i == 1) {
newData.toJS().should.eql({ 'foo': 'bar' });
oldData.toJS().should.eql({ 'foo': 'hello' });
structure.cursor().toJS().should.eql({ 'foo': 'bar' });
}
if(i == 2) {
newData.toJS().should.eql({ 'foo': 'bar', 'bar': 'world' });
oldData.toJS().should.eql({ 'foo': 'bar' });
structure.cursor().toJS().should.eql({'foo': 'bar', 'bar': 'world'});
done();
}
});
structure.once('swap', function (newData, oldData) {
structure.cursor('bar').update(function() {
return 'world';
});
});
structure.cursor(['foo']).update(function () {
return 'bar';
});
});
it('should set correct structure when modifying it during a change event', function (done) {
var structure = new Structure({
data: { 'subtree': {} }
});
var i = 0;
structure.on('swap', function (newData, oldData) {
i++;
if(i == 1) {
newData.toJS().should.eql({ subtree: { foo: 'bar' } });
oldData.toJS().should.eql({ subtree: {} });
}
if(i == 2) {
newData.toJS().should.eql({ subtree: { foo: 'bar', hello: 'world' } });
oldData.toJS().should.eql({ subtree: { foo: 'bar' } });
structure.cursor().toJS().should.eql({ subtree: { foo: 'bar', hello: 'world' } });
done();
}
});
structure.once('change', function (newValue, oldValue, path) {
path.should.eql(['subtree']);
newValue.toJS().should.eql({ foo: 'bar' });
oldValue.toJS().should.eql({});
structure.cursor('subtree').update('hello',function() {
return 'world';
});
});
structure.cursor().update('subtree', function () {
return Immutable.fromJS({
foo: 'bar'
});
});
});
it('should avoid stale cursor changed existing property', function() {
var struct = new Structure({
data: { foo: 42, bar: 24 }
});
var foo = struct.cursor('foo');
var bar = struct.cursor('bar');
var current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: 42, bar: 24 });
current.should.equal(oldRoot);
foo._rootData.should.equal(current);
newRoot.toJS().should.eql({ foo: 43, bar: 24 });
struct.current.toJS().should.eql(newRoot.toJS());
});
foo.update(function(val) {
val.should.equal(42);
return val + 1;
});
struct.current.toJS().should.eql({ foo: 43, bar: 24 });
current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: 43, bar: 24 });
current.toJS().should.not.eql(newRoot.toJS());
newRoot.toJS().should.not.eql(oldRoot.toJS());
newRoot.toJS().should.eql({ foo: 43, bar: 25 });
});
bar.update(function(val) {
val.should.equal(24);
return val + 1;
});
struct.current.toJS().should.eql({ foo: 43, bar: 25 });
});
it('should set correct structure when modifying it during an add event', function (done) {
var structure = new Structure({
data: { }
});
var i = 0;
structure.on('swap', function (newData, oldData) {
i++;
if(i == 1) {
newData.toJS().should.eql({ subtree: { foo: 'bar' } });
oldData.toJS().should.eql({});
structure.cursor().toJS().should.eql({ subtree: { foo: 'bar' } });
}
if(i == 2) {
structure.cursor().toJS().should.eql({ subtree: { foo: 'bar', hello: 'world' } });
done();
}
});
structure.once('add', function (newValue, path) {
path.should.eql(['subtree']);
newValue.toJS().should.eql({ foo: 'bar' });
structure.cursor('subtree').update('hello',function() {
return 'world';
});
});
structure.cursor().update('subtree', function () {
return Immutable.fromJS({
foo: 'bar'
});
});
});
it('should avoid stale cursor when adding a new property', function() {
var struct = new Structure({
data: { foo: {}, bar: {} }
});
var foo = struct.cursor('foo');
var bar = struct.cursor('bar');
var current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: {}, bar: {} });
current.should.equal(oldRoot);
foo._rootData.should.equal(current);
newRoot.toJS().should.eql({ foo: { a: 42 }, bar: {} });
struct.current.toJS().should.eql(newRoot.toJS());
});
foo.update('a', function() {
return 42;
});
struct.current.toJS().should.eql({ foo: {a: 42}, bar: {} });
current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: {a: 42}, bar: {} });
current.toJS().should.eql(oldRoot.toJS());
current.toJS().should.not.eql(newRoot.toJS());
newRoot.toJS().should.not.eql(oldRoot.toJS());
newRoot.toJS().should.eql({ foo: {a: 42}, bar: { b: "hello" } });
});
// This test case demonstrates the distinction between
// .setIn(path, newRoot.getIn(path)) and
// .updateIn(path, () => newRoot.getIn(path))
bar.set('b', "hello");
struct.current.toJS().should.eql({ foo: {a: 42}, bar: { b: "hello" } });
});
it('should set correct structure when modifying it during a delete event', function (done) {
var structure = new Structure({
data: { 'subtree': {} }
});
var i = 0;
structure.on('swap', function (newData, oldData) {
i++;
if(i == 1) {
newData.toJS().should.eql({});
oldData.toJS().should.eql({ subtree: {} });
}
if(i == 2) {
newData.toJS().should.eql({ subtree: { hello: 'world'} });
oldData.toJS().should.eql({});
structure.cursor().toJS().should.eql({ subtree: { hello: 'world' } });
done();
}
});
structure.once('delete', function (newValue, path) {
path.should.eql(['subtree']);
newValue.toJS().should.eql({});
structure.cursor('subtree').update('hello',function() {
return 'world';
});
});
structure.cursor().delete('subtree');
});
it('should avoid stale cursor when deleting existing property', function() {
var struct = new Structure({
data: { foo: { a: 42 }, bar: { b: 24 } }
});
var foo = struct.cursor('foo');
var bar = struct.cursor('bar');
var current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: { a: 42 }, bar: { b: 24 } });
current.should.equal(oldRoot);
foo._rootData.should.equal(current);
newRoot.toJS().should.eql({ foo: {}, bar: { b: 24 } });
struct.current.toJS().should.eql(newRoot.toJS());
});
foo.delete('a');
struct.current.toJS().should.eql({ foo: {}, bar: {b: 24} });
current = struct.current;
struct.once('swap', function(newRoot, oldRoot) {
oldRoot.toJS().should.eql({ foo: { }, bar: { b: 24 } });
current.toJS().should.eql(oldRoot.toJS());
current.toJS().should.not.eql(newRoot.toJS());
newRoot.toJS().should.not.eql(oldRoot.toJS());
newRoot.toJS().should.eql({ foo: {}, bar: {} });
});
bar.delete('b');
struct.current.toJS().should.eql({ foo: {}, bar: {} });
});
});
});
describe('history', function () {
it('should be able to undo default', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true
});
structure.cursor('foo').update(function () { return 'hello'; });
structure.cursor('foo').deref().should.equal('hello');
structure.undo();
structure.cursor('foo').deref().should.equal('bar');
structure.cursor('foo').update(function () { return 'hello2'; });
structure.cursor('foo').deref().should.equal('hello2');
structure.history.toJS().should.eql([
{ 'foo': 'bar' },
{ 'foo': 'hello2' }
]);
});
it('should be able to redo default', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true
});
structure.cursor('foo').update(function () { return 'hello'; });
structure.cursor('foo').deref().should.equal('hello');
structure.undo();
structure.cursor('foo').deref().should.equal('bar');
structure.redo();
structure.cursor('foo').deref().should.equal('hello');
});
it('should be able undo multiple steps', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true
});
structure.cursor('foo').update(function () { return 'Change 1'; });
structure.cursor('foo').update(function () { return 'Change 2'; });
structure.cursor('foo').deref().should.equal('Change 2');
structure.undo(2);
structure.cursor('foo').deref().should.equal('bar');
});
it('should be able redo multiple steps', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true
});
structure.cursor('foo').update(function () { return 'Change 1'; });
structure.cursor('foo').update(function () { return 'Change 2'; });
structure.cursor('foo').update(function () { return 'Change 3'; });
structure.cursor('foo').deref().should.equal('Change 3');
structure.undo(3);
structure.cursor('foo').deref().should.equal('bar');
structure.redo(2);
structure.cursor('foo').deref().should.equal('Change 2');
});
it('should be able undo until object passed as argument', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true
});
structure.cursor('foo').update(function () { return 'Change 1'; });
structure.cursor('foo').deref().should.equal('Change 1');
var change1 = structure.current;
structure.cursor('foo').update(function () { return 'Change 2'; });
structure.cursor('foo').deref().should.equal('Change 2');
structure.undoUntil(change1);
structure.cursor('foo').deref().should.equal('Change 1');
});
describe('with limit', function () {
it('should be able to undo default', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true,
historyLimit: 2
});
structure.cursor('foo').update(function () { return 'hello'; });
structure.cursor('foo').deref().should.equal('hello');
structure.undo();
structure.cursor('foo').deref().should.equal('bar');
structure.cursor('foo').update(function () { return 'hello2'; });
structure.cursor('foo').deref().should.equal('hello2');
structure.history.toJS().should.eql([
{ 'foo': 'bar' },
{ 'foo': 'hello2' }
]);
structure.cursor('foo').update(function () { return 'hello3'; });
structure.cursor('foo').deref().should.equal('hello3');
structure.history.toJS().should.eql([
{ 'foo': 'hello2' },
{ 'foo': 'hello3' }
]);
});
it('should be able to redo default', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true,
historyLimit: 2
});
structure.cursor('foo').update(function () { return 'hello'; });
structure.cursor('foo').deref().should.equal('hello');
structure.undo();
structure.cursor('foo').deref().should.equal('bar');
structure.redo();
structure.cursor('foo').deref().should.equal('hello');
});
it('should be able undo multiple steps up to capped start', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true,
historyLimit: 2
});
structure.cursor('foo').update(function () { return 'Change 1'; });
structure.cursor('foo').update(function () { return 'Change 2'; });
structure.cursor('foo').deref().should.equal('Change 2');
structure.undo(2);
structure.cursor('foo').deref().should.equal('Change 1');
});
it('should be able redo multiple steps', function () {
var structure = new Structure({
data: { 'foo': 'bar' },
history: true,
historyLimit: 2
});
structure.cursor('foo').update(function () { return 'Change 1'; });
structure.cursor('foo').update(function () { return 'Change 2'; });
structure.cursor('foo').update(function () { return 'Change 3'; });
structure.cursor('foo').deref().should.equal('Change 3');
structure.undo(3);
structure.cursor('foo').deref().should.equal('Change 2');
structure.redo(2);
structure.cursor('foo').deref().should.equal('Change 3');
});
});
});
describe('reference', function () {
it('should expose API for creating reference', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
structure.should.have.property('reference');
});
it('should expose references with observe and unobserve functions', function () {
var ref = new Structure({
data: { 'foo': 'bar' }
}).reference();
ref.should.have.property('observe');
ref.should.have.property('unobserveAll');
});
it('should listen to paths before they are created', function (done) {
var structure = new Structure({ data: {} });
var i = 0;
var ref = structure.reference('foo');
ref.observe(function (data) {
if (i++ === 0) {
ref.cursor().deref().should.eql('hello world');
} else {
ref.cursor().toJS().should.eql({ value: 10 });
done();
}
});
structure.cursor().update(function() {
return Immutable.fromJS({ foo: 'hello world' });
});
structure.cursor().update(function() {
return Immutable.fromJS({ foo: { value: 10 } });
});
});
it('should listen to nested paths before they are created', function (done) {
var structure = new Structure({ data: {} });
var i = 0;
var ref = structure.reference(['foo', 'value']);
ref.observe(function () {
ref.cursor().deref().should.eql(10);
done();
});
structure.cursor().update(function() {
return Immutable.fromJS({ foo: 'hello world' });
});
structure.cursor().update(function() {
return Immutable.fromJS({ foo: { value: 10 } });
});
});
it('should create cursor for value', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
structure.reference('foo').cursor().deref().should.equal('bar');
});
it('should have a self-updating cursor', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
var newCursor = ref.cursor().update(function () {
return 'updated';
});
newCursor.deref().should.equal('updated');
ref.cursor().deref().should.equal('updated');
});
it('should take cursor as argument', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference(structure.cursor('foo'));
var isCalled = false;
var newCursor = ref.cursor().update(function () {
isCalled = true;
return 'updated';
});
newCursor.deref().should.equal('updated');
ref.cursor().deref().should.equal('updated');
isCalled.should.equal(true);
});
it('should have a self-updating cursor when changing from outside', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
var newCursor = structure.cursor('foo').update(function () {
return 'updated';
});
newCursor.deref().should.equal('updated');
ref.cursor().deref().should.equal('updated');
});
it('should have a self-updating cursor on children', function () {
var structure = new Structure({
data: { 'foo': { 'bar': 1 } }
});
var ref = structure.reference('foo');
var newCursor = ref.cursor().cursor('bar').update(function (state) {
return 'updated';
});
newCursor.deref().should.equal('updated');
ref.cursor().toJS().should.eql({
'bar': 'updated'
});
});
it('should support sub-cursor', function () {
var structure = new Structure({
data: { 'foo': { 'bar': 1 } }
});
var ref = structure.reference('foo');
var newCursor = ref.cursor('bar').update(function (state) {
return 'updated';
});
newCursor.deref().should.equal('updated');
ref.cursor().toJS().should.eql({
'bar': 'updated'
});
});
it('should still be a reference after unobserve', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
ref.unobserveAll();
ref.cursor().update(function () { return 'updated'; });
ref.cursor().deref().should.equal('updated');
});
it('should be destroyable', function () {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
ref.destroy();
(ref.cursor === void 0).should.equal(true);
(ref.observe === void 0).should.equal(true);
(ref.unobserveAll === void 0).should.equal(true);
});
describe('listeners', function () {
it('should trigger change listener for reference', function (done) {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
ref.observe(function () { done(); });
ref.cursor().update(function () { return 'updated'; });
});
it('should trigger change listener for depending observed references', function (done) {
var structure = new Structure({
data: {a: {b: {c: "value"}}}
});
var ref = structure.reference(['a', 'b']);
ref.observe(function () { done(); });
structure.cursor('a').update(function (old) { return old.setIn(['b', 'c'], 'updated'); });
});
it('should trigger change listener for depending observed references', function (done) {
var structure = new Structure({
data: {a: {b: {c: "value"}}}
});
var ref = structure.reference(['a', 'b']);
ref.observe(function () { done(); });
structure.cursor().update(function (old) { return old.setIn(['a', 'b', 'c'], 'updated'); });
});
it('should trigger change listener for reference created from a cursor', function (done) {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference(structure.cursor('foo'));
ref.observe(function () { done(); });
ref.cursor().update(function () { return 'updated'; });
});
it('should trigger change listener for reference when changing cursor from outside', function (done) {
var structure = new Structure({
data: { 'foo': 'bar' }
});
var ref = structure.reference('foo');
ref.observe(function () { done(); });
structure.cursor('foo').update(function () { return 'updated'; });
});
it('should trigger change listener for reference when changing cursor from outside on root', function (done) {
var structure = new Structure({
data: { 'foo': 'bar' }
});