Skip to content

Commit a1339c4

Browse files
committed
1 parent 57ac1e2 commit a1339c4

7 files changed

Lines changed: 511 additions & 101 deletions

File tree

‎spec/ParseAPI.spec.js‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,113 @@ describe('miscellaneous', () => {
11001100
expect(body.updatedAt).not.toBeUndefined();
11011101
});
11021102

1103+
describe('save response with an operation that leaves the field unchanged', () => {
1104+
for (const trigger of ['beforeSave', 'afterSave']) {
1105+
it(`returns the stored values with ${trigger}`, async () => {
1106+
Parse.Cloud[trigger]('Score', () => {});
1107+
const obj = await new Parse.Object('Score').save({
1108+
counter: 10,
1109+
members: ['alice', 'bob'],
1110+
removed: ['x'],
1111+
stats: { views: 5, label: 'l' },
1112+
});
1113+
const response = await request({
1114+
method: 'PUT',
1115+
url: `http://localhost:8378/1/classes/Score/${obj.id}`,
1116+
headers,
1117+
body: JSON.stringify({
1118+
counter: { __op: 'Increment', amount: 0 },
1119+
members: { __op: 'AddUnique', objects: ['alice'] },
1120+
removed: { __op: 'Remove', objects: ['y'] },
1121+
'stats.views': { __op: 'Increment', amount: 0 },
1122+
}),
1123+
});
1124+
expect(response.data).toEqual({
1125+
updatedAt: jasmine.any(String),
1126+
counter: 10,
1127+
members: ['alice', 'bob'],
1128+
removed: ['x'],
1129+
// The final object of afterSave has the whole field
1130+
stats: trigger === 'afterSave' ? { views: 5, label: 'l' } : { views: 5 },
1131+
});
1132+
});
1133+
}
1134+
1135+
it('returns the stored parent of a dot notation key set to its stored value with afterSave', async () => {
1136+
Parse.Cloud.afterSave('Score', () => {});
1137+
const obj = await new Parse.Object('Score').save({ stats: { views: 5, label: 'l' } });
1138+
const response = await request({
1139+
method: 'PUT',
1140+
url: `http://localhost:8378/1/classes/Score/${obj.id}`,
1141+
headers,
1142+
body: JSON.stringify({ 'stats.label': 'l' }),
1143+
});
1144+
expect(response.data).toEqual({
1145+
updatedAt: jasmine.any(String),
1146+
stats: { views: 5, label: 'l' },
1147+
});
1148+
});
1149+
1150+
it('returns a field that beforeSave unsets as deleted', async () => {
1151+
const obj = await new Parse.Object('Score').save({ counter: 10, label: 'l' });
1152+
Parse.Cloud.beforeSave('Score', ({ object }) => {
1153+
object.unset('label');
1154+
});
1155+
const response = await request({
1156+
method: 'PUT',
1157+
url: `http://localhost:8378/1/classes/Score/${obj.id}`,
1158+
headers,
1159+
body: JSON.stringify({ counter: { __op: 'Increment', amount: 1 }, label: 'x' }),
1160+
});
1161+
expect(response.data).toEqual({
1162+
updatedAt: jasmine.any(String),
1163+
counter: 11,
1164+
label: { __op: 'Delete' },
1165+
});
1166+
});
1167+
1168+
it('passes the stored values to afterSave', async () => {
1169+
let object;
1170+
Parse.Cloud.afterSave('Score', request => {
1171+
object = request.object;
1172+
});
1173+
const item = await new Parse.Object('Score').save({ members: ['alice', 'bob'], counter: 10 });
1174+
item.addUnique('members', 'alice');
1175+
item.increment('counter', 0);
1176+
await item.save();
1177+
expect(object.get('members')).toEqual(['alice', 'bob']);
1178+
expect(object.get('counter')).toBe(10);
1179+
});
1180+
1181+
it('keeps the stored values in the JavaScript SDK', async () => {
1182+
Parse.Cloud.beforeSave('Score', () => {});
1183+
const item = await new Parse.Object('Score').save({ members: ['alice', 'bob'], counter: 10 });
1184+
item.addUnique('members', 'alice');
1185+
item.increment('counter', 0);
1186+
await item.save();
1187+
expect(item.get('members')).toEqual(['alice', 'bob']);
1188+
expect(item.get('counter')).toBe(10);
1189+
});
1190+
1191+
for (const [name, apply] of Object.entries({
1192+
'dot notation increment': item => item.increment('stats.views', 0),
1193+
'dot notation set': item => item.set('stats.label', 'l'),
1194+
})) {
1195+
it(`does not overwrite stored values after a ${name} on a later save in the JavaScript SDK`, async () => {
1196+
Parse.Cloud.afterSave('Score', () => {});
1197+
const obj = await new Parse.Object('Score').save({ stats: { views: 5, label: 'l' } });
1198+
const item = Parse.Object.extend('Score').createWithoutData(obj.id);
1199+
apply(item);
1200+
await item.save();
1201+
expect(item.dirtyKeys()).toEqual([]);
1202+
item.set('name', 'later');
1203+
await item.save();
1204+
await obj.fetch();
1205+
expect(obj.get('stats')).toEqual({ views: 5, label: 'l' });
1206+
});
1207+
}
1208+
});
1209+
11031210
it('test cloud function error handling', done => {
11041211
// Register a function which will fail
11051212
Parse.Cloud.define('willFail', () => {

‎spec/schemas.spec.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,8 @@ describe('schemas', () => {
28472847
await obj.save({ key: 1 }, { useMasterKey: true });
28482848
obj.increment('key', 10);
28492849
const objectAgain = await obj.save();
2850-
expect(objectAgain.get('key')).toBeUndefined();
2850+
// The SDK applies the increment to an empty value
2851+
expect(objectAgain.get('key')).toBe(10);
28512852
const stored = await new Parse.Query('AClass').get(obj.id, { useMasterKey: true });
28522853
expect(stored.get('key')).toBe(11);
28532854
});

0 commit comments

Comments
 (0)