@@ -119,14 +119,90 @@ describe('QueryString: Parsers', function () {
119
119
} )
120
120
} )
121
121
122
- context ( 'when use parse with defualt date fields' , function ( ) {
122
+ context ( 'when use parse with default date fields' , function ( ) {
123
123
it ( 'should return parse query date merge with default date' , function ( ) {
124
124
const query = '?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'
125
125
const result = index . parseDate ( query , { start_at : 'created_at' , end_at : 'created_at' } )
126
126
verifyDate ( result )
127
127
} )
128
128
} )
129
129
} )
130
+
131
+ describe ( 'parser()' , function ( ) {
132
+ context ( 'when parser is used with defaults options' , function ( ) {
133
+ it ( 'should return object with fields, sort, filters, pagination and original query' , function ( ) {
134
+ verifyParser ( index . parser ( '' ) )
135
+ } )
136
+
137
+ it ( 'should return parse query fields' , function ( ) {
138
+ const query = '?fields=name,age,created_at'
139
+ verifyFields ( index . parser ( query ) . fields )
140
+ } )
141
+
142
+ it ( 'should return parse query sort' , function ( ) {
143
+ const query = '?sort=name,-age,created_at'
144
+ verifySort ( index . parser ( query ) . sort )
145
+ } )
146
+
147
+ it ( 'should return parse query pagination' , function ( ) {
148
+ const query = '?limit=20&skip=3'
149
+ const result = index . parser ( query )
150
+ expect ( result . pagination ) . to . have . property ( 'limit' , 20 )
151
+ expect ( result . pagination ) . to . have . property ( 'skip' , 3 )
152
+ } )
153
+
154
+ it ( 'should return parse query filter' , function ( ) {
155
+ const query = '?name=lucas&age=30'
156
+ verifyFilter ( index . parser ( query ) . filters )
157
+ } )
158
+
159
+ it ( 'should return parse query date' , function ( ) {
160
+ const query = '?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'
161
+ verifyDate ( index . parser ( query ) . filters )
162
+ } )
163
+ } )
164
+
165
+ context ( 'when parser is used with custom options' , function ( ) {
166
+ it ( 'should return parse query fields merged with default fields' , function ( ) {
167
+ const query = '?fields=name,age,created_at'
168
+ const result = index . parser ( query , { fields : { _id : 0 } } )
169
+ verifyFields ( result . fields )
170
+ expect ( result . fields ) . to . have . property ( '_id' , 0 )
171
+ } )
172
+
173
+ it ( 'should return parsing query classification merged with custom classification' , function ( ) {
174
+ const query = '?sort=name,-age,created_at'
175
+ const result = index . parser ( query , { sort : { _id : 'desc' } } )
176
+ verifySort ( result . sort )
177
+ expect ( result . sort ) . to . have . property ( '_id' , 'desc' )
178
+ } )
179
+
180
+ it ( 'should return parse query pagination' , function ( ) {
181
+ const query = '?page=3'
182
+ verifyPage ( index . parser ( query ,
183
+ { pagination : { limit : 20 } } ,
184
+ { use_page : true }
185
+ ) . pagination )
186
+ } )
187
+
188
+ it ( 'should return parse query filters merge with custom filters' , function ( ) {
189
+ const query = '?name=lucas&age=30'
190
+ const result = index . parser ( query , { filters : { 'job' : 'Engineer' } } )
191
+ verifyFilter ( result . filters )
192
+ expect ( result . filters ) . to . have . property ( 'job' , 'Engineer' )
193
+ } )
194
+
195
+ it ( 'should return parse query date merge with default date' , function ( ) {
196
+ const query = '?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'
197
+ const result = index . parser (
198
+ query ,
199
+ { } ,
200
+ { date_fields : { start_at : 'timestamp' , end_at : 'timestamp' } } )
201
+ expect ( result . filters . $and [ 0 ] ) . to . have . all . keys ( 'timestamp' )
202
+ expect ( result . filters . $and [ 1 ] ) . to . have . all . keys ( 'timestamp' )
203
+ } )
204
+ } )
205
+ } )
130
206
} )
131
207
132
208
function verifyFields ( result ) {
@@ -166,4 +242,12 @@ function verifyDate(result) {
166
242
expect ( result . $and ) . to . have . lengthOf ( 2 )
167
243
expect ( result . $and [ 0 ] ) . to . have . all . keys ( 'created_at' )
168
244
expect ( result . $and [ 1 ] ) . to . have . all . keys ( 'created_at' )
169
- }
245
+ }
246
+
247
+ function verifyParser ( result ) {
248
+ expect ( result ) . to . have . property ( 'fields' )
249
+ expect ( result ) . to . have . property ( 'sort' )
250
+ expect ( result ) . to . have . property ( 'filters' )
251
+ expect ( result ) . to . have . property ( 'pagination' )
252
+ expect ( result ) . to . have . property ( 'original' )
253
+ }
0 commit comments