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 Object.keys returning empty array on unchanged property. close #191. #192

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
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface ProxyHandler {
changes: Record<string, any>;
content: unknown;
content: Content;
proxy: any;
children: Record<string, any>;
safeGet: (obj: any, key: string) => any;
Expand Down
18 changes: 15 additions & 3 deletions src/utils/object-tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@ const objectProxyHandler = {
},

ownKeys(node: ProxyHandler): any {
return Reflect.ownKeys(node.changes);
// If changes have been made reflect the change, otherwise reflect the original content
if (Object.keys(node.changes).length > 0) {
return Reflect.ownKeys(node.changes);
}
return Reflect.ownKeys(node.content);
},

getOwnPropertyDescriptor(node: ProxyHandler, prop: string): any {
return Reflect.getOwnPropertyDescriptor(node.changes, prop);
// If changes have been made reflect the change, otherwise reflect the original content
if (Object.keys(node.changes).length > 0) {
return Reflect.getOwnPropertyDescriptor(node.changes, prop);
}
return Reflect.getOwnPropertyDescriptor(node.content, prop);
},

has(node: ProxyHandler, prop: string): any {
return Reflect.has(node.changes, prop);
// If changes have been made reflect the change, otherwise reflect the original content
if (Object.keys(node.changes).length > 0) {
return Reflect.has(node.changes, prop);
}
return Reflect.has(node.content, prop);
},

set(node: ProxyHandler, key: string, value: unknown): any {
Expand Down
16 changes: 16 additions & 0 deletions test/validated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,22 @@ describe('Unit | Utility | validation changeset', () => {
const expectedResult = get(model, 'foo.bar.dog');
expect(actual).toEqual(expectedResult);
}
{
// Check Object.keys is proxied to the original model when no change has been made
const c = Changeset(model);
const actual = Object.keys(get(c, 'foo'));
const expectedResult = Object.keys(get(model, 'foo'));
expect(actual).toEqual(expectedResult);
}
{
// Check Object.keys is proxied to the change when a change has been made
const c = Changeset(model);
const newFoo = { baz: { dog: new Dog('woof') } };
set(c, 'foo', newFoo);
const actual = Object.keys(get(c, 'foo'));
const expectedResult = Object.keys(newFoo);
expect(actual).toEqual(expectedResult);
}
});

it('#get proxies to underlying array properties', () => {
Expand Down