forked from emberjs/ember.js
/
computed_test.js
1006 lines (839 loc) · 28.4 KB
/
computed_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
import { Object as EmberObject } from '@ember/-internals/runtime';
import {
ComputedProperty,
computed,
getCachedValueFor,
Descriptor,
defineProperty,
get,
set,
isWatching,
addObserver,
} from '..';
import { meta as metaFor } from '@ember/-internals/meta';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
let obj, count;
moduleFor(
'computed',
class extends AbstractTestCase {
['@test computed property should be an instance of descriptor'](assert) {
assert.ok(computed(function() {}) instanceof Descriptor);
}
['@test computed properties assert the presence of a getter or setter function']() {
expectAssertion(function() {
computed('nogetternorsetter', {});
}, 'Computed properties must receive a getter or a setter, you passed none.');
}
['@test computed properties check for the presence of a function or configuration object']() {
expectAssertion(function() {
computed('nolastargument');
}, 'computed expects a function or an object as last argument.');
}
['@test computed properties defined with an object only allow `get` and `set` keys']() {
expectAssertion(function() {
computed({
get() {},
set() {},
other() {},
});
}, 'Config object passed to computed can only contain `get` and `set` keys.');
}
['@test computed property can be accessed without `get`'](assert) {
let obj = {};
let count = 0;
defineProperty(
obj,
'foo',
computed(function(key) {
count++;
return 'computed ' + key;
})
);
assert.equal(obj.foo, 'computed foo', 'should return value');
assert.equal(count, 1, 'should have invoked computed property');
}
['@test defining computed property should invoke property on get'](assert) {
let obj = {};
let count = 0;
defineProperty(
obj,
'foo',
computed(function(key) {
count++;
return 'computed ' + key;
})
);
assert.equal(get(obj, 'foo'), 'computed foo', 'should return value');
assert.equal(count, 1, 'should have invoked computed property');
}
['@test can override volatile computed property'](assert) {
expectDeprecation(() => {
let obj = {};
defineProperty(obj, 'foo', computed(function() {}).volatile());
set(obj, 'foo', 'boom');
assert.equal(obj.foo, 'boom');
}, 'Setting a computed property as volatile has been deprecated. Instead, consider using a native getter with native class syntax.');
}
['@test defining computed property should invoke property on set'](assert) {
let obj = {};
let count = 0;
defineProperty(
obj,
'foo',
computed({
get(key) {
return this['__' + key];
},
set(key, value) {
count++;
this['__' + key] = 'computed ' + value;
return this['__' + key];
},
})
);
assert.equal(set(obj, 'foo', 'bar'), 'bar', 'should return set value');
assert.equal(count, 1, 'should have invoked computed property');
assert.equal(get(obj, 'foo'), 'computed bar', 'should return new value');
}
['@test defining a computed property with a dependent key ending with @each is expanded to []'](
assert
) {
let cp = computed('blazo.@each', function() {});
assert.deepEqual(cp._dependentKeys, ['blazo.[]']);
cp = computed('qux', 'zoopa.@each', function() {});
assert.deepEqual(cp._dependentKeys, ['qux', 'zoopa.[]']);
}
['@test defining a computed property with a dependent key more than one level deep beyond @each is not supported']() {
expectNoWarning(() => {
computed('todos', () => {});
});
expectNoWarning(() => {
computed('todos.@each.owner', () => {});
});
expectWarning(() => {
computed('todos.@each.owner.name', () => {});
}, /You used the key "todos\.@each\.owner\.name" which is invalid\. /);
expectWarning(() => {
computed('todos.@each.owner.@each.name', () => {});
}, /You used the key "todos\.@each\.owner\.@each\.name" which is invalid\. /);
}
}
);
let objA, objB;
moduleFor(
'computed should inherit through prototype',
class extends AbstractTestCase {
beforeEach() {
objA = { __foo: 'FOO' };
defineProperty(
objA,
'foo',
computed({
get(key) {
return this['__' + key];
},
set(key, value) {
this['__' + key] = 'computed ' + value;
return this['__' + key];
},
})
);
objB = Object.create(objA);
objB.__foo = 'FOO'; // make a copy;
}
afterEach() {
objA = objB = null;
}
['@test using get() and set()'](assert) {
assert.equal(get(objA, 'foo'), 'FOO', 'should get FOO from A');
assert.equal(get(objB, 'foo'), 'FOO', 'should get FOO from B');
set(objA, 'foo', 'BIFF');
assert.equal(get(objA, 'foo'), 'computed BIFF', 'should change A');
assert.equal(get(objB, 'foo'), 'FOO', 'should NOT change B');
set(objB, 'foo', 'bar');
assert.equal(get(objB, 'foo'), 'computed bar', 'should change B');
assert.equal(get(objA, 'foo'), 'computed BIFF', 'should NOT change A');
set(objA, 'foo', 'BAZ');
assert.equal(get(objA, 'foo'), 'computed BAZ', 'should change A');
assert.equal(get(objB, 'foo'), 'computed bar', 'should NOT change B');
}
}
);
moduleFor(
'redefining computed property to normal',
class extends AbstractTestCase {
beforeEach() {
objA = { __foo: 'FOO' };
defineProperty(
objA,
'foo',
computed({
get(key) {
return this['__' + key];
},
set(key, value) {
this['__' + key] = 'computed ' + value;
return this['__' + key];
},
})
);
objB = Object.create(objA);
defineProperty(objB, 'foo'); // make this just a normal property.
}
afterEach() {
objA = objB = null;
}
['@test using get() and set()'](assert) {
assert.equal(get(objA, 'foo'), 'FOO', 'should get FOO from A');
assert.equal(get(objB, 'foo'), undefined, 'should get undefined from B');
set(objA, 'foo', 'BIFF');
assert.equal(get(objA, 'foo'), 'computed BIFF', 'should change A');
assert.equal(get(objB, 'foo'), undefined, 'should NOT change B');
set(objB, 'foo', 'bar');
assert.equal(get(objB, 'foo'), 'bar', 'should change B');
assert.equal(get(objA, 'foo'), 'computed BIFF', 'should NOT change A');
set(objA, 'foo', 'BAZ');
assert.equal(get(objA, 'foo'), 'computed BAZ', 'should change A');
assert.equal(get(objB, 'foo'), 'bar', 'should NOT change B');
}
}
);
moduleFor(
'redefining computed property to another property',
class extends AbstractTestCase {
beforeEach() {
objA = { __foo: 'FOO' };
defineProperty(
objA,
'foo',
computed({
get(key) {
return this['__' + key];
},
set(key, value) {
this['__' + key] = 'A ' + value;
return this['__' + key];
},
})
);
objB = Object.create(objA);
objB.__foo = 'FOO';
defineProperty(
objB,
'foo',
computed({
get(key) {
return this['__' + key];
},
set(key, value) {
this['__' + key] = 'B ' + value;
return this['__' + key];
},
})
);
}
afterEach() {
objA = objB = null;
}
['@test using get() and set()'](assert) {
assert.equal(get(objA, 'foo'), 'FOO', 'should get FOO from A');
assert.equal(get(objB, 'foo'), 'FOO', 'should get FOO from B');
set(objA, 'foo', 'BIFF');
assert.equal(get(objA, 'foo'), 'A BIFF', 'should change A');
assert.equal(get(objB, 'foo'), 'FOO', 'should NOT change B');
set(objB, 'foo', 'bar');
assert.equal(get(objB, 'foo'), 'B bar', 'should change B');
assert.equal(get(objA, 'foo'), 'A BIFF', 'should NOT change A');
set(objA, 'foo', 'BAZ');
assert.equal(get(objA, 'foo'), 'A BAZ', 'should change A');
assert.equal(get(objB, 'foo'), 'B bar', 'should NOT change B');
}
}
);
moduleFor(
'computed - metadata',
class extends AbstractTestCase {
['@test can set metadata on a computed property'](assert) {
let computedProperty = computed(function() {});
computedProperty.meta({ key: 'keyValue' });
assert.equal(
computedProperty.meta().key,
'keyValue',
'saves passed meta hash to the _meta property'
);
}
['@test meta should return an empty hash if no meta is set'](assert) {
let computedProperty = computed(function() {});
assert.deepEqual(computedProperty.meta(), {}, 'returned value is an empty hash');
}
}
);
// ..........................................................
// CACHEABLE
//
moduleFor(
'computed - cacheable',
class extends AbstractTestCase {
beforeEach() {
obj = {};
count = 0;
let func = function() {
count++;
return 'bar ' + count;
};
defineProperty(obj, 'foo', computed({ get: func, set: func }));
}
afterEach() {
obj = count = null;
}
['@test cacheable should cache'](assert) {
assert.equal(get(obj, 'foo'), 'bar 1', 'first get');
assert.equal(get(obj, 'foo'), 'bar 1', 'second get');
assert.equal(count, 1, 'should only invoke once');
}
['@test modifying a cacheable property should update cache'](assert) {
assert.equal(get(obj, 'foo'), 'bar 1', 'first get');
assert.equal(get(obj, 'foo'), 'bar 1', 'second get');
assert.equal(set(obj, 'foo', 'baz'), 'baz', 'setting');
assert.equal(get(obj, 'foo'), 'bar 2', 'third get');
assert.equal(count, 2, 'should not invoke again');
}
['@test inherited property should not pick up cache'](assert) {
let objB = Object.create(obj);
assert.equal(get(obj, 'foo'), 'bar 1', 'obj first get');
assert.equal(get(objB, 'foo'), 'bar 2', 'objB first get');
assert.equal(get(obj, 'foo'), 'bar 1', 'obj second get');
assert.equal(get(objB, 'foo'), 'bar 2', 'objB second get');
set(obj, 'foo', 'baz'); // modify A
assert.equal(get(obj, 'foo'), 'bar 3', 'obj third get');
assert.equal(get(objB, 'foo'), 'bar 2', 'objB third get');
}
['@test getCachedValueFor should return the cached value'](assert) {
assert.equal(getCachedValueFor(obj, 'foo'), undefined, 'should not yet be a cached value');
get(obj, 'foo');
assert.equal(getCachedValueFor(obj, 'foo'), 'bar 1', 'should retrieve cached value');
}
['@test getCachedValueFor should return falsy cached values'](assert) {
defineProperty(
obj,
'falsy',
computed(function() {
return false;
})
);
assert.equal(getCachedValueFor(obj, 'falsy'), undefined, 'should not yet be a cached value');
get(obj, 'falsy');
assert.equal(getCachedValueFor(obj, 'falsy'), false, 'should retrieve cached value');
}
['@test setting a cached computed property passes the old value as the third argument'](
assert
) {
let obj = {
foo: 0,
};
let receivedOldValue;
defineProperty(
obj,
'plusOne',
computed('foo', {
get() {},
set(key, value, oldValue) {
receivedOldValue = oldValue;
return value;
},
})
);
set(obj, 'plusOne', 1);
assert.strictEqual(receivedOldValue, undefined, 'oldValue should be undefined');
set(obj, 'plusOne', 2);
assert.strictEqual(receivedOldValue, 1, 'oldValue should be 1');
set(obj, 'plusOne', 3);
assert.strictEqual(receivedOldValue, 2, 'oldValue should be 2');
}
}
);
// ..........................................................
// DEPENDENT KEYS
//
moduleFor(
'computed - dependentkey',
class extends AbstractTestCase {
beforeEach() {
obj = { bar: 'baz' };
count = 0;
let getterAndSetter = function() {
count++;
get(this, 'bar');
return 'bar ' + count;
};
defineProperty(
obj,
'foo',
computed('bar', {
get: getterAndSetter,
set: getterAndSetter,
})
);
}
afterEach() {
obj = count = null;
}
['@test should lazily watch dependent keys on set'](assert) {
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
set(obj, 'foo', 'bar');
assert.equal(isWatching(obj, 'bar'), true, 'lazily watching dependent key');
}
['@test should lazily watch dependent keys on get'](assert) {
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
get(obj, 'foo');
assert.equal(isWatching(obj, 'bar'), true, 'lazily watching dependent key');
}
['@test local dependent key should invalidate cache'](assert) {
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
assert.equal(get(obj, 'foo'), 'bar 1', 'get once');
assert.equal(isWatching(obj, 'bar'), true, 'lazily setup watching dependent key');
assert.equal(get(obj, 'foo'), 'bar 1', 'cached retrieve');
set(obj, 'bar', 'BIFF'); // should invalidate foo
assert.equal(get(obj, 'foo'), 'bar 2', 'should recache');
assert.equal(get(obj, 'foo'), 'bar 2', 'cached retrieve');
}
['@test should invalidate multiple nested dependent keys'](assert) {
let count = 0;
defineProperty(
obj,
'bar',
computed('baz', function() {
count++;
get(this, 'baz');
return 'baz ' + count;
})
);
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
assert.equal(isWatching(obj, 'baz'), false, 'precond not watching dependent key');
assert.equal(get(obj, 'foo'), 'bar 1', 'get once');
assert.equal(isWatching(obj, 'bar'), true, 'lazily setup watching dependent key');
assert.equal(isWatching(obj, 'baz'), true, 'lazily setup watching dependent key');
assert.equal(get(obj, 'foo'), 'bar 1', 'cached retrieve');
set(obj, 'baz', 'BIFF'); // should invalidate bar -> foo
assert.equal(
isWatching(obj, 'bar'),
false,
'should not be watching dependent key after cache cleared'
);
assert.equal(
isWatching(obj, 'baz'),
false,
'should not be watching dependent key after cache cleared'
);
assert.equal(get(obj, 'foo'), 'bar 2', 'should recache');
assert.equal(get(obj, 'foo'), 'bar 2', 'cached retrieve');
assert.equal(isWatching(obj, 'bar'), true, 'lazily setup watching dependent key');
assert.equal(isWatching(obj, 'baz'), true, 'lazily setup watching dependent key');
}
['@test circular keys should not blow up'](assert) {
let func = function() {
count++;
return 'bar ' + count;
};
defineProperty(obj, 'bar', computed('foo', { get: func, set: func }));
defineProperty(
obj,
'foo',
computed('bar', function() {
count++;
return 'foo ' + count;
})
);
assert.equal(get(obj, 'foo'), 'foo 1', 'get once');
assert.equal(get(obj, 'foo'), 'foo 1', 'cached retrieve');
set(obj, 'bar', 'BIFF'); // should invalidate bar -> foo -> bar
assert.equal(get(obj, 'foo'), 'foo 3', 'should recache');
assert.equal(get(obj, 'foo'), 'foo 3', 'cached retrieve');
}
['@test redefining a property should undo old dependent keys'](assert) {
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
assert.equal(get(obj, 'foo'), 'bar 1');
assert.equal(isWatching(obj, 'bar'), true, 'lazily watching dependent key');
defineProperty(
obj,
'foo',
computed('baz', function() {
count++;
return 'baz ' + count;
})
);
assert.equal(
isWatching(obj, 'bar'),
false,
'after redefining should not be watching dependent key'
);
assert.equal(get(obj, 'foo'), 'baz 2');
set(obj, 'bar', 'BIFF'); // should not kill cache
assert.equal(get(obj, 'foo'), 'baz 2');
set(obj, 'baz', 'BOP');
assert.equal(get(obj, 'foo'), 'baz 3');
}
['@test can watch multiple dependent keys specified declaratively via brace expansion'](
assert
) {
defineProperty(
obj,
'foo',
computed('qux.{bar,baz}', function() {
count++;
return 'foo ' + count;
})
);
assert.equal(get(obj, 'foo'), 'foo 1', 'get once');
assert.equal(get(obj, 'foo'), 'foo 1', 'cached retrieve');
set(obj, 'qux', {});
set(obj, 'qux.bar', 'bar'); // invalidate foo
assert.equal(get(obj, 'foo'), 'foo 2', 'foo invalidated from bar');
set(obj, 'qux.baz', 'baz'); // invalidate foo
assert.equal(get(obj, 'foo'), 'foo 3', 'foo invalidated from baz');
set(obj, 'qux.quux', 'quux'); // do not invalidate foo
assert.equal(get(obj, 'foo'), 'foo 3', 'foo not invalidated by quux');
}
['@test throws assertion if brace expansion notation has spaces']() {
expectAssertion(function() {
defineProperty(
obj,
'roo',
computed('fee.{bar, baz,bop , }', function() {
count++;
return 'roo ' + count;
})
);
}, /cannot contain spaces/);
}
['@test throws an assertion if an uncached `get` is called after object is destroyed'](assert) {
assert.equal(isWatching(obj, 'bar'), false, 'precond not watching dependent key');
let meta = metaFor(obj);
meta.destroy();
obj.toString = () => '<custom-obj:here>';
expectAssertion(() => {
get(obj, 'foo');
}, 'Cannot modify dependent keys for `foo` on `<custom-obj:here>` after it has been destroyed.');
assert.equal(isWatching(obj, 'bar'), false, 'deps were not updated');
}
}
);
// ..........................................................
// CHAINED DEPENDENT KEYS
//
let func;
moduleFor(
'computed - dependentkey with chained properties',
class extends AbstractTestCase {
beforeEach() {
obj = {
foo: {
bar: {
baz: {
biff: 'BIFF',
},
},
},
};
count = 0;
func = function() {
count++;
return get(obj, 'foo.bar.baz.biff') + ' ' + count;
};
}
afterEach() {
obj = count = func = null;
}
['@test depending on simple chain'](assert) {
// assign computed property
defineProperty(obj, 'prop', computed('foo.bar.baz.biff', func));
assert.equal(get(obj, 'prop'), 'BIFF 1');
set(get(obj, 'foo.bar.baz'), 'biff', 'BUZZ');
assert.equal(get(obj, 'prop'), 'BUZZ 2');
assert.equal(get(obj, 'prop'), 'BUZZ 2');
set(get(obj, 'foo.bar'), 'baz', { biff: 'BLOB' });
assert.equal(get(obj, 'prop'), 'BLOB 3');
assert.equal(get(obj, 'prop'), 'BLOB 3');
set(get(obj, 'foo.bar.baz'), 'biff', 'BUZZ');
assert.equal(get(obj, 'prop'), 'BUZZ 4');
assert.equal(get(obj, 'prop'), 'BUZZ 4');
set(get(obj, 'foo'), 'bar', { baz: { biff: 'BOOM' } });
assert.equal(get(obj, 'prop'), 'BOOM 5');
assert.equal(get(obj, 'prop'), 'BOOM 5');
set(get(obj, 'foo.bar.baz'), 'biff', 'BUZZ');
assert.equal(get(obj, 'prop'), 'BUZZ 6');
assert.equal(get(obj, 'prop'), 'BUZZ 6');
set(obj, 'foo', { bar: { baz: { biff: 'BLARG' } } });
assert.equal(get(obj, 'prop'), 'BLARG 7');
assert.equal(get(obj, 'prop'), 'BLARG 7');
set(get(obj, 'foo.bar.baz'), 'biff', 'BUZZ');
assert.equal(get(obj, 'prop'), 'BUZZ 8');
assert.equal(get(obj, 'prop'), 'BUZZ 8');
defineProperty(obj, 'prop');
set(obj, 'prop', 'NONE');
assert.equal(get(obj, 'prop'), 'NONE');
set(obj, 'foo', { bar: { baz: { biff: 'BLARG' } } });
assert.equal(get(obj, 'prop'), 'NONE'); // should do nothing
assert.equal(count, 8, 'should be not have invoked computed again');
}
['@test chained dependent keys should evaluate computed properties lazily'](assert) {
defineProperty(obj.foo.bar, 'b', computed(func));
defineProperty(obj.foo, 'c', computed('bar.b', function() {}));
assert.equal(count, 0, 'b should not run');
}
}
);
// ..........................................................
// improved-cp-syntax
//
moduleFor(
'computed - improved cp syntax',
class extends AbstractTestCase {
['@test setter and getters are passed using an object'](assert) {
let testObj = EmberObject.extend({
a: '1',
b: '2',
aInt: computed('a', {
get(keyName) {
assert.equal(keyName, 'aInt', 'getter receives the keyName');
return parseInt(this.get('a'));
},
set(keyName, value, oldValue) {
assert.equal(keyName, 'aInt', 'setter receives the keyName');
assert.equal(value, 123, 'setter receives the new value');
assert.equal(oldValue, 1, 'setter receives the old value');
this.set('a', String(value)); // side effect
return parseInt(this.get('a'));
},
}),
}).create();
assert.ok(testObj.get('aInt') === 1, 'getter works');
testObj.set('aInt', 123);
assert.ok(testObj.get('a') === '123', 'setter works');
assert.ok(testObj.get('aInt') === 123, 'cp has been updated too');
}
['@test setter can be omited'](assert) {
expectDeprecation(() => {
let testObj = EmberObject.extend({
a: '1',
b: '2',
aInt: computed('a', {
get(keyName) {
assert.equal(keyName, 'aInt', 'getter receives the keyName');
return parseInt(this.get('a'));
},
}),
}).create();
assert.ok(testObj.get('aInt') === 1, 'getter works');
assert.ok(testObj.get('a') === '1');
testObj.set('aInt', '123');
assert.ok(testObj.get('aInt') === '123', 'cp has been updated too');
}, /The aInt computed property was just overriden/);
}
['@test getter can be omited'](assert) {
let testObj = EmberObject.extend({
com: computed({
set(key, value) {
return value;
},
}),
}).create();
assert.ok(testObj.get('com') === undefined);
testObj.set('com', '123');
assert.ok(testObj.get('com') === '123', 'cp has been updated');
}
['@test the return value of the setter gets cached'](assert) {
let testObj = EmberObject.extend({
a: '1',
sampleCP: computed('a', {
get() {
assert.ok(false, 'The getter should not be invoked');
return 'get-value';
},
set() {
return 'set-value';
},
}),
}).create();
testObj.set('sampleCP', 'abcd');
assert.ok(testObj.get('sampleCP') === 'set-value', 'The return value of the CP was cached');
}
}
);
// ..........................................................
// BUGS
//
moduleFor(
'computed edge cases',
class extends AbstractTestCase {
['@test adding a computed property should show up in key iteration'](assert) {
let obj = {};
defineProperty(obj, 'foo', computed(function() {}));
let found = [];
for (let key in obj) {
found.push(key);
}
assert.ok(
found.indexOf('foo') >= 0,
'should find computed property in iteration found=' + found
);
assert.ok('foo' in obj, 'foo in obj should pass');
}
["@test when setting a value after it had been retrieved empty don't pass function UNDEFINED as oldValue"](
assert
) {
let obj = {};
let oldValueIsNoFunction = true;
defineProperty(
obj,
'foo',
computed({
get() {},
set(key, value, oldValue) {
if (typeof oldValue === 'function') {
oldValueIsNoFunction = false;
}
return undefined;
},
})
);
get(obj, 'foo');
set(obj, 'foo', undefined);
assert.ok(oldValueIsNoFunction);
}
}
);
moduleFor(
'computed - setter',
class extends AbstractTestCase {
['@test setting a watched computed property'](assert) {
let obj = {
firstName: 'Yehuda',
lastName: 'Katz',
};
defineProperty(
obj,
'fullName',
computed('firstName', 'lastName', {
get() {
return get(this, 'firstName') + ' ' + get(this, 'lastName');
},
set(key, value) {
let values = value.split(' ');
set(this, 'firstName', values[0]);
set(this, 'lastName', values[1]);
return value;
},
})
);
let fullNameDidChange = 0;
let firstNameDidChange = 0;
let lastNameDidChange = 0;
addObserver(obj, 'fullName', function() {
fullNameDidChange++;
});
addObserver(obj, 'firstName', function() {
firstNameDidChange++;
});
addObserver(obj, 'lastName', function() {
lastNameDidChange++;
});
assert.equal(get(obj, 'fullName'), 'Yehuda Katz');
set(obj, 'fullName', 'Yehuda Katz');
set(obj, 'fullName', 'Kris Selden');
assert.equal(get(obj, 'fullName'), 'Kris Selden');
assert.equal(get(obj, 'firstName'), 'Kris');
assert.equal(get(obj, 'lastName'), 'Selden');
assert.equal(fullNameDidChange, 1);
assert.equal(firstNameDidChange, 1);
assert.equal(lastNameDidChange, 1);
}
['@test setting a cached computed property that modifies the value you give it'](assert) {
let obj = {
foo: 0,
};
defineProperty(
obj,
'plusOne',
computed('foo', {
get() {
return get(this, 'foo') + 1;
},
set(key, value) {
set(this, 'foo', value);
return value + 1;
},
})
);
let plusOneDidChange = 0;
addObserver(obj, 'plusOne', function() {
plusOneDidChange++;
});
assert.equal(get(obj, 'plusOne'), 1);
set(obj, 'plusOne', 1);
assert.equal(get(obj, 'plusOne'), 2);
set(obj, 'plusOne', 1);
assert.equal(get(obj, 'plusOne'), 2);
assert.equal(plusOneDidChange, 1);
set(obj, 'foo', 5);
assert.equal(get(obj, 'plusOne'), 6);
assert.equal(plusOneDidChange, 2);
}
}
);
moduleFor(
'computed - default setter',
class extends AbstractTestCase {
["@test when setting a value on a computed property that doesn't handle sets"](assert) {
expectDeprecation(() => {
let obj = {};
let observerFired = false;
defineProperty(
obj,
'foo',
computed(function() {
return 'foo';
})
);
addObserver(obj, 'foo', null, () => (observerFired = true));
set(obj, 'foo', 'bar');
assert.equal(get(obj, 'foo'), 'bar', 'The set value is properly returned');
assert.ok(typeof obj.foo === 'string', 'The computed property was removed');
assert.ok(observerFired, 'The observer was still notified');
}, /The foo computed property was just overriden./);
}
}
);
moduleFor(
'computed - readOnly',
class extends AbstractTestCase {
['@test is chainable'](assert) {
let cp = computed(function() {}).readOnly();
assert.ok(cp instanceof Descriptor);
assert.ok(cp instanceof ComputedProperty);
}
['@test throws assertion if called over a CP with a setter defined with the new syntax']() {
expectAssertion(() => {
computed({
get() {},
set() {},
}).readOnly();
}, /Computed properties that define a setter using the new syntax cannot be read-only/);
}
['@test protects against setting'](assert) {
let obj = {};
defineProperty(
obj,
'bar',
computed(function() {
return 'barValue';
}).readOnly()
);
assert.equal(get(obj, 'bar'), 'barValue');
assert.throws(() => {
set(obj, 'bar', 'newBar');