-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathreduce_computed_macros_test.js
2368 lines (1917 loc) · 65 KB
/
reduce_computed_macros_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 { run } from '@ember/runloop';
import {
defineProperty,
setProperties,
get,
set,
addObserver,
computed,
observer,
} from '@ember/-internals/metal';
import {
Object as EmberObject,
ObjectProxy,
isArray,
A as emberA,
removeAt,
} from '@ember/-internals/runtime';
import {
sum,
min,
max,
map,
sort,
setDiff,
mapBy,
filter,
filterBy,
uniq,
uniqBy,
union,
intersect,
collect,
} from '@ember/object/computed';
import { moduleFor, AbstractTestCase, runLoopSettled } from 'internal-test-helpers';
let obj;
moduleFor(
'map',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
mapped: map('[email protected]', item => item.v),
mappedObjects: map('[email protected]', item => ({
name: item.v.name,
})),
}).create({
arrayObjects: emberA([{ v: { name: 'Robert' } }, { v: { name: 'Leanna' } }]),
array: emberA([{ v: 1 }, { v: 3 }, { v: 2 }, { v: 1 }]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test map is readOnly'](assert) {
assert.throws(function() {
obj.set('mapped', 1);
}, /Cannot set read-only property "mapped" on object:/);
}
['@test it maps simple properties'](assert) {
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 1]);
obj.get('array').pushObject({ v: 5 });
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 1, 5]);
removeAt(obj.get('array'), 3);
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 5]);
}
['@test it maps simple unshifted properties'](assert) {
let array = emberA();
obj = EmberObject.extend({
mapped: map('array', item => item.toUpperCase()),
}).create({
array,
});
array.unshiftObject('c');
array.unshiftObject('b');
array.unshiftObject('a');
array.popObject();
assert.deepEqual(
obj.get('mapped'),
['A', 'B'],
'properties unshifted in sequence are mapped correctly'
);
}
['@test it has the correct `this`'](assert) {
obj = EmberObject.extend({
mapped: map('array', function(item) {
assert.equal(this, obj, 'should have correct context');
return this.upperCase(item);
}),
upperCase(string) {
return string.toUpperCase();
},
}).create({
array: ['a', 'b', 'c'],
});
assert.deepEqual(
obj.get('mapped'),
['A', 'B', 'C'],
'properties unshifted in sequence are mapped correctly'
);
}
['@test it passes the index to the callback'](assert) {
let array = ['a', 'b', 'c'];
obj = EmberObject.extend({
mapped: map('array', (item, index) => index),
}).create({
array,
});
assert.deepEqual(obj.get('mapped'), [0, 1, 2], 'index is passed to callback correctly');
}
['@test it maps objects'](assert) {
assert.deepEqual(obj.get('mappedObjects'), [{ name: 'Robert' }, { name: 'Leanna' }]);
obj.get('arrayObjects').pushObject({
v: { name: 'Eddard' },
});
assert.deepEqual(obj.get('mappedObjects'), [
{ name: 'Robert' },
{ name: 'Leanna' },
{ name: 'Eddard' },
]);
removeAt(obj.get('arrayObjects'), 1);
assert.deepEqual(obj.get('mappedObjects'), [{ name: 'Robert' }, { name: 'Eddard' }]);
set(obj.get('arrayObjects')[0], 'v', { name: 'Stannis' });
assert.deepEqual(obj.get('mappedObjects'), [{ name: 'Stannis' }, { name: 'Eddard' }]);
}
['@test it maps unshifted objects with property observers'](assert) {
let array = emberA();
let cObj = { v: 'c' };
obj = EmberObject.extend({
mapped: map('[email protected]', item => get(item, 'v').toUpperCase()),
}).create({
array,
});
array.unshiftObject(cObj);
array.unshiftObject({ v: 'b' });
array.unshiftObject({ v: 'a' });
set(cObj, 'v', 'd');
assert.deepEqual(array.mapBy('v'), ['a', 'b', 'd'], 'precond - unmapped array is correct');
assert.deepEqual(
obj.get('mapped'),
['A', 'B', 'D'],
'properties unshifted in sequence are mapped correctly'
);
}
['@test it updates if additional dependent keys are modified'](assert) {
obj = EmberObject.extend({
mapped: map('array', ['key'], function(item) {
return item[this.key];
}),
}).create({
key: 'name',
array: emberA([{ name: 'Cercei', house: 'Lannister' }]),
});
assert.deepEqual(
obj.get('mapped'),
['Cercei'],
'precond - mapped array is initially correct'
);
obj.set('key', 'house');
assert.deepEqual(
obj.get('mapped'),
['Lannister'],
'mapped prop updates correctly when additional dependency is updated'
);
}
['@test it throws on bad inputs']() {
expectAssertion(() => {
map('items.@each.{prop}', 'foo');
}, /The final parameter provided to map must be a callback function/);
expectAssertion(() => {
map('items.@each.{prop}', 'foo', function() {});
}, /The second parameter provided to map must either be the callback or an array of additional dependent keys/);
expectAssertion(() => {
map('items.@each.{prop}', function() {}, ['foo']);
}, /The final parameter provided to map must be a callback function/);
expectAssertion(() => {
map('items.@each.{prop}', ['foo']);
}, /The final parameter provided to map must be a callback function/);
}
}
);
moduleFor(
'mapBy',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
mapped: mapBy('array', 'v'),
}).create({
array: emberA([{ v: 1 }, { v: 3 }, { v: 2 }, { v: 1 }]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test mapBy is readOnly'](assert) {
assert.throws(function() {
obj.set('mapped', 1);
}, /Cannot set read-only property "mapped" on object:/);
}
['@test it maps properties'](assert) {
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 1]);
obj.get('array').pushObject({ v: 5 });
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 1, 5]);
removeAt(obj.get('array'), 3);
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 5]);
}
async ['@test it is observable'](assert) {
let calls = 0;
assert.deepEqual(obj.get('mapped'), [1, 3, 2, 1]);
addObserver(obj, 'mapped.@each', () => calls++);
obj.get('array').pushObject({ v: 5 });
await runLoopSettled();
assert.equal(calls, 1, 'mapBy is observable');
}
}
);
moduleFor(
'filter',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
filtered: filter('array', item => item % 2 === 0),
}).create({
array: emberA([1, 2, 3, 4, 5, 6, 7, 8]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test filter is readOnly'](assert) {
assert.throws(function() {
obj.set('filtered', 1);
}, /Cannot set read-only property "filtered" on object:/);
}
['@test it filters according to the specified filter function'](assert) {
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'filter filters by the specified function'
);
}
['@test it passes the index to the callback'](assert) {
obj = EmberObject.extend({
filtered: filter('array', (item, index) => index === 1),
}).create({
array: ['a', 'b', 'c'],
});
assert.deepEqual(get(obj, 'filtered'), ['b'], 'index is passed to callback correctly');
}
['@test it has the correct `this`'](assert) {
obj = EmberObject.extend({
filtered: filter('array', function(item, index) {
assert.equal(this, obj);
return this.isOne(index);
}),
isOne(value) {
return value === 1;
},
}).create({
array: ['a', 'b', 'c'],
});
assert.deepEqual(get(obj, 'filtered'), ['b'], 'index is passed to callback correctly');
}
['@test it passes the array to the callback'](assert) {
obj = EmberObject.extend({
filtered: filter('array', (item, index, array) => index === get(array, 'length') - 2),
}).create({
array: emberA(['a', 'b', 'c']),
});
assert.deepEqual(obj.get('filtered'), ['b'], 'array is passed to callback correctly');
}
['@test it caches properly'](assert) {
let array = obj.get('array');
let filtered = obj.get('filtered');
assert.ok(filtered === obj.get('filtered'));
array.addObject(11);
let newFiltered = obj.get('filtered');
assert.ok(filtered !== newFiltered);
assert.ok(obj.get('filtered') === newFiltered);
}
['@test it updates as the array is modified'](assert) {
let array = obj.get('array');
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'precond - filtered array is initially correct'
);
array.addObject(11);
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'objects not passing the filter are not added'
);
array.addObject(12);
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8, 12],
'objects passing the filter are added'
);
array.removeObject(3);
array.removeObject(4);
assert.deepEqual(
obj.get('filtered'),
[2, 6, 8, 12],
'objects removed from the dependent array are removed from the computed array'
);
}
['@test the dependent array can be cleared one at a time'](assert) {
let array = get(obj, 'array');
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'precond - filtered array is initially correct'
);
// clear 1-8 but in a random order
array.removeObject(3);
array.removeObject(1);
array.removeObject(2);
array.removeObject(4);
array.removeObject(8);
array.removeObject(6);
array.removeObject(5);
array.removeObject(7);
assert.deepEqual(obj.get('filtered'), [], 'filtered array cleared correctly');
}
['@test the dependent array can be `clear`ed directly (#3272)'](assert) {
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'precond - filtered array is initially correct'
);
obj.get('array').clear();
assert.deepEqual(obj.get('filtered'), [], 'filtered array cleared correctly');
}
['@test it updates as the array is replaced'](assert) {
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'precond - filtered array is initially correct'
);
obj.set('array', [20, 21, 22, 23, 24]);
assert.deepEqual(
obj.get('filtered'),
[20, 22, 24],
'computed array is updated when array is changed'
);
}
['@test it updates properly on @each with {} dependencies'](assert) {
let item = EmberObject.create({ prop: true });
obj = EmberObject.extend({
filtered: filter('items.@each.{prop}', function(item) {
return item.get('prop') === true;
}),
}).create({
items: emberA([item]),
});
assert.deepEqual(obj.get('filtered'), [item]);
item.set('prop', false);
assert.deepEqual(obj.get('filtered'), []);
}
['@test it updates if additional dependent keys are modified'](assert) {
obj = EmberObject.extend({
filtered: filter('array', ['modulo'], function(item) {
return item % this.modulo === 0;
}),
}).create({
modulo: 2,
array: emberA([1, 2, 3, 4, 5, 6, 7, 8]),
});
assert.deepEqual(
obj.get('filtered'),
[2, 4, 6, 8],
'precond - filtered array is initially correct'
);
obj.set('modulo', 3);
assert.deepEqual(
obj.get('filtered'),
[3, 6],
'filtered prop updates correctly when additional dependency is updated'
);
}
['@test it throws on bad inputs']() {
expectAssertion(() => {
filter('items.@each.{prop}', 'foo');
}, /The final parameter provided to filter must be a callback function/);
expectAssertion(() => {
filter('items.@each.{prop}', 'foo', function() {});
}, /The second parameter provided to filter must either be the callback or an array of additional dependent keys/);
expectAssertion(() => {
filter('items.@each.{prop}', function() {}, ['foo']);
}, /The final parameter provided to filter must be a callback function/);
expectAssertion(() => {
filter('items.@each.{prop}', ['foo']);
}, /The final parameter provided to filter must be a callback function/);
}
}
);
moduleFor(
'filterBy',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
a1s: filterBy('array', 'a', 1),
as: filterBy('array', 'a'),
bs: filterBy('array', 'b'),
}).create({
array: emberA([
{ name: 'one', a: 1, b: false },
{ name: 'two', a: 2, b: false },
{ name: 'three', a: 1, b: true },
{ name: 'four', b: true },
]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test filterBy is readOnly'](assert) {
assert.throws(function() {
obj.set('as', 1);
}, /Cannot set read-only property "as" on object:/);
}
['@test properties can be filtered by truthiness'](assert) {
assert.deepEqual(
obj.get('as').mapBy('name'),
['one', 'two', 'three'],
'properties can be filtered by existence'
);
assert.deepEqual(obj.get('bs').mapBy('name'), ['three', 'four'], 'booleans can be filtered');
set(obj.get('array')[0], 'a', undefined);
set(obj.get('array')[3], 'a', true);
set(obj.get('array')[0], 'b', true);
set(obj.get('array')[3], 'b', false);
assert.deepEqual(
obj.get('as').mapBy('name'),
['two', 'three', 'four'],
'arrays computed by filter property respond to property changes'
);
assert.deepEqual(
obj.get('bs').mapBy('name'),
['one', 'three'],
'arrays computed by filtered property respond to property changes'
);
obj.get('array').pushObject({ name: 'five', a: 6, b: true });
assert.deepEqual(
obj.get('as').mapBy('name'),
['two', 'three', 'four', 'five'],
'arrays computed by filter property respond to added objects'
);
assert.deepEqual(
obj.get('bs').mapBy('name'),
['one', 'three', 'five'],
'arrays computed by filtered property respond to added objects'
);
obj.get('array').popObject();
assert.deepEqual(
obj.get('as').mapBy('name'),
['two', 'three', 'four'],
'arrays computed by filter property respond to removed objects'
);
assert.deepEqual(
obj.get('bs').mapBy('name'),
['one', 'three'],
'arrays computed by filtered property respond to removed objects'
);
obj.set('array', [{ name: 'six', a: 12, b: true }]);
assert.deepEqual(
obj.get('as').mapBy('name'),
['six'],
'arrays computed by filter property respond to array changes'
);
assert.deepEqual(
obj.get('bs').mapBy('name'),
['six'],
'arrays computed by filtered property respond to array changes'
);
}
['@test properties can be filtered by values'](assert) {
assert.deepEqual(
obj.get('a1s').mapBy('name'),
['one', 'three'],
'properties can be filtered by matching value'
);
obj.get('array').pushObject({ name: 'five', a: 1 });
assert.deepEqual(
obj.get('a1s').mapBy('name'),
['one', 'three', 'five'],
'arrays computed by matching value respond to added objects'
);
obj.get('array').popObject();
assert.deepEqual(
obj.get('a1s').mapBy('name'),
['one', 'three'],
'arrays computed by matching value respond to removed objects'
);
set(obj.get('array')[1], 'a', 1);
set(obj.get('array')[2], 'a', 2);
assert.deepEqual(
obj.get('a1s').mapBy('name'),
['one', 'two'],
'arrays computed by matching value respond to modified properties'
);
}
['@test properties values can be replaced'](assert) {
obj = EmberObject.extend({
a1s: filterBy('array', 'a', 1),
a1bs: filterBy('a1s', 'b'),
}).create({
array: [],
});
assert.deepEqual(
obj.get('a1bs').mapBy('name'),
[],
'properties can be filtered by matching value'
);
set(obj, 'array', [{ name: 'item1', a: 1, b: true }]);
assert.deepEqual(
obj.get('a1bs').mapBy('name'),
['item1'],
'properties can be filtered by matching value'
);
}
}
);
[['uniq', uniq], ['union', union]].forEach(tuple => {
let [name, macro] = tuple;
moduleFor(
`computed.${name}`,
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
union: macro('array', 'array2', 'array3'),
}).create({
array: emberA([1, 2, 3, 4, 5, 6]),
array2: emberA([4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9]),
array3: emberA([1, 8, 10]),
});
}
afterEach() {
run(obj, 'destroy');
}
[`@test ${name} is readOnly`](assert) {
assert.throws(function() {
obj.set('union', 1);
}, /Cannot set read-only property "union" on object:/);
}
['@test does not include duplicates'](assert) {
let array = obj.get('array');
let array2 = obj.get('array2');
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
name + ' does not include duplicates'
);
array.pushObject(8);
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
name + ' does not add existing items'
);
array.pushObject(11);
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
name + ' adds new items'
);
removeAt(array2, 6); // remove 7
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
name + ' does not remove items that are still in the dependent array'
);
array2.removeObject(7);
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11],
name + ' removes items when their last instance is gone'
);
}
['@test has set-union semantics'](assert) {
let array = obj.get('array');
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
name + ' is initially correct'
);
array.removeObject(6);
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'objects are not removed if they exist in other dependent arrays'
);
array.clear();
assert.deepEqual(
obj.get('union').sort((x, y) => x - y),
[1, 4, 5, 6, 7, 8, 9, 10],
'objects are removed when they are no longer in any dependent array'
);
}
}
);
});
moduleFor(
'computed.uniqBy',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
list: null,
uniqueById: uniqBy('list', 'id'),
}).create({
list: emberA([{ id: 1, value: 'one' }, { id: 2, value: 'two' }, { id: 1, value: 'one' }]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test uniqBy is readOnly'](assert) {
assert.throws(function() {
obj.set('uniqueById', 1);
}, /Cannot set read-only property "uniqueById" on object:/);
}
['@test does not include duplicates'](assert) {
assert.deepEqual(obj.get('uniqueById'), [{ id: 1, value: 'one' }, { id: 2, value: 'two' }]);
}
['@test it does not share state among instances'](assert) {
let MyObject = EmberObject.extend({
list: [],
uniqueByName: uniqBy('list', 'name'),
});
let a = MyObject.create({
list: [{ name: 'bob' }, { name: 'mitch' }, { name: 'mitch' }],
});
let b = MyObject.create({
list: [{ name: 'warren' }, { name: 'mitch' }],
});
assert.deepEqual(a.get('uniqueByName'), [{ name: 'bob' }, { name: 'mitch' }]);
// Making sure that 'mitch' appears
assert.deepEqual(b.get('uniqueByName'), [{ name: 'warren' }, { name: 'mitch' }]);
}
['@test it handles changes to the dependent array'](assert) {
obj.get('list').pushObject({ id: 3, value: 'three' });
assert.deepEqual(
obj.get('uniqueById'),
[{ id: 1, value: 'one' }, { id: 2, value: 'two' }, { id: 3, value: 'three' }],
'The list includes three'
);
obj.get('list').pushObject({ id: 3, value: 'three' });
assert.deepEqual(
obj.get('uniqueById'),
[{ id: 1, value: 'one' }, { id: 2, value: 'two' }, { id: 3, value: 'three' }],
'The list does not include a duplicate three'
);
}
['@test it returns an empty array when computed on a non-array'](assert) {
let MyObject = EmberObject.extend({
list: null,
uniq: uniqBy('list', 'name'),
});
let a = MyObject.create({ list: 'not an array' });
assert.deepEqual(a.get('uniq'), []);
}
}
);
moduleFor(
'computed.intersect',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
intersection: intersect('array', 'array2', 'array3'),
}).create({
array: emberA([1, 2, 3, 4, 5, 6]),
array2: emberA([3, 3, 3, 4, 5]),
array3: emberA([3, 5, 6, 7, 8]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test intersect is readOnly'](assert) {
assert.throws(function() {
obj.set('intersection', 1);
}, /Cannot set read-only property "intersection" on object:/);
}
['@test it has set-intersection semantics'](assert) {
let array2 = obj.get('array2');
let array3 = obj.get('array3');
assert.deepEqual(
obj.get('intersection').sort((x, y) => x - y),
[3, 5],
'intersection is initially correct'
);
array2.shiftObject();
assert.deepEqual(
obj.get('intersection').sort((x, y) => x - y),
[3, 5],
'objects are not removed when they are still in all dependent arrays'
);
array2.shiftObject();
assert.deepEqual(
obj.get('intersection').sort((x, y) => x - y),
[3, 5],
'objects are not removed when they are still in all dependent arrays'
);
array2.shiftObject();
assert.deepEqual(
obj.get('intersection'),
[5],
'objects are removed once they are gone from all dependent arrays'
);
array2.pushObject(1);
assert.deepEqual(
obj.get('intersection'),
[5],
'objects are not added as long as they are missing from any dependent array'
);
array3.pushObject(1);
assert.deepEqual(
obj.get('intersection').sort((x, y) => x - y),
[1, 5],
'objects added once they belong to all dependent arrays'
);
}
}
);
moduleFor(
'setDiff',
class extends AbstractTestCase {
beforeEach() {
obj = EmberObject.extend({
diff: setDiff('array', 'array2'),
}).create({
array: emberA([1, 2, 3, 4, 5, 6, 7]),
array2: emberA([3, 4, 5, 10]),
});
}
afterEach() {
run(obj, 'destroy');
}
['@test setDiff is readOnly'](assert) {
assert.throws(function() {
obj.set('diff', 1);
}, /Cannot set read-only property "diff" on object:/);
}
['@test it asserts if given fewer or more than two dependent properties']() {
expectAssertion(
function() {
EmberObject.extend({
diff: setDiff('array'),
}).create({
array: emberA([1, 2, 3, 4, 5, 6, 7]),
array2: emberA([3, 4, 5]),
});
},
/`computed\.setDiff` requires exactly two dependent arrays/,
'setDiff requires two dependent arrays'
);
expectAssertion(
function() {
EmberObject.extend({
diff: setDiff('array', 'array2', 'array3'),
}).create({
array: emberA([1, 2, 3, 4, 5, 6, 7]),
array2: emberA([3, 4, 5]),
array3: emberA([7]),
});
},
/`computed\.setDiff` requires exactly two dependent arrays/,
'setDiff requires two dependent arrays'
);
}
['@test it has set-diff semantics'](assert) {
let array1 = obj.get('array');
let array2 = obj.get('array2');
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 6, 7],
'set-diff is initially correct'
);
array2.popObject();
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 6, 7],
'removing objects from the remove set has no effect if the object is not in the keep set'
);
array2.shiftObject();
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 3, 6, 7],
"removing objects from the remove set adds them if they're in the keep set"
);
array1.removeObject(3);
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 6, 7],
'removing objects from the keep array removes them from the computed array'
);
array1.pushObject(5);
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 6, 7],
'objects added to the keep array that are in the remove array are not added to the computed array'
);
array1.pushObject(22);
assert.deepEqual(
obj.get('diff').sort((x, y) => x - y),
[1, 2, 6, 7, 22],
'objects added to the keep array not in the remove array are added to the computed array'
);
}
}
);
class SortWithSortPropertiesTestCase extends AbstractTestCase {
beforeEach() {
this.obj = this.buildObject();
}
afterEach() {
if (this.obj) {
this.cleanupObject();
}