Skip to content

Commit 7455cac

Browse files
committed
Corrects error in ordering when defaults values are used
1 parent d5d63ed commit 7455cac

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

lib/mapper/ordination.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function processQuery(query) {
2727

2828

2929
function validate_sort(_values, _default) {
30-
return Object.assign(_values, _default)
30+
return {
31+
..._default, ..._values
32+
}
3133
}
3234

3335
exports = module.exports = {

test/unit/ordination.spec.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,42 @@ const expect = require('chai').expect
22
const ordination = require('../../lib/mapper/ordination')
33

44
describe('QueryString: Ordination', function () {
5-
5+
66
context('when ordination query is a simple string', function () {
77
it('should return a JSON with order params', function (done) {
8-
verify(ordination.sort({ sort: '-name,age,created_at' }, default_options))
8+
verify(ordination.sort({sort: '-name,age,created_at'}, default_options))
99
done()
1010
})
1111
})
1212

1313
context('when ordination query is an array of strings', function () {
1414
it('should return a JSON with order params', function (done) {
15-
verify(ordination.sort({ sort: ['-name,age', 'created_at'] }, default_options))
15+
verify(ordination.sort({sort: ['-name,age', 'created_at']}, default_options))
1616
done()
1717
})
1818
})
1919

2020
context('when there are blank spaces between ordination query', function () {
2121
it('should return a JSON with order params, ignoring the blank space', function (done) {
22-
verify(ordination.sort({ sort: '-na m e, age, cr eat ed_at' }, default_options))
22+
verify(ordination.sort({sort: '-na m e, age, cr eat ed_at'}, default_options))
2323
done()
2424
})
2525
})
2626

2727
context('when there are null fields in ordination query', function () {
2828
it('should return a JSON with order params, ignoring the null fields', function (done) {
29-
verify(ordination.sort({ sort: ',,,,,-name,,,,age,,,created_at,,,,,,,' }, default_options))
29+
verify(ordination.sort({sort: ',,,,,-name,,,,age,,,created_at,,,,,,,'}, default_options))
3030
done()
3131
})
3232
})
3333

3434
context('when there are special characters in ordination query', function () {
3535
it('should return a JSON with order params, ignoring the special characteres', function (done) {
36-
verify(ordination.sort({ sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at' }, default_options))
36+
verify(ordination.sort({sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at'}, default_options))
3737
done()
3838
})
3939

40-
})
40+
})
4141

4242
context('when use the default options without query', function () {
4343
it('should return a JSON with default ordination params', function (done) {
@@ -48,16 +48,23 @@ describe('QueryString: Ordination', function () {
4848
})
4949
})
5050

51-
context('when use custom params without query', function () {
51+
context('when use custom params', function () {
5252
it('should return a JSON with custom params', function () {
53-
const custom_options = { default: { sort: { created_at: 'asc' }}}
53+
const custom_options = {default: {sort: {created_at: 'asc'}}}
5454
const result = ordination.sort({}, custom_options)
5555
expect(result).is.not.null
5656
expect(result).to.have.property('created_at')
5757
expect(result.created_at).to.eql('asc')
5858
})
59-
})
6059

60+
it('should return a JSON with custom parameters and those of the query', function () {
61+
const custom_options = {default: {sort: {created_at: 'asc'}}}
62+
const result = ordination.sort({sort: '-created_at,-age,name'}, custom_options)
63+
expect(result.created_at).to.eql('desc')
64+
expect(result.age).to.eql('desc')
65+
expect(result.name).to.eql('asc')
66+
})
67+
})
6168
})
6269

6370
function verify(result) {
@@ -68,4 +75,4 @@ function verify(result) {
6875
expect(result.age).to.eql('asc')
6976
expect(result.created_at).to.eql('asc')
7077

71-
}
78+
}

0 commit comments

Comments
 (0)