@@ -35,6 +35,69 @@ describe('QueryString: Filters', function () {
3535 } )
3636 } )
3737
38+ context ( 'when query filters have multiple values for multiple fields (status and sourcehealthentity_id)' , function ( ) {
39+ it ( 'should return a JSON with filter parameters containing a $AND filter with multiple $OR filters' , function ( done ) {
40+ const bedId = generateObjectId ( )
41+ const requestStatusList = 'regulated,'
42+ const sourceHealthEntityIds = `${ generateObjectId ( ) } ,${ generateObjectId ( ) } `
43+ const result = filter . filters ( {
44+ bed_id : bedId , status : requestStatusList , sourcehealthentity_id : sourceHealthEntityIds
45+ } , default_options )
46+
47+ expect ( result ) . to . have . property ( '$and' )
48+ expect ( result [ '$and' ] ) . to . eql ( [
49+ { '$or' : [ { status : requestStatusList . split ( ',' ) [ 0 ] } , { status : requestStatusList . split ( ',' ) [ 1 ] } ] } ,
50+ {
51+ '$or' : [
52+ { sourcehealthentity_id : sourceHealthEntityIds . split ( ',' ) [ 0 ] } ,
53+ { sourcehealthentity_id : sourceHealthEntityIds . split ( ',' ) [ 1 ] }
54+ ]
55+ } ] )
56+ expect ( result . bed_id ) . to . eql ( bedId )
57+
58+ done ( )
59+ } )
60+ } )
61+
62+ context ( 'when query filters have multiple values for one field (status)' , function ( ) {
63+ it ( 'should return a JSON with filter parameters containing a $OR filter with multiple values' , function ( done ) {
64+ const bedId = generateObjectId ( )
65+ const requestStatusList = 'regulated,'
66+ const sourceHealthEntityId = generateObjectId ( )
67+ const result = filter . filters ( {
68+ bed_id : bedId , status : requestStatusList , sourcehealthentity_id : sourceHealthEntityId
69+ } , default_options )
70+ expect ( result ) . to . not . have . property ( '$and' )
71+ expect ( result ) . to . have . property ( '$or' )
72+ expect ( result [ '$or' ] ) . to . eql ( [ { status : requestStatusList . split ( ',' ) [ 0 ] } , { status : requestStatusList . split ( ',' ) [ 1 ] } ] )
73+ expect ( result . bed_id ) . to . eql ( bedId )
74+ expect ( result . sourcehealthentity_id ) . to . eql ( sourceHealthEntityId )
75+
76+ done ( )
77+ } )
78+ } )
79+
80+ context ( 'when query filters have multiple values for one field (sourcehealthentity_id)' , function ( ) {
81+ it ( 'should return a JSON with filter parameters containing a $OR filter with multiple values' , function ( done ) {
82+ const bedId = generateObjectId ( )
83+ const requestStatus = 'regulated'
84+ const sourceHealthEntityIds = `${ generateObjectId ( ) } ,${ generateObjectId ( ) } `
85+ const result = filter . filters ( {
86+ bed_id : bedId , status : requestStatus , sourcehealthentity_id : sourceHealthEntityIds
87+ } , default_options )
88+ expect ( result ) . to . not . have . property ( '$and' )
89+ expect ( result ) . to . have . property ( '$or' )
90+ expect ( result [ '$or' ] ) . to . eql ( [
91+ { sourcehealthentity_id : sourceHealthEntityIds . split ( ',' ) [ 0 ] } ,
92+ { sourcehealthentity_id : sourceHealthEntityIds . split ( ',' ) [ 1 ] }
93+ ] )
94+ expect ( result . bed_id ) . to . eql ( bedId )
95+ expect ( result . status ) . to . eql ( requestStatus )
96+
97+ done ( )
98+ } )
99+ } )
100+
38101 context ( 'when query filters key contains blank space' , function ( ) {
39102 it ( 'should return a JSON with filters params, ignoring blank spaces' , function ( done ) {
40103 verify ( filter . filters ( { ' na me ' : 'lucas' , age : '30' } , default_options ) )
@@ -76,3 +139,17 @@ function verify(result) {
76139 expect ( result . name ) . to . eql ( 'lucas' )
77140 expect ( result . age ) . to . eql ( 30 )
78141}
142+
143+ /**
144+ * Randomly generates a valid 24-byte hex ID.
145+ *
146+ * @returns {string }
147+ */
148+ function generateObjectId ( ) {
149+ const chars = 'abcdef0123456789'
150+ let randS = ''
151+ for ( let i = 0 ; i < 24 ; i ++ ) {
152+ randS += chars . charAt ( Math . floor ( Math . random ( ) * chars . length ) )
153+ }
154+ return randS
155+ }
0 commit comments