@@ -55,6 +55,7 @@ describe('service > resources-getter', () => {
55
55
type : 'String' ,
56
56
get : ( film ) => `${ film . title } ${ film . duration } ` ,
57
57
} ,
58
+ { field : 'rating' , type : 'Number' } ,
58
59
] ,
59
60
} ,
60
61
} ,
@@ -78,6 +79,7 @@ describe('service > resources-getter', () => {
78
79
_id : { type : 'ObjectId' } ,
79
80
title : { type : String } ,
80
81
duration : { type : Number } ,
82
+ rating : { type : Number } ,
81
83
} ) ;
82
84
83
85
OrderModel = mongoose . model ( 'Order' , OrderSchema ) ;
@@ -134,11 +136,13 @@ describe('service > resources-getter', () => {
134
136
_id : '41224d776a326fb40f000011' ,
135
137
duration : 149 ,
136
138
title : 'Terminator' ,
139
+ rating : 4.5 ,
137
140
} ,
138
141
{
139
142
_id : '41224d776a326fb40f000012' ,
140
143
duration : 360 ,
141
144
title : 'Titanic' ,
145
+ rating : 4 ,
142
146
} ,
143
147
{
144
148
_id : '41224d776a326fb40f000013' ,
@@ -390,5 +394,56 @@ describe('service > resources-getter', () => {
390
394
expect ( durations ) . toHaveLength ( 0 ) ;
391
395
} ) ;
392
396
} ) ;
397
+
398
+ describe ( 'with a condition on a non-filtered field' , ( ) => {
399
+ it ( 'should return filtered results on the rating' , async ( ) => {
400
+ expect . assertions ( 1 ) ;
401
+
402
+ const parameters = {
403
+ fields : { films : 'title' } ,
404
+ page : { number : '1' , size : '15' } ,
405
+ filters : '{"field":"rating","operator":"present","value":null}' ,
406
+ timezone : 'Europe/Paris' ,
407
+ } ;
408
+
409
+ const result = await new ResourcesGetter ( FilmModel , options , parameters ) . perform ( ) ;
410
+
411
+ expect ( result [ 0 ] ) . toHaveLength ( 2 ) ;
412
+ } ) ;
413
+
414
+ it ( 'should return sorted results by rating (asc)' , async ( ) => {
415
+ expect . assertions ( 3 ) ;
416
+
417
+ const parameters = {
418
+ fields : { films : 'title' } ,
419
+ page : { number : '1' , size : '15' } ,
420
+ sort : 'rating' ,
421
+ timezone : 'Europe/Paris' ,
422
+ } ;
423
+
424
+ const result = await new ResourcesGetter ( FilmModel , options , parameters ) . perform ( ) ;
425
+
426
+ expect ( result [ 0 ] ) . toHaveLength ( 3 ) ;
427
+ expect ( result [ 0 ] [ 0 ] . title ) . toBe ( 'Matrix' ) ;
428
+ expect ( result [ 0 ] [ 1 ] . title ) . toBe ( 'Titanic' ) ;
429
+ } ) ;
430
+
431
+ it ( 'should return sorted results by rating (desc)' , async ( ) => {
432
+ expect . assertions ( 3 ) ;
433
+
434
+ const parameters = {
435
+ fields : { films : 'title' } ,
436
+ page : { number : '1' , size : '15' } ,
437
+ sort : '-rating' ,
438
+ timezone : 'Europe/Paris' ,
439
+ } ;
440
+
441
+ const result = await new ResourcesGetter ( FilmModel , options , parameters ) . perform ( ) ;
442
+
443
+ expect ( result [ 0 ] ) . toHaveLength ( 3 ) ;
444
+ expect ( result [ 0 ] [ 0 ] . title ) . toBe ( 'Terminator' ) ;
445
+ expect ( result [ 0 ] [ 1 ] . title ) . toBe ( 'Titanic' ) ;
446
+ } ) ;
447
+ } ) ;
393
448
} ) ;
394
449
} ) ;
0 commit comments