-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
changeset-test.js
1896 lines (1566 loc) · 68 KB
/
changeset-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 Changeset, { EmberChangeset, Changeset as ChangesetFactory } from 'ember-changeset';
import { settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import EmberObject, {
get,
set,
setProperties,
} from '@ember/object';
import { reads } from '@ember/object/computed';
import ObjectProxy from '@ember/object/proxy';
import { dasherize } from '@ember/string';
import { isPresent, typeOf } from '@ember/utils';
import { next } from '@ember/runloop';
let dummyModel;
let exampleArray = [];
let dummyValidations = {
name(value) {
return isPresent(value) && value.length > 3 || 'too short';
},
password(value) {
return value || ['foo', 'bar'];
},
passwordConfirmation(newValue, _oldValue, { password: changedPassword }, { password }) {
return isPresent(newValue) && (changedPassword === newValue || password === newValue) || "password doesn't match";
},
async(value) {
return Promise.resolve(value);
},
options(value) {
return isPresent(value);
},
org: {
isCompliant(value) {
return !!value;
},
usa: {
ny(value) {
return isPresent(value) || "must be present";
}
}
}
};
function dummyValidator({ key, newValue, oldValue, changes, content }) {
let validatorFn = get(dummyValidations, key);
if (typeOf(validatorFn) === 'function') {
return validatorFn(newValue, oldValue, changes, content);
}
}
module('Unit | Utility | changeset', function(hooks) {
hooks.beforeEach(function() {
let Dummy = EmberObject.extend({
save() {
return Promise.resolve(this);
}
});
dummyModel = Dummy.create({ exampleArray });
});
/**
* #toString
*/
test('content can be an empty hash', async function(assert) {
assert.expect(1);
let emptyObject = Object.create(null);
let dummyChangeset = new Changeset(emptyObject, dummyValidator);
assert.equal(dummyChangeset.toString(), 'changeset:[object Object]');
});
/**
* #error
*/
test('#error returns the error object and keeps changes', async function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
let expectedResult = { name: { validation: 'too short', value: 'a' } };
dummyChangeset.set('name', 'a');
assert.deepEqual(dummyChangeset.error, expectedResult, 'should return error object');
assert.deepEqual(dummyChangeset.get('error').name, expectedResult.name, 'should return nested error');
assert.deepEqual(dummyChangeset.get('error.name'), expectedResult.name, 'should return nested error');
assert.deepEqual(dummyChangeset.change, { name: 'a' }, 'should return change object');
assert.deepEqual(dummyChangeset.get('change.name'), 'a', 'should return nested change');
assert.deepEqual(dummyChangeset.get('change').name, 'a', 'should return nested change');
});
test('can get nested values in the error object', function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
let expectedResult = { validation: 'too short', value: 'a' };
dummyChangeset.set('name', 'a');
assert.deepEqual(dummyChangeset.get('error.name'), expectedResult, 'should return error object for `name` key');
});
/**
* #change
*/
test('#change returns the changes object', async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
let expectedResult = { name: 'a' };
dummyChangeset.set('name', 'a');
assert.deepEqual(get(dummyChangeset, 'change'), expectedResult, 'should return changes object');
});
test('#change supports `undefined`', async function(assert) {
let model = { name: 'a' };
let dummyChangeset = new Changeset(model);
let expectedResult = { name: undefined };
dummyChangeset.set('name', undefined);
assert.deepEqual(
get(dummyChangeset, 'change'), expectedResult,
'property changed to `undefined` should be included in change object'
);
});
test('#change works with arrays', async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
const newArray = [...exampleArray, 'new'];
let expectedResult = { exampleArray: newArray }
dummyChangeset.set('exampleArray', newArray);
assert.deepEqual(get(dummyChangeset, 'change'), expectedResult, 'should return changes object');
});
/**
* #errors
*/
test('#errors returns the error object and keeps changes', async function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
let expectedResult = [{ key: 'name', validation: 'too short', value: 'a' }];
dummyChangeset.set('name', 'a');
assert.deepEqual(dummyChangeset.errors, expectedResult, 'should return errors object');
assert.deepEqual(dummyChangeset.get('errors'), expectedResult, 'should return nested errors');
assert.deepEqual(dummyChangeset.change, { name: 'a' }, 'should return change object');
});
test('can get nested values in the errors object', function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('unknown', 'wat');
dummyChangeset.set('org.usa.ny', '');
dummyChangeset.set('name', '');
let expectedErrors = [
{ key: 'org.usa.ny', validation: 'must be present', value: '' },
{ key: 'name', validation: 'too short', value: '' }
];
assert.deepEqual(
dummyChangeset.get('errors'),
expectedErrors,
'should return errors object for `org.usa.ny` key and `name` key'
);
});
/**
* #changes
*/
/**
* #data
*/
test("data reads the changeset CONTENT", async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
assert.equal(get(dummyChangeset, 'data'), dummyModel, 'should return data');
});
test("data is readonly", async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
try {
set(dummyChangeset, 'data', { foo: 'bar' });
} catch({ message }) {
assert.throws(
({message}) => message === "Cannot set read-only property 'data' on object: changeset:[object Object]",
'should throw error'
);
}
});
/**
* #isValid
*/
/**
* #isInvalid
*/
/**
* #isPristine
*/
test("isPristine returns true if changes are equal to content's values", async function(assert) {
dummyModel.set('name', 'Bobby');
dummyModel.set('thing', 123);
dummyModel.set('nothing', null);
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('name', 'Bobby');
dummyChangeset.set('nothing', null);
assert.ok(dummyChangeset.get('isPristine'), 'should be pristine');
});
test("isPristine returns false if changes are not equal to content's values", async function(assert) {
dummyModel.set('name', 'Bobby');
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('name', 'Bobby');
dummyChangeset.set('thing', 123);
assert.notOk(dummyChangeset.get('isPristine'), 'should not be pristine');
});
test('isPristine works with `null` values', async function(assert) {
dummyModel.set('name', null);
dummyModel.set('age', 15);
let dummyChangeset = new Changeset(dummyModel);
assert.ok(dummyChangeset.get('isPristine'), 'should be pristine');
dummyChangeset.set('name', 'Kenny');
assert.notOk(dummyChangeset.get('isPristine'), 'should not be pristine');
dummyChangeset.set('name', null);
assert.ok(dummyChangeset.get('isPristine'), 'should be pristine');
});
/**
* #isDirty
*/
/**
* #get
*/
test('#get proxies to content', async function(assert) {
set(dummyModel, 'name', 'Jim Bob');
let dummyChangeset = new Changeset(dummyModel);
let result = get(dummyChangeset, 'name');
assert.equal(result, 'Jim Bob', 'should proxy to content');
});
test('#get returns the content when the proxied content is a class', async function(assert) {
class Moment {
constructor(date) {
this.date = date;
}
}
let d = new Date('2015');
let momentInstance = new Moment(d);
let c = new Changeset({
startDate: momentInstance
});
let newValue = c.get('startDate');
assert.deepEqual(newValue, momentInstance, 'correct getter');
assert.ok(newValue instanceof Moment, 'correct instance');
assert.equal(newValue.date, d, 'correct date on moment object');
});
test('#get returns change if present', async function(assert) {
set(dummyModel, 'name', 'Jim Bob');
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name', 'Milton Waddams');
let result = get(dummyChangeset, 'name');
assert.equal(result, 'Milton Waddams', 'should proxy to change');
});
test('#get returns change that is a blank value', async function(assert) {
set(dummyModel, 'name', 'Jim Bob');
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name', '');
let result = get(dummyChangeset, 'name');
assert.equal(result, '', 'should proxy to change');
});
test('#get returns change that has undefined as value', async function(assert) {
set(dummyModel, 'name', 'Jim Bob');
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name', undefined);
let result = get(dummyChangeset, 'name');
assert.equal(result, undefined, 'should proxy to change');
});
test('#get returns change that has array as sibling', async function(assert) {
set(dummyModel, 'name', 'Bob');
set(dummyModel, 'creds', ['burgers']);
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name', 'Burdger');
assert.equal(get(dummyChangeset, 'name'), 'Burdger', 'should proxy name to change');
assert.deepEqual(get(dummyChangeset, 'creds'), ['burgers'], 'should proxy creds to change');
set(dummyChangeset, 'creds', ['fries']);
assert.equal(get(dummyChangeset, 'name'), 'Burdger', 'should proxy name to change');
assert.deepEqual(get(dummyChangeset, 'creds'), ['fries'], 'should proxy creds to change after change');
});
test('nested objects will return correct values', async function(assert) {
set(dummyModel, 'org', {
asia: { sg: '_initial' }, // for the sake of disambiguating nulls
usa: {
ca: null,
ny: null,
ma: { name: null }
}
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
assert.equal(dummyChangeset.get('org.asia.sg'), '_initial', 'returns initial value');
dummyChangeset.set('org.asia.sg', 'sg');
assert.equal(dummyChangeset.get('org.asia.sg'), 'sg', 'returns newly set value');
});
test('nested objects can contain arrays', async function(assert) {
assert.expect(7);
setProperties(dummyModel, {
name: 'Bob',
contact: {
emails: [ 'bob@email.com', 'the_bob@email.com' ]
}
});
assert.deepEqual(dummyModel.get('contact.emails'), [ 'bob@email.com', 'the_bob@email.com' ], 'returns initial model value');
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
assert.equal(dummyChangeset.get('name'), 'Bob', 'returns changeset initial value');
assert.deepEqual(dummyChangeset.get('contact.emails'), [ 'bob@email.com', 'the_bob@email.com' ], 'returns changeset initial value');
dummyChangeset.set('contact.emails', [ 'fred@email.com', 'the_fred@email.com' ]);
assert.deepEqual(dummyChangeset.get('contact.emails'), [ 'fred@email.com', 'the_fred@email.com' ], 'returns changeset changed value');
dummyChangeset.rollback();
assert.deepEqual(dummyChangeset.get('contact.emails'), [ 'bob@email.com', 'the_bob@email.com' ], 'returns changeset rolledback value');
dummyChangeset.set('contact.emails', [ 'fred@email.com', 'the_fred@email.com' ]);
assert.deepEqual(dummyChangeset.get('contact.emails'), [ 'fred@email.com', 'the_fred@email.com' ], 'returns changeset changed value');
dummyChangeset.execute();
assert.deepEqual(dummyModel.contact.emails, [ 'fred@email.com', 'the_fred@email.com' ], 'returns model saved value');
});
test('#getted Object proxies to underlying method', async function(assert) {
class Dog {
constructor(b) {
this.breed = b;
}
bark() {
return `woof i'm a ${this.breed}`;
}
}
let model = {
foo: {
bar: {
dog: new Dog('shiba inu, wow')
}
}
};
{
let c = new Changeset(model);
let actual = c.get('foo.bar.dog').bark();
let expectedResult = "woof i'm a shiba inu, wow";
assert.equal(actual, expectedResult, 'should proxy to underlying method');
}
{
let c = new Changeset(model);
let actual = get(c, 'foo.bar.dog');
let expectedResult = get(model, 'foo.bar.dog');
assert.equal(actual, expectedResult, "using Ember.get will work");
}
{
let c = new Changeset(model);
let actual = get(c, 'foo.bar.dog');
let expectedResult = get(model, 'foo.bar.dog');
assert.equal(actual, expectedResult, "you dont have to use .content");
}
});
/**
* #set
*/
test('#set adds a change if valid', async function(assert) {
let expectedChanges = [{ key: 'name', value: 'foo' }];
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('name', 'foo');
let changes = get(dummyChangeset, 'changes');
assert.equal(dummyModel.name, undefined, 'should keep change');
assert.equal(dummyChangeset.get('name'), 'foo', 'should have new change');
assert.deepEqual(changes, expectedChanges, 'should add change');
});
test('#set Ember.set works', async function(assert) {
let expectedChanges = [{ key: 'name', value: 'foo' }];
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name', 'foo');
assert.equal(dummyModel.name, undefined, 'should keep change');
assert.equal(dummyChangeset.get('name'), 'foo', 'should have new change');
let changes = get(dummyChangeset, 'changes');
assert.deepEqual(changes, expectedChanges, 'should add change');
dummyChangeset.execute();
assert.equal(dummyModel.name, 'foo', 'should be applied');
assert.equal(dummyChangeset.get('name'), 'foo', 'should have new change');
});
test('#set Ember.set doesnt work for nested', async function(assert) {
set(dummyModel, 'name', {});
let dummyChangeset = new Changeset(dummyModel);
set(dummyChangeset, 'name.short', 'foo');
assert.equal(dummyChangeset.get('name.short'), 'foo', 'should have new change');
assert.deepEqual(dummyModel.name.short, 'foo', 'has property on moel already before execute');
let changes = get(dummyChangeset, 'changes');
assert.deepEqual(changes, [], 'no changes with nested key Ember.set');
dummyChangeset.execute();
assert.equal(dummyModel.name.short, 'foo', 'still has property');
});
test('#set adds a change if the key is an object', async function(assert) {
set(dummyModel, 'org', {
usa: {
ny: 'ny',
}
});
let c = new Changeset(dummyModel);
c.set('org.usa.ny', 'foo');
assert.equal(dummyModel.org.usa.ny, 'ny', 'should keep change');
assert.equal(c.get('org.usa.ny'), 'foo', 'should have new change');
let expectedChanges = [{ key: 'org.usa.ny', value: 'foo' }];
let changes = get(c, 'changes');
assert.deepEqual(changes, expectedChanges, 'should add change');
});
test('#set use native setters with nested doesnt work', async function(assert) {
set(dummyModel, 'org', {
usa: {
ny: 'ny',
}
});
let c = new Changeset(dummyModel);
c.org.usa.ny = 'foo';
assert.equal(dummyModel.org.usa.ny, 'foo', 'change applied to model');
assert.equal(c.get('org.usa.ny'), 'foo', 'should have new change');
let changes = get(c, 'changes');
assert.deepEqual(changes, [], 'no changes');
});
test('#set use native setters at single level', async function(assert) {
dummyModel.org = 'ny';
let c = new Changeset(dummyModel);
c.org = 'foo';
assert.equal(dummyModel.org, 'ny', 'should keep change');
assert.equal(c.org, 'foo', 'should have new change');
let changes = get(c, 'changes');
assert.deepEqual(changes, [{ key: 'org', value: 'foo' }], 'should add change');
});
test('#set adds a change if value is an object', async function(assert) {
class Moment {
constructor(date) {
this.date = date;
}
}
let c = new Changeset(dummyModel);
let d = new Date();
let momentInstance = new Moment(d);
c.set('startDate', momentInstance);
let expectedChanges = [{ key: 'startDate', value: momentInstance }];
let changes = get(c, 'changes');
assert.deepEqual(changes, expectedChanges, 'should add change');
let newValue = c.get('startDate');
assert.deepEqual(newValue, momentInstance, 'correct getter');
assert.ok(newValue instanceof Moment, 'correct instance');
assert.equal(newValue.date, d, 'correct date on moment object');
newValue = get(c, 'startDate');
assert.deepEqual(newValue, momentInstance, 'correct getter');
assert.ok(newValue instanceof Moment, 'correct instance');
assert.equal(newValue.date, d, 'correct date on moment object');
});
test('#set supports `undefined`', async function(assert) {
let model = EmberObject.create({ name: 'foo' });
let dummyChangeset = new Changeset(model);
dummyChangeset.set('name', undefined);
assert.equal(
get(dummyChangeset, 'name'),
undefined,
'should return changed value'
);
assert.deepEqual(
get(dummyChangeset, 'changes'),
[{ key: 'name', value: undefined }],
'should add change'
);
});
test('#set does not add a change if new value equals old value', async function(assert) {
let model = EmberObject.create({ name: 'foo' });
let dummyChangeset = new Changeset(model);
dummyChangeset.set('name', 'foo');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[],
'change is not added if new value equals old value'
);
});
test('#set does not add a change if new value equals old value and `skipValidate` is true', async function(assert) {
let model = EmberObject.create({ name: 'foo' });
let dummyChangeset = new Changeset(model, {}, null, {skipValidate: true});
dummyChangeset.set('name', 'foo');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[],
'change is not added if new value equals old value'
);
});
test('#set removes a change if set back to original value', async function(assert) {
let model = EmberObject.create({ name: 'foo' });
let dummyChangeset = new Changeset(model);
dummyChangeset.set('name', 'bar');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[{ key: 'name', value: 'bar' }],
'change is added when value is different than original value'
);
dummyChangeset.set('name', 'foo');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[],
'change is removed when new value matches original value'
);
});
test('#set removes a change if set back to original value when obj is ProxyObject', async function(assert) {
let model = ObjectProxy.create({ content: { name: 'foo' } });
let dummyChangeset = new Changeset(model);
dummyChangeset.set('name', 'bar');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[{ key: 'name', value: 'bar' }],
'change is added when value is different than original value'
);
dummyChangeset.set('name', 'foo');
assert.deepEqual(
get(dummyChangeset, 'changes'),
[],
'change is removed when new value matches original value'
);
});
test('#set does add a change if invalid', async function(assert) {
let expectedErrors = [
{ key: 'name', validation: 'too short', value: 'a' },
{ key: 'password', validation: ['foo', 'bar'], value: false }
];
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('name', 'a');
dummyChangeset.set('password', false);
let changes = get(dummyChangeset, 'changes');
let errors = get(dummyChangeset, 'errors');
let isValid = get(dummyChangeset, 'isValid');
let isInvalid = get(dummyChangeset, 'isInvalid');
let expectedChanges = [{ "key": "name", "value": "a" }, { "key": "password", "value": false }];
assert.deepEqual(changes, expectedChanges, 'should add change');
assert.deepEqual(errors, expectedErrors, 'should have errors');
assert.notOk(isValid, 'should not be valid');
assert.ok(isInvalid, 'should be invalid');
});
test('#set adds the change without validation if `skipValidate` option is set', async function(assert) {
let expectedChanges = [{ key: 'password', value: false }];
let dummyChangeset = new Changeset(dummyModel, dummyValidator, null, {skipValidate: true});
dummyChangeset.set('password', false);
let changes = get(dummyChangeset, 'changes');
assert.deepEqual(changes, expectedChanges, 'should add change');
});
test('#set should remove nested changes when setting roots', async function(assert) {
set(dummyModel, 'org', {
usa: {
ny: 'ny',
ca: 'ca',
},
});
let c = new Changeset(dummyModel);
c.set('org.usa.ny', 'foo');
c.set('org.usa.ca', 'bar');
c.set('org', 'no usa for you')
let actual = get(c, 'changes');
let expectedResult = [{ key: 'org', value: 'no usa for you' }];
assert.deepEqual(actual, expectedResult, 'removes nested changes');
});
test('it works with setProperties', async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
let expectedResult = [
{ key: 'firstName', value: 'foo' },
{ key: 'lastName', value: 'bar' }
];
dummyChangeset.setProperties({ firstName: 'foo', lastName: 'bar' });
assert.deepEqual(get(dummyChangeset, 'changes'), expectedResult, 'precondition');
});
test('it accepts async validations', async function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
let expectedChanges = [{ key: 'async', value: true }];
let expectedError = { async: { validation: 'is invalid', value: 'is invalid' } };
await dummyChangeset.set('async', true);
await settled();
assert.deepEqual(get(dummyChangeset, 'changes'), expectedChanges, 'should set change');
dummyChangeset.set('async', 'is invalid');
await settled();
assert.deepEqual(get(dummyChangeset, 'error'), expectedError, 'should set error');
});
test('it clears errors when setting to original value', async function(assert) {
set(dummyModel, 'name', 'Jim Bob');
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('name', '');
assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.set('name', 'Jim Bob');
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});
test('it clears errors when setting to original value when nested', async function(assert) {
set(dummyModel, 'org', {
usa: { ny: 'vaca' }
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('org.usa.ny', '');
assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.set('org.usa.ny', 'vaca');
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});
test('it clears errors when setting to original value when nested Booleans', async function(assert) {
set(dummyModel, 'org', {
isCompliant: true
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('org.isCompliant', false);
assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.set('org.isCompliant', true);
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});
test('#set should delete nested changes when equal', async function(assert) {
set(dummyModel, 'org', {
usa: { ny: 'i need a vacation' }
});
let c = new Changeset(dummyModel, dummyValidator, dummyValidations);
c.set('org.usa.br', 'whoop');
let actual = get(c, 'change.org.usa.ny');
let expectedResult = undefined;
assert.equal(actual, expectedResult, 'should clear nested key');
});
test('#set works when replacing an Object with an primitive', async function(assert) {
let model = { foo: { bar: { baz: 42 } } };
let c = new Changeset(model);
assert.deepEqual(c.get('foo'), get(model, 'foo'));
c.set('foo', 'not an object anymore');
c.execute();
assert.equal(c.get('foo'), get(model, 'foo'));
});
test('#set works after save', async function(assert) {
dummyModel['org'] = {
usa: {
mn: 'mn',
ny: 'ny'
}
};
const c = new Changeset(dummyModel);
c.set('org.usa.ny', 'NY');
c.set('org.usa.mn', 'MN');
assert.equal(c.get('org.usa.ny'), 'NY');
assert.equal(c.get('org.usa.mn'), 'MN');
assert.equal(dummyModel.org.usa.ny, 'ny');
assert.equal(dummyModel.org.usa.mn, 'mn');
c.save();
assert.equal(c.get('org.usa.ny'), 'NY');
assert.equal(c.get('org.usa.mn'), 'MN');
assert.equal(dummyModel.org.usa.ny, 'NY');
assert.equal(dummyModel.org.usa.mn, 'MN');
c.set('org.usa.ny', 'nil');
assert.equal(c.get('org.usa.ny'), 'nil');
assert.equal(c.get('org.usa.mn'), 'MN');
assert.equal(dummyModel.org.usa.ny, 'NY');
assert.equal(dummyModel.org.usa.mn ,'MN');
c.save();
assert.equal(c.get('org.usa.ny'), 'nil');
assert.equal(c.get('org.usa.mn'), 'MN');
assert.equal(dummyModel.org.usa.ny, 'nil');
assert.equal(dummyModel.org.usa.mn, 'MN');
c.set('org.usa.ny', 'nil2');
c.set('org.usa.mn', 'undefined');
assert.equal(c.get('org.usa.ny'), 'nil2');
assert.equal(c.get('org.usa.mn'), 'undefined');
assert.equal(dummyModel.org.usa.ny, 'nil');
assert.equal(dummyModel.org.usa.mn, 'MN');
c.save();
assert.equal(c.get('org.usa.ny'), 'nil2');
assert.equal(c.get('org.usa.mn'), 'undefined');
assert.equal(dummyModel.org.usa.ny, 'nil2');
assert.equal(dummyModel.org.usa.mn, 'undefined');
});
/**
* #prepare
*/
test('#prepare provides callback to modify changes', async function(assert) {
let date = new Date();
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('first_name', 'foo');
dummyChangeset.set('date_of_birth', date);
dummyChangeset.prepare((changes) => {
let modified = {};
for (let key in changes) {
modified[dasherize(key)] = changes[key];
}
return modified;
});
let changeKeys = get(dummyChangeset, 'changes').map((change) => get(change, 'key'));
assert.deepEqual(changeKeys, ['first-name', 'date-of-birth'], 'should update changes');
dummyChangeset.execute();
assert.equal(get(dummyModel, 'first-name'), 'foo', 'should update changes');
assert.equal(get(dummyModel, 'date-of-birth'), date, 'should update changes');
});
test('#prepare throws if callback does not return object', async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('first_name', 'foo');
try {
dummyChangeset.prepare(() => { return 'foo'; });
} catch({ message }) {
assert.throws(
({message}) => message === "Assertion Failed: Callback to `changeset.prepare` must return an object",
'should throw error'
);
}
});
/**
* #execute
*/
test('#execute applies changes to content if valid', async function(assert) {
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('name', 'foo');
assert.equal(get(dummyModel, 'name'), undefined, 'precondition');
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
dummyChangeset.execute();
assert.equal(get(dummyModel, 'name'), 'foo', 'should apply changes');
});
test('#execute does not apply changes to content if invalid', async function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('name', 'a');
assert.equal(get(dummyModel, 'name'), undefined, 'precondition');
assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.execute();
assert.equal(get(dummyModel, 'name'), undefined, 'should not apply changes');
});
test('#execute does not remove original nested objects', function(a) {
class DogTag {}
let dog = {};
dog.info = new DogTag;
dog.info.name = 'mishka';
dog.info.breed = 'husky';
let c = new Changeset(dog);
c.set('info.name', 'laika');
c.execute();
let condition = get(dog, 'info') instanceof DogTag;
a.ok(condition, 'should not remove original object');
});
[
{
model: () => ({ org: { usa: { ny: '', ca: '' } } }),
setCalls: [
['org.usa.ny', 'foo'],
['org.usa.ca', 'bar'],
['org', 'no usa for you'],
],
result: () => ({ org: 'no usa for you' }),
},
{
model: () => ({ org: { usa: { ny: '', ca: '' } } }),
setCalls: [
['org.usa.ny', 'foo'],
['org', 'no usa for you'],
['org.usa.ca', 'bar'],
],
result: () => ({ org: { usa: { ca: 'bar', ny: '' } } }),
},
{
model: () => ({ org: { usa: { ny: '', ca: '' } } }),
setCalls: [
['org', 'no usa for you'],
['org.usa.ny', 'foo'],
['org.usa.ca', 'bar'],
],
result: () => ({ org: { usa: { ny: 'foo', ca: 'bar' } } }),
},
].forEach(({ model, setCalls, result }, i) => {
test(`#execute - table-driven test ${i+1}`, async function(assert) {
let m = model();
let c = new Changeset(m);
setCalls.forEach(([k, v]) => c.set(k, v));
c.execute();
let actual = m;
let expectedResult = result();
assert.deepEqual(actual, expectedResult, `table driven test ${i+1}`);
});
});
test('it works with nested keys', async function(assert) {
let expectedResult = {
org: {
asia: { sg: 'sg' },
usa: {
ca: 'ca',
ny: 'ny',
ma: { name: 'Massachusetts' }
}
}
};
set(dummyModel, 'org', {
asia: { sg: null },
usa: {
ca: null,
ny: null,
ma: { name: null }
}
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('org.asia.sg', 'sg');
dummyChangeset.set('org.usa.ca', 'ca');
dummyChangeset.set('org.usa.ny', 'ny');
dummyChangeset.set('org.usa.ma', { name: 'Massachusetts' });
dummyChangeset.execute();
assert.deepEqual(dummyChangeset.change, expectedResult, 'should have correct shape');
assert.deepEqual(dummyChangeset._content.org, expectedResult.org, 'should have correct shape');
assert.deepEqual(get(dummyModel, 'org'), expectedResult.org, 'should set value');
});
/**
* #save
*/
test('#save proxies to content', async function(assert) {
let result;
let options;
let done = assert.async();
set(dummyModel, 'save', (dummyOptions) => {
result = 'ok';
options = dummyOptions;
return Promise.resolve('saveResult');
});
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('name', 'foo');
assert.equal(result, undefined, 'precondition');
let promise = dummyChangeset.save('test options');
assert.equal(result, 'ok', 'should save');
assert.deepEqual(get(dummyChangeset, 'change'), { name: 'foo' }, 'should save');
assert.equal(options, 'test options', 'should proxy options when saving');
assert.ok(!!promise && typeof promise.then === 'function', 'save returns a promise');
promise.then((saveResult) => {
assert.equal(saveResult, 'saveResult', 'save proxies to save promise of content');
}).finally(() => done());
});
test('#save handles non-promise proxy content', async function(assert) {
let result;
let options;
let done = assert.async();
set(dummyModel, 'save', (dummyOptions) => {
result = 'ok';
options = dummyOptions;
return Promise.resolve('saveResult');
});
let dummyChangeset = new Changeset(dummyModel);
dummyChangeset.set('name', 'foo');
assert.equal(result, undefined, 'precondition');
let promise = dummyChangeset.save('test options');
assert.equal(result, 'ok', 'should save');
assert.equal(options, 'test options', 'should proxy options when saving');
assert.ok(!!promise && typeof promise.then === 'function', 'save returns a promise');
promise.then((saveResult) => {
assert.equal(saveResult, 'saveResult', 'save proxies to save promise of content');
}).finally(() => done());
});
test('#save handles rejected proxy content', async function(assert) {
assert.expect(1);
let done = assert.async();
let dummyChangeset = new Changeset(dummyModel);
assert.expect(1);