@@ -201,6 +201,58 @@ describe('Cloud Code', () => {
201
201
}
202
202
} ) ;
203
203
204
+ it ( 'beforeFind can return object without DB operation' , async ( ) => {
205
+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
206
+ return new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ;
207
+ } ) ;
208
+ Parse . Cloud . afterFind ( 'beforeFind' , ( ) => {
209
+ throw 'afterFind should not run' ;
210
+ } ) ;
211
+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
212
+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
213
+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
214
+ await newObj . save ( ) ;
215
+ } ) ;
216
+
217
+ it ( 'beforeFind can return array of objects without DB operation' , async ( ) => {
218
+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
219
+ return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
220
+ } ) ;
221
+ Parse . Cloud . afterFind ( 'beforeFind' , ( ) => {
222
+ throw 'afterFind should not run' ;
223
+ } ) ;
224
+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
225
+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
226
+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
227
+ await newObj . save ( ) ;
228
+ } ) ;
229
+
230
+ it ( 'beforeFind can return object for get query without DB operation' , async ( ) => {
231
+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
232
+ return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
233
+ } ) ;
234
+ Parse . Cloud . afterFind ( 'beforeFind' , ( ) => {
235
+ throw 'afterFind should not run' ;
236
+ } ) ;
237
+ const newObj = await new Parse . Query ( 'beforeFind' ) . get ( 'objId' ) ;
238
+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
239
+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
240
+ await newObj . save ( ) ;
241
+ } ) ;
242
+
243
+ it ( 'beforeFind can return empty array without DB operation' , async ( ) => {
244
+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
245
+ return [ ] ;
246
+ } ) ;
247
+ Parse . Cloud . afterFind ( 'beforeFind' , ( ) => {
248
+ throw 'afterFind should not run' ;
249
+ } ) ;
250
+ const obj = new Parse . Object ( 'beforeFind' ) ;
251
+ await obj . save ( ) ;
252
+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
253
+ expect ( newObj ) . toBeUndefined ( ) ;
254
+ } ) ;
255
+
204
256
it ( 'beforeSave rejection with custom error code' , function ( done ) {
205
257
Parse . Cloud . beforeSave ( 'BeforeSaveFailWithErrorCode' , function ( ) {
206
258
throw new Parse . Error ( 999 , 'Nope' ) ;
0 commit comments