Skip to content

Commit a776de4

Browse files
committed
moddified triggers to return null when beforeSave
also changed test cases to be more descriptive + added extra test case that returns promise in the beforeSave
1 parent ec1b130 commit a776de4

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

spec/CloudCode.spec.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,41 @@ describe('Cloud Code', () => {
179179
});
180180
});
181181

182-
it('test beforeSave returns value on create and update when beforeSave returns true', done => {
183-
Parse.Cloud.beforeSave('BeforeSaveChanged', function(req) {
184-
req.object.set('foo', 'baz');
182+
it('test beforeSave applies changes when beforeSave returns true', done => {
183+
Parse.Cloud.beforeSave('Insurance', function(req) {
184+
req.object.set('rate', '$49.99/Month');
185185
return true;
186186
});
187187

188-
const obj = new Parse.Object('BeforeSaveChanged');
189-
obj.set('foo', 'bing');
190-
obj.save().then(() => {
191-
expect(obj.get('foo')).toEqual('baz');
192-
obj.set('foo', 'bar');
193-
return obj.save().then(() => {
194-
expect(obj.get('foo')).toEqual('baz');
195-
done();
188+
const insurance = new Parse.Object('Insurance');
189+
insurance.set('rate', '$5.00/Month');
190+
insurance.save().then(insurance => {
191+
expect(insurance.get('rate')).toEqual('$49.99/Month');
192+
done();
193+
});
194+
});
195+
196+
it('test beforeSave applies changes and resolves returned promise', done => {
197+
Parse.Cloud.beforeSave('Insurance', function(req) {
198+
req.object.set('rate', '$49.99/Month');
199+
return new Parse.Query('Pet').get(req.object.get('pet').id).then(pet => {
200+
pet.set('healthy', true);
201+
return pet.save();
202+
});
203+
});
204+
205+
const pet = new Parse.Object('Pet');
206+
pet.set('healthy', false);
207+
pet.save().then(pet => {
208+
const insurance = new Parse.Object('Insurance');
209+
insurance.set('pet', pet);
210+
insurance.set('rate', '$5.00/Month');
211+
insurance.save().then(insurance => {
212+
expect(insurance.get('rate')).toEqual('$49.99/Month');
213+
new Parse.Query('Pet').get(insurance.get('pet').id).then(pet => {
214+
expect(pet.get('healthy')).toEqual(true);
215+
done();
216+
});
196217
});
197218
});
198219
});

src/triggers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,16 @@ export function maybeRunTrigger(
573573
auth
574574
);
575575
}
576+
577+
// beforeSave is expected to return null (nothing)
578+
if (triggerType === Types.beforeSave) {
579+
// if the beforeSave returned a promise, return null after the promise is resolved
580+
if (promise && typeof promise.then === 'function') {
581+
return promise.then(() => null);
582+
}
583+
return null;
584+
}
585+
576586
return promise;
577587
})
578588
.then(success, error);

0 commit comments

Comments
 (0)