Skip to content

fix: Handle increment on nested fields deep level #1301

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

Merged
merged 2 commits into from
Feb 23, 2021
Merged
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
14 changes: 14 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ describe('Parse Object', () => {
assert.equal(result.get('objectField').number, 20);
});

it('can increment nested four levels', async () => {
const obj = new TestObject({ a: { b: { c: { d: 1 } } } });
await obj.save();
obj.increment('a.b.c.d');
assert.strictEqual(obj.get('a').b.c.d, 2);

await obj.save();
assert.strictEqual(obj.get('a').b.c.d, 2);

const query = new Parse.Query(TestObject);
const result = await query.get(obj.id);
assert.strictEqual(result.get('a').b.c.d, 2);
});

it('can increment nested field and retain full object', async () => {
const obj = new Parse.Object('TestIncrementObject');
obj.set('objectField', { number: 5, letter: 'a' });
Expand Down
6 changes: 5 additions & 1 deletion src/ObjectStateMutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ export function estimateAttributes(
data[first] = { ...serverData[first] };
let object = { ...data };
for (let i = 0; i < fields.length - 1; i++) {
object = object[fields[i]];
const key = fields[i];
if (!(key in object)) {
object[key] = {};
}
object = object[key];
}
object[last] = pendingOps[i][attr].applyTo(object[last]);
} else {
Expand Down
15 changes: 14 additions & 1 deletion src/__tests__/ObjectStateMutations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('ObjectStateMutations', () => {
});

it('can estimate attributes for nested documents', () => {
const serverData = { objectField: { counter: 10, letter: 'a' } };
let serverData = { objectField: { counter: 10, letter: 'a' } };
let pendingOps = [{ 'objectField.counter': new ParseOps.IncrementOp(2) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
Expand All @@ -182,6 +182,19 @@ describe('ObjectStateMutations', () => {
letter: 'a',
},
});
serverData = {};
pendingOps = [{ 'objectField.subField.subField.counter': new ParseOps.IncrementOp(20) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
objectField: {
subField: {
subField: {
counter: 20,
},
},
},
});
});

it('can commit changes from the server', () => {
Expand Down