Skip to content

Commit 61f10e0

Browse files
committed
Added tests for parser() function and adjustments in index.d.ts
1 parent 65ce1a5 commit 61f10e0

File tree

7 files changed

+92
-13
lines changed

7 files changed

+92
-13
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare namespace queryStringsParser {
55
export interface QueryStringsParser {
66
(options?: IOptions): any
77

8-
parser(query?: string | object, options?: IOptions): any
8+
parser(query?: string | object, defaults?: IDefault, options?: IOptions): any
99

1010
parseFields(query?: string | object, options?: IOptions): any
1111

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
2-
32
const read = require('./lib/read')
43

5-
64
exports = module.exports = function (params) {
75
return read.parser(params)
86
}

lib/read.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function validate_options(params) {
6666
}
6767

6868
exports = module.exports.parseAll = function (_query, _default, _options) {
69-
const params = validate_options(Object.assign({'default': _default} || {}, _options || {}))
69+
const params = validate_options(Object.assign({'default': _default}, _options || {}))
7070
return {
7171
fields: fields.fields(stringToJson(_query), params),
7272
sort: ordination.sort(stringToJson(_query), params),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-strings-parser",
3-
"version": "2.1.0",
3+
"version": "2.1.2",
44
"description": "Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL and other databases...",
55
"license": "MIT",
66
"main": "index.js",
@@ -48,4 +48,4 @@
4848
"nyc": "^14.1.1",
4949
"supertest": "^4.0.2"
5050
}
51-
}
51+
}

test/integration/index.custom.config.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ describe('queryFilter()', function () {
108108
})
109109

110110
function validate(query, options) {
111-
expect(query).is.not.null
112111
expect(query).is.not.eql({})
113112
expect(query.pagination.limit).to.eql(options.default.pagination.limit)
114113
expect(query.pagination.page).to.eql(options.default.pagination.page)
115114
expect(query.sort).to.eql(options.default.sort)
116115
expect(query.fields).to.eql(options.default.fields)
117116
expect(query.filters).to.eql(options.default.filters)
118-
}
117+
}

test/integration/index.default.config.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ describe('queryFilter()', function () {
511511

512512

513513
function validate(query, options) {
514-
expect(query).is.not.null
515514
expect(query).is.not.eql({})
516515
expect(query.pagination.limit).to.eql(options.default.pagination.limit)
517516
expect(query.pagination.skip).to.eql(options.default.pagination.skip)
@@ -521,7 +520,6 @@ function validate(query, options) {
521520
}
522521

523522
function validateWithPeriod(query, options) {
524-
expect(query).is.not.null
525523
expect(query).is.not.eql({})
526524
expect(query.pagination.limit).to.eql(options.default.pagination.limit)
527525
expect(query.pagination.skip).to.eql(options.default.pagination.skip)
@@ -541,4 +539,4 @@ function dateToString(date) {
541539
return `${date.getFullYear()}-`
542540
.concat(`${(date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0'.concat((date.getMonth() + 1))}-`)
543541
.concat(`${date.getDate() > 9 ? date.getDate() : '0'.concat(date.getDate())}`)
544-
}
542+
}

test/unit/index.spec.js

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,90 @@ describe('QueryString: Parsers', function () {
119119
})
120120
})
121121

122-
context('when use parse with defualt date fields', function () {
122+
context('when use parse with default date fields', function () {
123123
it('should return parse query date merge with default date', function () {
124124
const query = '?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'
125125
const result = index.parseDate(query, {start_at: 'created_at', end_at: 'created_at'})
126126
verifyDate(result)
127127
})
128128
})
129129
})
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+
})
130206
})
131207

132208
function verifyFields(result) {
@@ -166,4 +242,12 @@ function verifyDate(result) {
166242
expect(result.$and).to.have.lengthOf(2)
167243
expect(result.$and[0]).to.have.all.keys('created_at')
168244
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

Comments
 (0)