Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix snapshot/restore methods to separate keys and values #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,22 +721,15 @@ export class BufferedChangeset implements IChangeset {
return this;
}

private getChangesFromSnapshot(changes: Changes) {
return keys(changes).reduce((newObj, key) => {
newObj[key] = this.getChangeForProp(changes[key]);
return newObj;
}, {} as Changes);
}

private getChangeForProp(value: any) {
private getChangesFromSnapshot(value: any) {
if (!isObject(value)) {
return new Change(value);
}

return keys(value).reduce((newObj, key) => {
newObj[key] = this.getChangeForProp(value[key]);
newObj[key] = this.getChangesFromSnapshot(value[key]);
return newObj;
}, {} as Changes);
}, Object.create(value) as Changes);
}

/**
Expand Down
13 changes: 3 additions & 10 deletions src/validated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,22 +527,15 @@ export class ValidatedChangeset {
return this;
}

private getChangesFromSnapshot(changes: Changes) {
return keys(changes).reduce((newObj, key) => {
newObj[key] = this.getChangeForProp(changes[key]);
return newObj;
}, {} as Changes);
}

private getChangeForProp(value: any) {
private getChangesFromSnapshot(value: any) {
if (!isObject(value)) {
return new Change(value);
}

return keys(value).reduce((newObj, key) => {
newObj[key] = this.getChangeForProp(value[key]);
newObj[key] = this.getChangesFromSnapshot(value[key]);
return newObj;
}, {} as Changes);
}, Object.create(value) as Changes);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3277,6 +3277,32 @@ describe('Unit | Utility | changeset', () => {
]);
});

it('#restore restores a snapshot of the changeset when nested value is a class instance', () => {
class Country {
constructor(public id: string, public name: string) {}
}
let us = new Country('US', 'United States');
let prk = new Country('PRK', 'North Korea');
let aus = new Country('AUS', 'Australia');

let user = {
name: 'Adam',
address: { country: us }
};
let changeset = Changeset(user);
changeset.set('name', 'Jim Bob');
changeset.set('address.country', prk);
let snapshot1 = changeset.snapshot();

changeset.set('name', 'Poteto');
changeset.set('address.country', aus);

changeset.restore(snapshot1);

expect(changeset.get('name')).toBe('Jim Bob');
expect(changeset.get('address.country')).toStrictEqual(prk);
});

/**
* #cast
*/
Expand Down
36 changes: 31 additions & 5 deletions test/validated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2455,13 +2455,13 @@ describe('Unit | Utility | validation changeset', () => {
expect(snapshot).toEqual(expectedResult);
});

// /**
// * #restore
// */
/**
* #restore
*/

// it('#restore restores a snapshot of the changeset', () => {
// let dummyChangesetA = Changeset(dummyModel));
// let dummyChangesetB = Changeset(dummyModel));
// let dummyChangesetA = Changeset(dummyModel);
// let dummyChangesetB = Changeset(dummyModel);
// dummyChangesetA.set('name', 'Pokemon Go');
// dummyChangesetA.set('password', false);
// let snapshot = dummyChangesetA.snapshot();
Expand Down Expand Up @@ -2497,6 +2497,32 @@ describe('Unit | Utility | validation changeset', () => {
// ]);
// });

it('#restore restores a snapshot of the changeset when nested value is a class instance', () => {
class Country {
constructor(public id: string, public name: string) {}
}
let us = new Country('US', 'United States');
let prk = new Country('PRK', 'North Korea');
let aus = new Country('AUS', 'Australia');

let user = {
name: 'Adam',
address: { country: us }
};
let changeset = Changeset(user);
changeset.set('name', 'Jim Bob');
changeset.set('address.country', prk);
let snapshot1 = changeset.snapshot();

changeset.set('name', 'Poteto');
changeset.set('address.country', aus);

changeset.restore(snapshot1);

expect(changeset.get('name')).toBe('Jim Bob');
expect(changeset.get('address.country')).toStrictEqual(prk);
});

// /**
// * #cast
// */
Expand Down