Skip to content

Commit

Permalink
bugfix: execute does not work with new ValidatedChangeset (#169)
Browse files Browse the repository at this point in the history
* Improve new validated changeset

* prettier

* fix lint

* properly handle deep merging with structuredClone

* add comment

* prettier
  • Loading branch information
snewcomer authored Apr 17, 2022
1 parent 548234c commit 58b5ae9
Show file tree
Hide file tree
Showing 9 changed files with 1,661 additions and 249 deletions.
1,467 changes: 1,467 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@
},
"volta": {
"node": "14.19.1"
},
"dependencies": {
"@types/ungap__structured-clone": "^0.3.0",
"@ungap/structured-clone": "^0.3.4",
"i": "^0.3.7",
"npm": "^8.7.0"
}
}
1 change: 1 addition & 0 deletions src/utils/get-key-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PublicErrors } from '../types';

/**
* traverse through target and return leaf nodes with `value` property and key as 'person.name'
* Only detects key paths with Changes
*
* @method getKeyValues
* @return {Array} [{ 'person.name': value }]
Expand Down
2 changes: 0 additions & 2 deletions src/utils/merge-nested.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const { keys } = Object;
/**
* Given an array of objects, merge their keys into a new object and
* return the new object.
*
* This function merges using `setNestedProperty`.
*/
export default function mergeNested<T>(
...objects: Array<{ [key: string]: T }>
Expand Down
9 changes: 7 additions & 2 deletions src/validated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import mergeDeep from './utils/merge-deep';
import setDeep from './utils/set-deep';
import getDeep, { getSubObject } from './utils/get-deep';
import { objectToArray, arrayToObject } from './utils/array-object';
import structuredClone from '@ungap/structured-clone';

import {
Changes,
Expand Down Expand Up @@ -303,7 +304,10 @@ export class ValidatedChangeset {

// we want mutation on original object
// @tracked
this[CONTENT] = this.mergeDeep(content, changes);
this[CONTENT] = this.mergeDeep(content, changes, {
safeGet: this.safeGet,
safeSet: this.safeSet
});
}

// trigger any registered callbacks by same keyword as method name
Expand Down Expand Up @@ -387,7 +391,8 @@ export class ValidatedChangeset {
const changes = this[CHANGES];
const content = this[CONTENT];

return cb({ ...normalizeObject(content), ...normalizeObject(changes) });
// return an object that does not poison original model and provides user with full set of data + changes to validate
return cb(this.mergeDeep(structuredClone(content), changes));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/utils/get-key-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('Unit | Utility | getKeyValues', function() {

it('it works with nested keys', () => {
const result = getKeyValues({
lastLogin: new Date(), // not a change
team: {
name: 'scoot'
},
user: {
firstName: { [VALUE]: 'Michael' },
lastName: { [VALUE]: 'Bolton' },
Expand Down
10 changes: 10 additions & 0 deletions test/utils/merge-deep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('Unit | Utility | merge deep', () => {
}

foo = { baz: 'ba' };
jam = new Change(['Jull', 'Olafur']);
}

class B extends A {
Expand All @@ -117,5 +118,14 @@ describe('Unit | Utility | merge deep', () => {
expect(value.boo).toBe('doo');
expect(value.other).toBe('Ivan');
expect(value.foo).toEqual({ baz: 'bar' });
expect(value.jam).toEqual(new Change(['Jull', 'Olafur']));

expect(objA.jam).toEqual(new Change(['Jull', 'Olafur']));
expect(objA.foo).toEqual({ baz: 'bar' });
expect(objA._boo).toEqual('doo');
expect(objB.boo).toEqual(new Change('doo'));
expect(objA.other).toEqual('Ivan');
expect(objB.foo).toEqual({ baz: new Change('bar') });
expect(objA.other).toEqual('Ivan');
});
});
4 changes: 2 additions & 2 deletions test/utils/normalize-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('Unit | Utility | normalize object', () => {
});

it('it returns multiple values from nested', () => {
const objA = { name: new Change('Ivan'), foo: new Change('bar') };
const objA = { name: new Change('Ivan'), foo: new Change('bar'), bar: 'zoo' };
const value = normalizeObject(objA);

expect(value).toEqual({ name: 'Ivan', foo: 'bar' });
expect(value).toEqual({ name: 'Ivan', foo: 'bar', bar: 'zoo' });
});

it('it returns for deep nested', () => {
Expand Down
Loading

0 comments on commit 58b5ae9

Please sign in to comment.