@@ -201,6 +201,60 @@ describe('Cloud Code', () => {
201201 done ( ) ;
202202 }
203203 } ) ;
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' , req => {
209+ expect ( req . objects ) . toBeDefined ( ) ;
210+ expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
211+ } ) ;
212+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
213+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
214+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
215+ await newObj . save ( ) ;
216+ } ) ;
217+
218+ it ( 'beforeFind can return array of objects without DB operation' , async ( ) => {
219+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
220+ return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
221+ } ) ;
222+ Parse . Cloud . afterFind ( 'beforeFind' , req => {
223+ expect ( req . objects ) . toBeDefined ( ) ;
224+ expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
225+ } ) ;
226+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
227+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
228+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
229+ await newObj . save ( ) ;
230+ } ) ;
231+
232+ it ( 'beforeFind can return object for get query without DB operation' , async ( ) => {
233+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
234+ return [ new Parse . Object ( 'TestObject' , { foo : 'bar' } ) ] ;
235+ } ) ;
236+ Parse . Cloud . afterFind ( 'beforeFind' , req => {
237+ expect ( req . objects ) . toBeDefined ( ) ;
238+ expect ( req . objects [ 0 ] . get ( 'foo' ) ) . toBe ( 'bar' ) ;
239+ } ) ;
240+ const newObj = await new Parse . Query ( 'beforeFind' ) . get ( 'objId' ) ;
241+ expect ( newObj . className ) . toBe ( 'TestObject' ) ;
242+ expect ( newObj . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
243+ await newObj . save ( ) ;
244+ } ) ;
245+
246+ it ( 'beforeFind can return empty array without DB operation' , async ( ) => {
247+ Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
248+ return [ ] ;
249+ } ) ;
250+ Parse . Cloud . afterFind ( 'beforeFind' , req => {
251+ expect ( req . objects . length ) . toBe ( 0 ) ;
252+ } ) ;
253+ const obj = new Parse . Object ( 'beforeFind' ) ;
254+ await obj . save ( ) ;
255+ const newObj = await new Parse . Query ( 'beforeFind' ) . first ( ) ;
256+ expect ( newObj ) . toBeUndefined ( ) ;
257+ } ) ;
204258
205259 it ( 'beforeFind can return object without DB operation' , async ( ) => {
206260 Parse . Cloud . beforeFind ( 'beforeFind' , ( ) => {
0 commit comments