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