@@ -568,6 +568,70 @@ describe('Parse.Object testing', () => {
568
568
) ;
569
569
} ) ;
570
570
571
+ it_only_db ( 'mongo' ) ( 'can increment array nested fields' , async ( ) => {
572
+ const obj = new TestObject ( ) ;
573
+ obj . set ( 'items' , [ { value : 'a' , count : 5 } , { value : 'b' , count : 1 } ] ) ;
574
+ await obj . save ( ) ;
575
+ obj . increment ( 'items.0.count' , 15 ) ;
576
+ obj . increment ( 'items.1.count' , 4 ) ;
577
+ await obj . save ( ) ;
578
+ expect ( obj . toJSON ( ) . items [ 0 ] . value ) . toBe ( 'a' ) ;
579
+ expect ( obj . toJSON ( ) . items [ 1 ] . value ) . toBe ( 'b' ) ;
580
+ expect ( obj . toJSON ( ) . items [ 0 ] . count ) . toBe ( 20 ) ;
581
+ expect ( obj . toJSON ( ) . items [ 1 ] . count ) . toBe ( 5 ) ;
582
+ const query = new Parse . Query ( TestObject ) ;
583
+ const result = await query . get ( obj . id ) ;
584
+ expect ( result . get ( 'items' ) [ 0 ] . value ) . toBe ( 'a' ) ;
585
+ expect ( result . get ( 'items' ) [ 1 ] . value ) . toBe ( 'b' ) ;
586
+ expect ( result . get ( 'items' ) [ 0 ] . count ) . toBe ( 20 ) ;
587
+ expect ( result . get ( 'items' ) [ 1 ] . count ) . toBe ( 5 ) ;
588
+ expect ( result . get ( 'items' ) ) . toEqual ( obj . get ( 'items' ) ) ;
589
+ } ) ;
590
+
591
+ it_only_db ( 'mongo' ) ( 'can increment array nested fields missing index' , async ( ) => {
592
+ const obj = new TestObject ( ) ;
593
+ obj . set ( 'items' , [ ] ) ;
594
+ await obj . save ( ) ;
595
+ obj . increment ( 'items.1.count' , 15 ) ;
596
+ await obj . save ( ) ;
597
+ expect ( obj . toJSON ( ) . items [ 0 ] ) . toBe ( null ) ;
598
+ expect ( obj . toJSON ( ) . items [ 1 ] . count ) . toBe ( 15 ) ;
599
+ const query = new Parse . Query ( TestObject ) ;
600
+ const result = await query . get ( obj . id ) ;
601
+ expect ( result . get ( 'items' ) [ 0 ] ) . toBe ( null ) ;
602
+ expect ( result . get ( 'items' ) [ 1 ] . count ) . toBe ( 15 ) ;
603
+ expect ( result . get ( 'items' ) ) . toEqual ( obj . get ( 'items' ) ) ;
604
+ } ) ;
605
+
606
+ it ( 'can query array nested fields' , async ( ) => {
607
+ const objects = [ ] ;
608
+ for ( let i = 0 ; i < 10 ; i ++ ) {
609
+ const obj = new TestObject ( ) ;
610
+ obj . set ( 'items' , [ i , { value : i } ] ) ;
611
+ objects . push ( obj ) ;
612
+ }
613
+ await Parse . Object . saveAll ( objects ) ;
614
+ let query = new Parse . Query ( TestObject ) ;
615
+ query . greaterThan ( 'items.1.value' , 5 ) ;
616
+ let result = await query . find ( ) ;
617
+ expect ( result . length ) . toBe ( 4 ) ;
618
+
619
+ query = new Parse . Query ( TestObject ) ;
620
+ query . lessThan ( 'items.0' , 3 ) ;
621
+ result = await query . find ( ) ;
622
+ expect ( result . length ) . toBe ( 3 ) ;
623
+
624
+ query = new Parse . Query ( TestObject ) ;
625
+ query . equalTo ( 'items.0' , 5 ) ;
626
+ result = await query . find ( ) ;
627
+ expect ( result . length ) . toBe ( 1 ) ;
628
+
629
+ query = new Parse . Query ( TestObject ) ;
630
+ query . notEqualTo ( 'items.0' , 5 ) ;
631
+ result = await query . find ( ) ;
632
+ expect ( result . length ) . toBe ( 9 ) ;
633
+ } ) ;
634
+
571
635
it ( 'addUnique with object' , function ( done ) {
572
636
const x1 = new Parse . Object ( 'X' ) ;
573
637
x1 . set ( 'stuff' , [ 1 , { hello : 'world' } , { foo : 'bar' } ] ) ;
0 commit comments