Skip to content
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
46 changes: 46 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,52 @@ const mockAdapter = {
};

describe('Cloud Code', () => {
describe('nested sibling snapshot after Increment', () => {
for (const before of [false, true]) {
it(`matches stored nested siblings after Increment with preceding value=${before}`, async () => {
const initial = { views: 5, untouched: { enabled: true } };
const obj = await new Parse.Object('IncrementSnapshot').save({ stats: initial });
const seen = [];
Parse.Cloud.afterSave('IncrementSnapshot', ({ object }) => {
seen.push(object.get('stats'));
});
const body = {};
if (before) {
body['stats.title'] = 'before';
}
body['stats.views'] = { __op: 'Increment', amount: 1 };
body['stats.label'] = 'after';
body['stats.active'] = false;
body['stats.tags'] = ['a', 'b'];
body['stats.metadata'] = { source: 'rest', priority: 2 };
const response = await request({
method: 'PUT',
url: `http://localhost:8378/1/classes/IncrementSnapshot/${obj.id}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body,
});
const stored = await new Parse.Query('IncrementSnapshot').get(obj.id, { useMasterKey: true });
const expected = {
...initial,
views: 6,
...(before ? { title: 'before' } : {}),
label: 'after',
active: false,
tags: ['a', 'b'],
metadata: { source: 'rest', priority: 2 },
};
expect(stored.get('stats')).toEqual(expected);
expect(seen.length).toBe(1);
expect(seen[0]).toEqual(stored.get('stats'));
expect(response.data.stats).toEqual(stored.get('stats'));
});
}
});

it('can load absolute cloud code file', done => {
reconfigureServer({
cloud: __dirname + '/cloud/cloudCodeRelativeFile.js',
Expand Down
37 changes: 37 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@ const validatorFail = () => {
};

describe('ParseLiveQuery', function () {
it('nested sibling snapshot after Increment matches stored data without afterSave', async () => {
const obj = await new TestObject().save({ stats: { views: 5, untouched: 'kept' } });
const query = new Parse.Query(TestObject);
query.equalTo('objectId', obj.id);
const subscription = await query.subscribe();
try {
const updated = new Promise(resolve => subscription.on('update', resolve));
await request({
method: 'PUT',
url: `http://localhost:8378/1/classes/TestObject/${obj.id}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
'stats.views': { __op: 'Increment', amount: 1 },
'stats.title': 'after',
'stats.active': false,
'stats.metadata': { source: 'rest' },
},
});
const eventObject = await updated;
const stored = await new Parse.Query(TestObject).get(obj.id, { useMasterKey: true });
expect(stored.get('stats')).toEqual({
views: 6,
untouched: 'kept',
title: 'after',
active: false,
metadata: { source: 'rest' },
});
expect(eventObject.get('stats')).toEqual(stored.get('stats'));
} finally {
await subscription.unsubscribe();
}
});

beforeEach(() => {
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null);
});
Expand Down
6 changes: 4 additions & 2 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2512,10 +2512,12 @@ function getSaveJSON() {
// The save response handling of the SDK, estimating the attributes once instead of
// once per returned field
function handleSaveResponse(response, status) {
let attributes;
// The SDK pops pending operations before merging returned subdocuments. Capture
// their estimate first so a partial operation result keeps sibling writes.
const attributes = estimateAttributes.call(this);
Object.defineProperty(this, 'attributes', {
configurable: true,
get: () => (attributes ??= estimateAttributes.call(this)),
get: () => attributes,
});
try {
Object.getPrototypeOf(this)._handleSaveResponse.call(this, response, status);
Expand Down