@@ -1306,4 +1306,134 @@ describe('beforeFind hooks', () => {
13061306 done ( ) ;
13071307 } ) ;
13081308 } ) ;
1309- } )
1309+ } ) ;
1310+
1311+ describe ( 'afterFind hooks' , ( ) => {
1312+ it ( 'should add afterFind trigger using get' , ( done ) => {
1313+ Parse . Cloud . afterFind ( 'MyObject' , ( req , res ) => {
1314+ for ( let i = 0 ; i < req . objects . length ; i ++ ) {
1315+ req . objects [ i ] . set ( "secretField" , "###" ) ;
1316+ }
1317+ res . success ( req . objects ) ;
1318+ } ) ;
1319+ let obj = new Parse . Object ( 'MyObject' ) ;
1320+ obj . set ( 'secretField' , 'SSID' ) ;
1321+ obj . save ( ) . then ( function ( ) {
1322+ let query = new Parse . Query ( 'MyObject' ) ;
1323+ query . get ( obj . id ) . then ( function ( result ) {
1324+ expect ( result . get ( 'secretField' ) ) . toEqual ( '###' ) ;
1325+ done ( ) ;
1326+ } , function ( error ) {
1327+ fail ( error ) ;
1328+ done ( ) ;
1329+ } ) ;
1330+ } , function ( error ) {
1331+ fail ( error ) ;
1332+ done ( ) ;
1333+ } ) ;
1334+ } ) ;
1335+
1336+ it ( 'should add afterFind trigger using find' , ( done ) => {
1337+ Parse . Cloud . afterFind ( 'MyObject' , ( req , res ) => {
1338+ for ( let i = 0 ; i < req . objects . length ; i ++ ) {
1339+ req . objects [ i ] . set ( "secretField" , "###" ) ;
1340+ }
1341+ res . success ( req . objects ) ;
1342+ } ) ;
1343+ let obj = new Parse . Object ( 'MyObject' ) ;
1344+ obj . set ( 'secretField' , 'SSID' ) ;
1345+ obj . save ( ) . then ( function ( ) {
1346+ let query = new Parse . Query ( 'MyObject' ) ;
1347+ query . equalTo ( 'objectId' , obj . id ) ;
1348+ query . find ( ) . then ( function ( results ) {
1349+ expect ( results [ 0 ] . get ( 'secretField' ) ) . toEqual ( '###' ) ;
1350+ done ( ) ;
1351+ } , function ( error ) {
1352+ fail ( error ) ;
1353+ done ( ) ;
1354+ } ) ;
1355+ } , function ( error ) {
1356+ fail ( error ) ;
1357+ done ( ) ;
1358+ } ) ;
1359+ } ) ;
1360+
1361+ it ( 'should filter out results' , ( done ) => {
1362+ Parse . Cloud . afterFind ( 'MyObject' , ( req , res ) => {
1363+ let filteredResults = [ ] ;
1364+ for ( let i = 0 ; i < req . objects . length ; i ++ ) {
1365+ if ( req . objects [ i ] . get ( "secretField" ) === "SSID1" ) {
1366+ filteredResults . push ( req . objects [ i ] ) ;
1367+ }
1368+ }
1369+ res . success ( filteredResults ) ;
1370+ } ) ;
1371+ let obj0 = new Parse . Object ( 'MyObject' ) ;
1372+ obj0 . set ( 'secretField' , 'SSID1' ) ;
1373+ let obj1 = new Parse . Object ( 'MyObject' ) ;
1374+ obj1 . set ( 'secretField' , 'SSID2' ) ;
1375+ Parse . Object . saveAll ( [ obj0 , obj1 ] ) . then ( function ( ) {
1376+ let query = new Parse . Query ( 'MyObject' ) ;
1377+ query . find ( ) . then ( function ( results ) {
1378+ expect ( results [ 0 ] . get ( 'secretField' ) ) . toEqual ( 'SSID1' ) ;
1379+ expect ( results . length ) . toEqual ( 1 ) ;
1380+ done ( ) ;
1381+ } , function ( error ) {
1382+ fail ( error ) ;
1383+ done ( ) ;
1384+ } ) ;
1385+ } , function ( error ) {
1386+ fail ( error ) ;
1387+ done ( ) ;
1388+ } ) ;
1389+ } ) ;
1390+
1391+ it ( 'should handle failures' , ( done ) => {
1392+ Parse . Cloud . afterFind ( 'MyObject' , ( req , res ) => {
1393+ res . error ( Parse . Error . SCRIPT_FAILED , "It should fail" ) ;
1394+ } ) ;
1395+ let obj = new Parse . Object ( 'MyObject' ) ;
1396+ obj . set ( 'secretField' , 'SSID' ) ;
1397+ obj . save ( ) . then ( function ( ) {
1398+ let query = new Parse . Query ( 'MyObject' ) ;
1399+ query . equalTo ( 'objectId' , obj . id ) ;
1400+ query . find ( ) . then ( function ( results ) {
1401+ fail ( "AfterFind should handle response failure correctly" ) ;
1402+ done ( ) ;
1403+ } , function ( error ) {
1404+ done ( ) ;
1405+ } ) ;
1406+ } , function ( error ) {
1407+ done ( ) ;
1408+ } ) ;
1409+ } ) ;
1410+
1411+ it ( 'should also work with promise' , ( done ) => {
1412+ Parse . Cloud . afterFind ( 'MyObject' , ( req , res ) => {
1413+ let promise = new Parse . Promise ( ) ;
1414+ setTimeout ( function ( ) {
1415+ for ( let i = 0 ; i < req . objects . length ; i ++ ) {
1416+ req . objects [ i ] . set ( "secretField" , "###" ) ;
1417+ }
1418+ promise . resolve ( req . objects ) ;
1419+ } , 1000 ) ;
1420+ return promise ;
1421+ } ) ;
1422+ let obj = new Parse . Object ( 'MyObject' ) ;
1423+ obj . set ( 'secretField' , 'SSID' ) ;
1424+ obj . save ( ) . then ( function ( ) {
1425+ let query = new Parse . Query ( 'MyObject' ) ;
1426+ query . equalTo ( 'objectId' , obj . id ) ;
1427+ query . find ( ) . then ( function ( results ) {
1428+ expect ( results [ 0 ] . get ( 'secretField' ) ) . toEqual ( '###' ) ;
1429+ done ( ) ;
1430+ } , function ( error ) {
1431+ fail ( error ) ;
1432+ } ) ;
1433+ } , function ( error ) {
1434+ fail ( error ) ;
1435+ } ) ;
1436+ } ) ;
1437+
1438+ } ) ;
1439+
0 commit comments