@@ -201,15 +201,19 @@ describe('Cloud Code', () => {
201
201
done ( ) ;
202
202
}
203
203
} ) ;
204
- it ( 'beforeFind can return object without DB operation' , async ( ) => {
204
+ // Helper function to set up database spy
205
+ function setupDatabaseSpy ( ) {
205
206
const config = Config . get ( 'test' ) ;
206
207
const databaseAdapter = config . database . adapter ;
207
- const findSpy = spyOn ( databaseAdapter , 'find' ) . and . callThrough ( ) ;
208
+ return spyOn ( databaseAdapter , 'find' ) . and . callThrough ( ) ;
209
+ }
210
+
211
+ it ( 'beforeFind can return object without DB operation' , async ( ) => {
212
+ const findSpy = setupDatabaseSpy ( ) ;
208
213
209
214
Parse . Cloud . beforeFind ( 'TestObject' , ( ) => {
210
215
return new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ;
211
216
} ) ;
212
-
213
217
Parse . Cloud . afterFind ( 'TestObject' , req => {
214
218
expect ( req . objects ) . toBeDefined ( ) ;
215
219
expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
@@ -218,21 +222,16 @@ describe('Cloud Code', () => {
218
222
const newObj = await new Parse . Query ( 'TestObject' ) . first ( ) ;
219
223
expect ( newObj . className ) . toBe ( 'TestObject' ) ;
220
224
expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
221
-
222
225
expect ( findSpy ) . not . toHaveBeenCalled ( ) ;
223
-
224
226
await newObj . save ( ) ;
225
227
} ) ;
226
228
227
229
it ( 'beforeFind can return array of objects without DB operation' , async ( ) => {
228
- const config = Config . get ( 'test' ) ;
229
- const databaseAdapter = config . database . adapter ;
230
- const findSpy = spyOn ( databaseAdapter , 'find' ) . and . callThrough ( ) ;
230
+ const findSpy = setupDatabaseSpy ( ) ;
231
231
232
232
Parse . Cloud . beforeFind ( 'TestObject' , ( ) => {
233
233
return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
234
234
} ) ;
235
-
236
235
Parse . Cloud . afterFind ( 'TestObject' , req => {
237
236
expect ( req . objects ) . toBeDefined ( ) ;
238
237
expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
@@ -241,64 +240,50 @@ describe('Cloud Code', () => {
241
240
const newObj = await new Parse . Query ( 'TestObject' ) . first ( ) ;
242
241
expect ( newObj . className ) . toBe ( 'TestObject' ) ;
243
242
expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
244
-
245
243
expect ( findSpy ) . not . toHaveBeenCalled ( ) ;
246
-
247
244
await newObj . save ( ) ;
248
245
} ) ;
249
246
250
247
it ( 'beforeFind can return object for get query without DB operation' , async ( ) => {
251
- const config = Config . get ( 'test' ) ;
252
- const databaseAdapter = config . database . adapter ;
253
- const findSpy = spyOn ( databaseAdapter , 'find' ) . and . callThrough ( ) ;
248
+ const findSpy = setupDatabaseSpy ( ) ;
254
249
255
250
Parse . Cloud . beforeFind ( 'TestObject' , ( ) => {
256
251
return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
257
252
} ) ;
258
-
259
253
Parse . Cloud . afterFind ( 'TestObject' , req => {
260
254
expect ( req . objects ) . toBeDefined ( ) ;
261
255
expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
262
256
} ) ;
263
257
264
258
const testObj = new Parse . Object ( 'TestObject' ) ;
265
259
await testObj . save ( ) ;
266
-
267
260
findSpy . calls . reset ( ) ;
268
261
269
262
const newObj = await new Parse . Query ( 'TestObject' ) . get ( testObj . id ) ;
270
263
expect ( newObj . className ) . toBe ( 'TestObject' ) ;
271
264
expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
272
-
273
265
expect ( findSpy ) . not . toHaveBeenCalled ( ) ;
274
-
275
266
await newObj . save ( ) ;
276
267
} ) ;
277
268
278
269
it ( 'beforeFind can return empty array without DB operation' , async ( ) => {
279
- const config = Config . get ( 'test' ) ;
280
- const databaseAdapter = config . database . adapter ;
281
- const findSpy = spyOn ( databaseAdapter , 'find' ) . and . callThrough ( ) ;
270
+ const findSpy = setupDatabaseSpy ( ) ;
282
271
283
272
Parse . Cloud . beforeFind ( 'TestObject' , ( ) => {
284
273
return [ ] ;
285
274
} ) ;
286
-
287
275
Parse . Cloud . afterFind ( 'TestObject' , req => {
288
276
expect ( req . objects . length ) . toBe ( 0 ) ;
289
277
} ) ;
290
278
291
279
const obj = new Parse . Object ( 'TestObject' ) ;
292
280
await obj . save ( ) ;
293
-
294
281
findSpy . calls . reset ( ) ;
295
282
296
283
const newObj = await new Parse . Query ( 'TestObject' ) . first ( ) ;
297
284
expect ( newObj ) . toBeUndefined ( ) ;
298
-
299
285
expect ( findSpy ) . not . toHaveBeenCalled ( ) ;
300
286
} ) ;
301
-
302
287
const { maybeRunAfterFindTrigger } = require ( '../lib/triggers' ) ;
303
288
304
289
describe ( 'maybeRunAfterFindTrigger - direct function tests' , ( ) => {
0 commit comments