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 issue #178 [Cannot get property in an array of objects] #179

Open
wants to merge 3 commits 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/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ export class BufferedChangeset implements IChangeset {

// 'user.name'
const normalizedBaseChanges = normalizeObject(baseChanges);
if (this.isObject(normalizedBaseChanges)) {
if (this.isObject(normalizedBaseChanges) || Array.isArray(normalizedBaseChanges)) {
const result = this.maybeUnwrapProxy(
this.getDeep(normalizedBaseChanges, remaining.join('.'))
);
Expand Down
2 changes: 1 addition & 1 deletion src/validated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ export class ValidatedChangeset {

// 'user.name'
const normalizedBaseChanges = normalizeObject(baseChanges);
if (this.isObject(normalizedBaseChanges)) {
if (this.isObject(normalizedBaseChanges) || Array.isArray(normalizedBaseChanges)) {
const result = this.maybeUnwrapProxy(
this.getDeep(normalizedBaseChanges, remaining.join('.'))
);
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3562,3 +3562,16 @@ describe('Unit | Utility | changeset', () => {
}
});
});
describe('array of objects', () => {
test('changes in nested object are gettable', () => {
const changeset = Changeset({ contacts: [] });
const contactObject = {
name: 'william'
};

changeset.set('contacts', [contactObject]);

expect(changeset.get('contacts.0')).toStrictEqual(contactObject);
expect(changeset.get('contacts.0.name')).toStrictEqual(contactObject.name);
});
});
12 changes: 12 additions & 0 deletions test/validated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ describe('Unit | Utility | validation changeset', () => {
expect(dummyChangeset.change).toEqual(expectedResult);
});

it('#change in array of objects are gettable', () => {
const changeset = Changeset({ contacts: [] });
const contactObject = {
name: 'william'
};

changeset.set('contacts', [contactObject]);

expect(changeset.get('contacts.0')).toStrictEqual(contactObject);
expect(changeset.get('contacts.0.name')).toStrictEqual(contactObject.name);
});

it('#change works with arrays', () => {
const dummyChangeset = Changeset(dummyModel);
const newArray = [...exampleArray, 'new'];
Expand Down