@@ -9,6 +9,7 @@ describe('queryFilter()', function () {
9
9
10
10
context ( 'when use default configurations' , function ( ) {
11
11
context ( 'when query is empty' , function ( ) {
12
+
12
13
it ( 'should return req.query as default middleware options' , function ( done ) {
13
14
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : { } } )
14
15
const res = httpMocks . createResponse ( )
@@ -113,6 +114,21 @@ describe('queryFilter()', function () {
113
114
} )
114
115
} )
115
116
117
+
118
+ context ( 'when query filters param value is a object' , function ( ) {
119
+ it ( 'should return req.query as default middleware options' , function ( done ) {
120
+
121
+ const query = { value : { and : 123 } }
122
+ const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
123
+ const res = httpMocks . createResponse ( )
124
+
125
+ qs ( { } ) ( req , res , function next ( ) {
126
+ validate ( req , default_options )
127
+ } )
128
+ done ( )
129
+ } )
130
+ } )
131
+
116
132
context ( 'when query contains simple filters param' , function ( ) {
117
133
it ( 'should return req.query with set field params' , function ( done ) {
118
134
const expect_filters = { name : 'lucas' , age : 30 }
@@ -200,15 +216,15 @@ describe('queryFilter()', function () {
200
216
} )
201
217
202
218
context ( 'when query contains date filters param' , function ( ) {
203
- it ( 'should return req.query with set date_start params as date' , function ( done ) {
219
+ it ( 'should return req.query with set start_at params as date' , function ( done ) {
204
220
const expect_filters = {
205
221
$and : [
206
222
{ created_at : { $lt : new Date ( ) . toISOString ( ) } } ,
207
223
{ created_at : { $gte : '2018-12-05T00:00:00.000Z' } }
208
224
]
209
225
}
210
226
211
- const query = { date_start : '2018-12-05' }
227
+ const query = { start_at : '2018-12-05' }
212
228
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
213
229
const res = httpMocks . createResponse ( )
214
230
@@ -221,15 +237,15 @@ describe('queryFilter()', function () {
221
237
done ( )
222
238
} )
223
239
224
- it ( 'should return req.query with set date_start params as dateTime' , function ( done ) {
240
+ it ( 'should return req.query with set start_at params as dateTime' , function ( done ) {
225
241
const expect_filters = {
226
242
$and : [
227
243
{ created_at : { $lt : new Date ( ) . toISOString ( ) } } ,
228
244
{ created_at : { $gte : '2018-12-05T00:00:00.000Z' } }
229
245
]
230
246
}
231
247
232
- const query = { date_start : '2018-12-05T00:00:00' }
248
+ const query = { start_at : '2018-12-05T00:00:00' }
233
249
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
234
250
const res = httpMocks . createResponse ( )
235
251
@@ -242,15 +258,15 @@ describe('queryFilter()', function () {
242
258
done ( )
243
259
} )
244
260
245
- it ( 'should return req.query with set date_start and date_end params as date' , function ( done ) {
261
+ it ( 'should return req.query with set start_at and end_at params as date' , function ( done ) {
246
262
247
263
const expect_filters = {
248
264
$and : [
249
265
{ created_at : { $lt : '2018-12-11T00:00:00.000Z' } } ,
250
266
{ created_at : { $gte : '2018-12-01T00:00:00.000Z' } } ]
251
267
}
252
268
253
- const query = { date_start : '2018-12-01' , date_end : '2018-12-11' }
269
+ const query = { start_at : '2018-12-01' , end_at : '2018-12-11' }
254
270
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
255
271
const res = httpMocks . createResponse ( )
256
272
@@ -263,15 +279,15 @@ describe('queryFilter()', function () {
263
279
done ( )
264
280
} )
265
281
266
- it ( 'should return req.query with set date_start and date_end params as dateTime' , function ( done ) {
282
+ it ( 'should return req.query with set start_at and end_at params as dateTime' , function ( done ) {
267
283
268
284
const expect_filters = {
269
285
$and : [
270
286
{ created_at : { $lt : '2018-12-11T00:00:00.000Z' } } ,
271
287
{ created_at : { $gte : '2018-12-01T00:00:00.000Z' } } ]
272
288
}
273
289
274
- const query = { date_start : '2018-12-01T00:00:00' , date_end : '2018-12-11T00:00:00' }
290
+ const query = { start_at : '2018-12-01T00:00:00' , end_at : '2018-12-11T00:00:00' }
275
291
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
276
292
const res = httpMocks . createResponse ( )
277
293
@@ -284,15 +300,14 @@ describe('queryFilter()', function () {
284
300
done ( )
285
301
} )
286
302
287
- it ( 'should return req.query with set period and date_end param' , function ( done ) {
288
-
303
+ it ( 'should return req.query with set period and end_at param' , function ( done ) {
289
304
const expect_filters = {
290
305
$and : [
291
306
{ created_at : { $lt : new Date ( '2018-12-09' ) . toISOString ( ) } } ,
292
307
{ created_at : { $gte : new Date ( '2018-11-10' ) . toISOString ( ) } }
293
308
]
294
309
}
295
- const query = { period : '1m' , date_end : '2018-12-09' }
310
+ const query = { period : '1m' , end_at : '2018-12-09' }
296
311
const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
297
312
const res = httpMocks . createResponse ( )
298
313
@@ -402,6 +417,31 @@ describe('queryFilter()', function () {
402
417
} )
403
418
done ( )
404
419
} )
420
+
421
+ it ( 'should return req.query with today start_at for invalid period' , function ( done ) {
422
+
423
+ const now = new Date ( )
424
+ const today = dateToString ( new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) + 1 ) )
425
+ const beforeToday = dateToString ( new Date ( now . getFullYear ( ) - 1 , now . getMonth ( ) , now . getDate ( ) ) )
426
+ const expect_filters = {
427
+ $and : [
428
+ { created_at : { $lt : new Date ( today ) . toISOString ( ) } } ,
429
+ { created_at : { $gte : new Date ( beforeToday ) . toISOString ( ) } }
430
+ ]
431
+ }
432
+
433
+ const query = { period : '12' }
434
+ const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
435
+ const res = httpMocks . createResponse ( )
436
+
437
+ const options = JSON . parse ( JSON . stringify ( default_options ) )
438
+ options . default . filters = expect_filters
439
+
440
+ qs ( { } ) ( req , res , function next ( ) {
441
+ validateFilterWithDate ( req , options )
442
+ } )
443
+ done ( )
444
+ } )
405
445
} )
406
446
407
447
context ( 'when use custom options' , function ( ) {
0 commit comments