@@ -684,4 +684,150 @@ describe('read lists of ideas', () => {
684
684
} ) ;
685
685
} ) ;
686
686
} ) ;
687
+
688
+ describe ( 'GET /ideas?filter[highlyVoted]' , ( ) => {
689
+ let user0 ;
690
+ // create and save testing data
691
+ beforeEach ( async ( ) => {
692
+ const primarys = 'ideas' ;
693
+ const data = {
694
+ users : 6 ,
695
+ tags : 6 ,
696
+ verifiedUsers : [ 0 , 1 , 2 , 3 , 4 ] ,
697
+ ideas : [ [ { } , 0 ] , [ { } , 0 ] , [ { } , 1 ] , [ { } , 2 ] , [ { } , 2 ] , [ { } , 2 ] , [ { } , 3 ] ] ,
698
+ userTag : [
699
+ [ 0 , 0 , '' , 5 ] , [ 0 , 1 , '' , 4 ] , [ 0 , 2 , '' , 3 ] , [ 0 , 4 , '' , 1 ] ,
700
+ [ 1 , 1 , '' , 4 ] , [ 1 , 3 , '' , 2 ] ,
701
+ [ 2 , 5 , '' , 2 ]
702
+ ] ,
703
+ ideaTags : [
704
+ [ 0 , 0 ] , [ 0 , 1 ] , [ 0 , 2 ] ,
705
+ [ 1 , 1 ] , [ 1 , 2 ] ,
706
+ [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 4 ] ,
707
+ [ 4 , 0 ] , [ 4 , 1 ] , [ 4 , 2 ] , [ 4 , 3 ] , [ 4 , 4 ] ,
708
+ [ 5 , 2 ] , [ 5 , 3 ] ,
709
+ [ 6 , 3 ]
710
+ ] ,
711
+ // odeas with votes: 3:3, 1:3, 5:1, 2:1, 0:0, 6: -1, 4:-2
712
+ votes : [
713
+ [ 0 , [ primarys , 0 ] , - 1 ] ,
714
+ [ 1 , [ primarys , 0 ] , 1 ] ,
715
+ [ 0 , [ primarys , 1 ] , 1 ] ,
716
+ [ 1 , [ primarys , 1 ] , 1 ] ,
717
+ [ 2 , [ primarys , 1 ] , 1 ] ,
718
+ [ 0 , [ primarys , 2 ] , - 1 ] ,
719
+ [ 1 , [ primarys , 2 ] , 1 ] ,
720
+ [ 2 , [ primarys , 2 ] , 1 ] ,
721
+ [ 0 , [ primarys , 3 ] , 1 ] ,
722
+ [ 1 , [ primarys , 3 ] , 1 ] ,
723
+ [ 2 , [ primarys , 3 ] , 1 ] ,
724
+ [ 3 , [ primarys , 3 ] , 1 ] ,
725
+ [ 4 , [ primarys , 3 ] , - 1 ] ,
726
+ [ 0 , [ primarys , 4 ] , - 1 ] ,
727
+ [ 1 , [ primarys , 4 ] , - 1 ] ,
728
+ [ 3 , [ primarys , 5 ] , 1 ] ,
729
+ [ 3 , [ primarys , 6 ] , - 1 ]
730
+ ]
731
+ } ;
732
+
733
+ dbData = await dbHandle . fill ( data ) ;
734
+
735
+ [ user0 , , , , , ] = dbData . users ;
736
+ } ) ;
737
+
738
+ context ( 'logged in' , ( ) => {
739
+
740
+ beforeEach ( ( ) => {
741
+ agent = agentFactory . logged ( user0 ) ;
742
+ } ) ;
743
+
744
+ context ( 'valid data' , ( ) => {
745
+
746
+ it ( '[highly voted ideas] 200 and return array of matched ideas' , async ( ) => {
747
+
748
+ // request
749
+ const response = await agent
750
+ . get ( '/ideas?filter[highlyVoted]=0' )
751
+ . expect ( 200 ) ;
752
+
753
+ // without pagination, limit for ideas 5 we should find 5 ideas...
754
+ should ( response . body ) . have . property ( 'data' ) . Array ( ) . length ( 5 ) ;
755
+
756
+ // sorted by creation date desc
757
+ should ( response . body . data . map ( idea => idea . attributes . title ) )
758
+ . eql ( [ 3 , 1 , 5 , 2 , 0 ] . map ( no => `idea title ${ no } ` ) ) ;
759
+
760
+ } ) ;
761
+
762
+ it ( '[highly voted ideas with at least 2 votes in plus] 200 and return array of matched ideas' , async ( ) => {
763
+
764
+ // request
765
+ const response = await agent
766
+ . get ( '/ideas?filter[highlyVoted]=2' )
767
+ . expect ( 200 ) ;
768
+
769
+ // without pagination, limit for ideas 5 we should find 5 ideas...
770
+ should ( response . body ) . have . property ( 'data' ) . Array ( ) . length ( 2 ) ;
771
+
772
+ // sorted by creation date desc
773
+ should ( response . body . data . map ( idea => idea . attributes . title ) )
774
+ . eql ( [ 3 , 1 ] . map ( no => `idea title ${ no } ` ) ) ;
775
+
776
+ // shoud value be at least 2
777
+ should ( Math . min ( ...response . body . data . map ( idea => idea . meta . voteSum ) ) )
778
+ . aboveOrEqual ( 2 ) ;
779
+ } ) ;
780
+
781
+
782
+ it ( '[pagination] offset and limit the results' , async ( ) => {
783
+ const response = await agent
784
+ . get ( '/ideas?filter[highlyVoted]=0&page[offset]=1&page[limit]=3' )
785
+ . expect ( 200 ) ;
786
+
787
+ // we should find 3 ideas
788
+ should ( response . body ) . have . property ( 'data' ) . Array ( ) . length ( 3 ) ;
789
+
790
+ // sorted by creation date desc
791
+ should ( response . body . data . map ( idea => idea . attributes . title ) )
792
+ . eql ( [ 1 , 5 , 2 ] . map ( no => `idea title ${ no } ` ) ) ;
793
+ } ) ;
794
+
795
+ } ) ;
796
+
797
+ context ( 'invalid data' , ( ) => {
798
+
799
+ it ( '[invalid query.filter.highlyVoted] 400' , async ( ) => {
800
+ await agent
801
+ . get ( '/ideas?filter[highlyVoted]=string' )
802
+ . expect ( 400 ) ;
803
+ } ) ;
804
+
805
+ it ( '[invalid query.filter.highlyVoted] 400' , async ( ) => {
806
+ await agent
807
+ . get ( '/ideas?filter[highlyVoted]' )
808
+ . expect ( 400 ) ;
809
+ } ) ;
810
+
811
+ it ( '[invalid pagination] 400' , async ( ) => {
812
+ await agent
813
+ . get ( '/ideas?filter[highlyVoted]=0&page[offset]=1&page[limit]=21' )
814
+ . expect ( 400 ) ;
815
+ } ) ;
816
+
817
+ it ( '[unexpected query params] 400' , async ( ) => {
818
+ await agent
819
+ . get ( '/ideas?filter[highlyVoted]=0&additional[param]=3&page[offset]=1&page[limit]=3' )
820
+ . expect ( 400 ) ;
821
+ } ) ;
822
+ } ) ;
823
+ } ) ;
824
+
825
+ context ( 'not logged in' , ( ) => {
826
+ it ( '403' , async ( ) => {
827
+ await agent
828
+ . get ( '/ideas?filter[highlyVoted]=0' )
829
+ . expect ( 403 ) ;
830
+ } ) ;
831
+ } ) ;
832
+ } ) ;
687
833
} ) ;
0 commit comments