Skip to content

Commit 21637d2

Browse files
committed
Fix snapshot/restore methods to separate keys and values
1 parent 6dd774f commit 21637d2

File tree

4 files changed

+63
-25
lines changed

4 files changed

+63
-25
lines changed

src/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -721,22 +721,15 @@ export class BufferedChangeset implements IChangeset {
721721
return this;
722722
}
723723

724-
private getChangesFromSnapshot(changes: Changes) {
725-
return keys(changes).reduce((newObj, key) => {
726-
newObj[key] = this.getChangeForProp(changes[key]);
727-
return newObj;
728-
}, {} as Changes);
729-
}
730-
731-
private getChangeForProp(value: any) {
724+
private getChangesFromSnapshot(value: any) {
732725
if (!isObject(value)) {
733726
return new Change(value);
734727
}
735728

736729
return keys(value).reduce((newObj, key) => {
737-
newObj[key] = this.getChangeForProp(value[key]);
730+
newObj[key] = this.getChangesFromSnapshot(value[key]);
738731
return newObj;
739-
}, {} as Changes);
732+
}, Object.create(value) as Changes);
740733
}
741734

742735
/**

src/validated.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,22 +527,15 @@ export class ValidatedChangeset {
527527
return this;
528528
}
529529

530-
private getChangesFromSnapshot(changes: Changes) {
531-
return keys(changes).reduce((newObj, key) => {
532-
newObj[key] = this.getChangeForProp(changes[key]);
533-
return newObj;
534-
}, {} as Changes);
535-
}
536-
537-
private getChangeForProp(value: any) {
530+
private getChangesFromSnapshot(value: any) {
538531
if (!isObject(value)) {
539532
return new Change(value);
540533
}
541534

542535
return keys(value).reduce((newObj, key) => {
543-
newObj[key] = this.getChangeForProp(value[key]);
536+
newObj[key] = this.getChangesFromSnapshot(value[key]);
544537
return newObj;
545-
}, {} as Changes);
538+
}, Object.create(value) as Changes);
546539
}
547540

548541
/**

test/index.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,6 +3277,32 @@ describe('Unit | Utility | changeset', () => {
32773277
]);
32783278
});
32793279

3280+
it('#restore restores a snapshot of the changeset when nested value is a class instance', () => {
3281+
class Country {
3282+
constructor(public id: string, public name: string) {}
3283+
}
3284+
let us = new Country('US', 'United States');
3285+
let prk = new Country('PRK', 'North Korea');
3286+
let aus = new Country('AUS', 'Australia');
3287+
3288+
let user = {
3289+
name: 'Adam',
3290+
address: { country: us }
3291+
};
3292+
let changeset = Changeset(user);
3293+
changeset.set('name', 'Jim Bob');
3294+
changeset.set('address.country', prk);
3295+
let snapshot1 = changeset.snapshot();
3296+
3297+
changeset.set('name', 'Poteto');
3298+
changeset.set('address.country', aus);
3299+
3300+
changeset.restore(snapshot1);
3301+
3302+
expect(changeset.get('name')).toBe('Jim Bob');
3303+
expect(changeset.get('address.country')).toStrictEqual(prk);
3304+
});
3305+
32803306
/**
32813307
* #cast
32823308
*/

test/validated.test.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,13 +2455,13 @@ describe('Unit | Utility | validation changeset', () => {
24552455
expect(snapshot).toEqual(expectedResult);
24562456
});
24572457

2458-
// /**
2459-
// * #restore
2460-
// */
2458+
/**
2459+
* #restore
2460+
*/
24612461

24622462
// it('#restore restores a snapshot of the changeset', () => {
2463-
// let dummyChangesetA = Changeset(dummyModel));
2464-
// let dummyChangesetB = Changeset(dummyModel));
2463+
// let dummyChangesetA = Changeset(dummyModel);
2464+
// let dummyChangesetB = Changeset(dummyModel);
24652465
// dummyChangesetA.set('name', 'Pokemon Go');
24662466
// dummyChangesetA.set('password', false);
24672467
// let snapshot = dummyChangesetA.snapshot();
@@ -2497,6 +2497,32 @@ describe('Unit | Utility | validation changeset', () => {
24972497
// ]);
24982498
// });
24992499

2500+
it('#restore restores a snapshot of the changeset when nested value is a class instance', () => {
2501+
class Country {
2502+
constructor(public id: string, public name: string) {}
2503+
}
2504+
let us = new Country('US', 'United States');
2505+
let prk = new Country('PRK', 'North Korea');
2506+
let aus = new Country('AUS', 'Australia');
2507+
2508+
let user = {
2509+
name: 'Adam',
2510+
address: { country: us }
2511+
};
2512+
let changeset = Changeset(user);
2513+
changeset.set('name', 'Jim Bob');
2514+
changeset.set('address.country', prk);
2515+
let snapshot1 = changeset.snapshot();
2516+
2517+
changeset.set('name', 'Poteto');
2518+
changeset.set('address.country', aus);
2519+
2520+
changeset.restore(snapshot1);
2521+
2522+
expect(changeset.get('name')).toBe('Jim Bob');
2523+
expect(changeset.get('address.country')).toStrictEqual(prk);
2524+
});
2525+
25002526
// /**
25012527
// * #cast
25022528
// */

0 commit comments

Comments
 (0)