it('test beforeSave applies changes when beforeSave returns true', done => { Parse.Cloud.beforeSave('Insurance', function(req) { req.object.set('rate', '$49.99/Month'); return true; }); const insurance = new Parse.Object('Insurance'); insurance.set('rate', '$5.00/Month'); insurance.save().then(insurance => { expect(insurance.get('rate')).toEqual('$49.99/Month'); done(); }); });
it('should have access to context when saving an existing object', async () => { const obj = new TestObject(); await obj.save(null); Parse.Cloud.beforeSave('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); Parse.Cloud.afterSave('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); await obj.save(null, { context: { a: 'a' } }); });
it('can load relative cloud code file', done => { reconfigureServer({ cloud: './spec/cloud/cloudCodeAbsoluteFile.js' }).then( () => { Parse.Cloud.run('cloudCodeInFile', {}).then(result => { expect(result).toEqual( 'It is possible to define cloud code in a file.' ); done(); }); } ); });
it('beforeDeleteFile should call with fileObject', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); Parse.Cloud.beforeDeleteFile((req) => { expect(req.file).toBeInstanceOf(Parse.File); expect(req.file._name).toEqual('popeye.txt'); expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); expect(req.fileSize).toBe(null); }); const file = new Parse.File('popeye.txt'); await file.destroy({ useMasterKey: true }); });
/** * Verifies that an afterSave hook throwing an exception * will not prevent a successful save response from being returned */ it('should succeed on afterSave exception', done => { Parse.Cloud.afterSave('AfterSaveTestClass', function() { throw 'Exception'; }); const AfterSaveTestClass = Parse.Object.extend('AfterSaveTestClass'); const obj = new AfterSaveTestClass(); obj.save().then(done, done.fail); });
it('should have access to context as destroyAll argument', async () => { Parse.Cloud.beforeDelete('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); Parse.Cloud.afterDelete('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); const obj1 = new TestObject(); const obj2 = new TestObject(); await Parse.Object.saveAll([obj1, obj2]); await Parse.Object.destroyAll([obj1, obj2], { context: { a: 'a' } }); });
it('afterSaveFile should throw error', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); Parse.Cloud.afterSaveFile(async () => { throw new Parse.Error(400, 'some-error-message'); }); const filename = 'donald_duck.pdf'; const file = new Parse.File(filename, [1, 2, 3], 'text/plain'); try { await file.save({ useMasterKey: true }); } catch (error) { expect(error.message).toBe('some-error-message'); } });
it('should not run afterLogin on sign up', async done => { let hit = 0; Parse.Cloud.afterLogin(req => { hit++; expect(req.object.get('username')).toEqual('testuser'); }); const user = await Parse.User.signUp('testuser', 'p@ssword'); expect(user).toBeDefined(); expect(hit).toBe(0); done(); });
it('beforeSaveFile should not change file if nothing is returned', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); Parse.Cloud.beforeSaveFile(() => { return; }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); const result = await file.save({ useMasterKey: true }); expect(result).toBe(file); });
it('should have access to context in beforeFind hook', async () => { Parse.Cloud.beforeFind('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); const query = new Parse.Query('TestObject'); return query.find({ context: { a: 'a' } }); });
it('should have access to context when saving a new object', async () => { Parse.Cloud.beforeSave('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); Parse.Cloud.afterSave('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); const obj = new TestObject(); await obj.save(null, { context: { a: 'a' } }); });
it('should have access to context as destroy a object', async () => { Parse.Cloud.beforeDelete('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); Parse.Cloud.afterDelete('TestObject', (req) => { expect(req.context.a).toEqual('a'); }); const obj = new TestObject(); await obj.save(); await obj.destroy({ context: { a: 'a' } }); });
it('can load absolute cloud code file', done => { reconfigureServer({ cloud: __dirname + '/cloud/cloudCodeRelativeFile.js', }).then(() => { Parse.Cloud.run('cloudCodeInFile', {}).then(result => { expect(result).toEqual( 'It is possible to define cloud code in a file.' ); done(); }); }); });